Skip to content
Rashid Azar

Install MongoDB on Ubuntu 16.04

Linux1 min read

To ensure the credibility of the packages Ubuntu makes sure they are signed with GPG keys. Let’s begin by importing the GPG keys we need for the official MongoDB repository:

1$ sudo apt-key adv --keyserver hkp:// --recv EA312927

Next add the MongoDB repository in /etc/apt/sources.list.d using this command:

1$ echo "deb "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Installing MongoDB

1$ sudo apt-get update
2$ sudo apt-get install -y mongodb-org

Create service file for MongoDB

1$ sudo vim /etc/systemd/system/mongodb.service

Paste following code:

2Description=High-performance, schema-free document-oriented database
9ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

Now we have to update systemd to include our newly created service and we enable and start the service:

1$ sudo systemctl daemon-reload
2$ sudo systemctl enable mongod
3$ sudo systemctl start mongod

Create admin user in MongoDB:

1$ mongo
2> use admin
3> db.createUser({user:"admin", pwd:"admin54321", roles:[{role:"root", db:"admin"}]})
4> exit

Enable MongoDB authentication

Open file /etc/systemd/system/mongodb.service and update following line:

1ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

After that reload configurations:

1$ sudo systemctl daemon-reload
2$ sudo systemctl restart mongod

Now connect to the MongoDB shell using this command:

1$ mongo -u admin -p --authenticationDatabase admin
© 2021 by Rashid Azar. All rights reserved.