(MongoDB: Install MongoDB Community Edition on Ubuntu)
(Linode: Install MongoDB on Ubuntu 16.04 (Xenial))
(Digital Ocean: How To Install MongoDB on Ubuntu 14.04)
Before You Begin
- Check for updates
sudo apt-get update && sudo apt-get upgrade
Add the MongoDB repository
- Import the MongoDB public GPG key for package signing
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
- Add the MongoDB repository to your sources.list.d directory
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
- Update your repositories. This allows apt to read from the newly added MongoDB repo
sudo apt-get update
- Install MongoDB
sudo apt-get install mongodb-org
- Enable auto-start on reboot
sudo systemctl enable mongod.service
- Open a port in ufw
sudo ufw allow 27017
Running MongoDB
- Start MongoDB
sudo service mongod start
- Verify MongoDB started
sudo nano /var/log/mongodb/mongod.log
and look for
[initandlisten] waiting for connections on port 27017
- Stop MongoDB
sudo service mongod stop
- Restart MongoDB
sudo service mongod restart
Setup MongoDB users
- Open MongoDB
mongo --port 27017
- Create an admin database
use admin
- Add a mongodbAdminUsername and mongodbAdminPassword (letters and numbers, no funny characters)
db.createUser({user: "mongodbAdminUsername", pwd: "mongodbAdminPassword", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
- Exit
exit
- Restart MongoDB
sudo service mongod restart