This is a guide to installing, configuring and using MongoDB on localhost
-
Import public key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
-
Create source list file
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
-
Install
sudo apt-get update sudo apt-get install -y mongodb-org
-
Start mongod daemon and add it as service to be started at boot time
systemctl start mongod systemctl enable mongod
-
Check that mongo server is running on port 27017
netstat -plntu
You should see a line that reads:
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2745/mongod
-
Open mongo shell
mongo
-
Add root user to the admin db
use admin db.createUser({user:"admin", pwd:"your_pwd_here", roles:[{role:"root", db:"admin"}]})
sudo service mongod restart
mongo -u admin -p your_pwd_here --authenticationDatabase admin
use your_db_name
Note: immediately after this cmd, if you exec 'show dbs' you will not see the new db. Only when data is inserted into it will Mongodb consider the database to really exist.
Refer to this cheat sheet