Install and configure MongoDB community edition
Perform these steps on the Amazon EC2 instance that you launched in Launch an Amazon EC2 instance.
-
Go to Install MongoDB community edition on Amazon Linux
in the MongoDB documentation and follow the instructions there. -
By default, the MongoDB server (
mongod) only allows loopback connections from IP address 127.0.0.1 (localhost). To allow connections from elsewhere in your Amazon VPC, do the following:-
Edit the
/etc/mongod.conffile and look for the following lines.# network interfaces net: port: 27017 bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
-
Modify the
bindIpline so that it looks like the following.bindIp: public-dns-name
-
Replace
with the actual public DNS name for your instance, for examplepublic-dns-nameec2-11-22-33-44.us-west-2.compute.amazonaws.com. -
Save the
/etc/mongod.conffile, and then restartmongod.sudo service mongod restart
-
-
Populate your MongoDB instance with data by doing the following:
-
Use the
wgetcommand to download a JSON file containing sample data.wget http://media.mongodb.org/zips.json
-
Use the
mongoimportcommand to import the data into a new database (zips-db).mongoimport --host public-dns-name:27017 --db zips-db --file zips.json
-
After the import completes, use the
mongoshell to connect to MongoDB and verify that the data was loaded successfully.mongo --host public-dns-name:27017
-
Replace
with the actual public DNS name for your instance.public-dns-name -
At the
mongoshell prompt, enter the following commands.use zips-db db.zips.count() db.zips.aggregate( [ { $group: { _id: { state: "$state", city: "$city" }, pop: { $sum: "$pop" } } }, { $group: { _id: "$_id.state", avgCityPop: { $avg: "$pop" } } } ] )
The output should display the following:
-
The name of the database (
zips-db) -
The number of documents in the
zipscollection (29353) -
The average population for cities in each state
-
-
Exit from the
mongoshell and return to the command prompt by using the following command.exit
-