How to install MongoDB on Ubuntu 16.04
MongoDB is a free, open source NoSQL document-oriented database known for its high performance. MongoDB is written in C ++ and stores its data in JSON format called BSON or Binary JSON. It was created in 2009 and is currently being developed by MongoDB Inc.
In this tutorial, we will install MongoDB on Ubuntu 16.04.
Requirements:
- Server on Ubuntu 16.04 – 64 bit
- Root or non-root sudo user
Note: Run all commands in this tutorial without sudo if you are logged in as root.
Adding the official MongoDB repository
To ensure the validity of Ubuntu packages, they need to be signed with GPG keys. Let’s start by importing the GPG keys that we need for the official MongoDB repository:
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Then add MongoDB repository to /etc/apt/sources.list.d with this command:
# 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
And then run the update command, so Ubuntu can read the packages from the repository being added:
# sudo apt-get update
Installing MongoDB
In order to start installing MongoDB from the repository we just added, we will run the command:
# sudo apt-get install -y mongodb-org
Although MongoDB repository now provides a block of files in a package, we left this part of the article for educational purposes, it can be used to install other services.
Now we need to create a Systemd file for MongoDB. First, let’s briefly explain what files are in Systemd. Unit files store information about services, sockets, devices, basically any resource managed by Systemd, which is the init system and used by most Linux distributions.
Create a file in the / etc / systemd / system / directory using nano:
# sudo nano /etc/systemd/system/mongodb.service
Paste the following text below:
[Unit] Description=High-performance, schema-free document-oriented database After=network.target Documentation=https://docs.mongodb.org/manual [Service] User=mongodb Group=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target
Make sure to save (press Ctrl + O) and close the file (press Ctrl + X). Now we have to update Systemd and enable our newly created service and start the service:
# sudo systemctl daemon-reload # sudo systemctl enable mongod # sudo systemctl start mongod
Check that the service is running:
# systemctl status mongod
The output should look like this:
● mongodb.service - High-performance, schema-free document-oriented database Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2017-07-13 18:21:23 CDT; 7s ago Docs: https://docs.mongodb.org/manual Main PID: 3977 (mongod) CGroup: /system.slice/mongodb.service └─3977 /usr/bin/mongod --quiet --config /etc/mongod.conf Jul 13 18:21:23 test systemd[1]: Started High-performance, schema-free document-oriented database.
Setting up MongoDB as administrator
To set up an admin username and password in MongoDB, you first need to open a MongoDB shell, type:
# mongo
Inside the MongoDB shell, enter the following command to navigate to the admin database:
> use admin
Now let’s create an administrator name and set a password for the user:
> db.createUser({user:"admin", pwd:"admin54321-", roles:[{role:"root", db:"admin"}]})
Note: You can replace the PWD value with your own password, like: PWD: ”MyPassword”. The output from the above commands should look like this:
Successfully added user: { "user" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] }
Enter this command in a shell to exit the shell:
> exit
Enabling MongoDB Authentication
Open /lib/systemd/system/mongod.service with nano or any editor you like:
# sudo nano /lib/systemd/system/mongod.service
On the ExecStart line add a new -auth argument, the line should look like this:
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
Make sure to save (press Ctrl + O) and close the file (press Ctrl + X). Restart Systemd to enable the new version of our modified service file:
# sudo systemctl daemon-reload
Then restart MongoDB for the changes to take effect:
# sudo systemctl restart mongod
Now connect to MongoDB shell with this command:
# mongo -u admin -p --authenticationDatabase admin
You will be prompted for a password, enter the password set above. Once you have verified inside the shell by making the administrator authentication we created, run this command:
> db.runCommand({connectionStatus : 1})
The output should look like this:
{ "authInfo" : { "authenticatedUsers" : [ { "user" : "admin", "db" : "admin" } ], "authenticatedUserRoles" : [ { "role" : "root", "db" : "admin" } ] }, "ok" : 1 }
That’s it, you have successfully installed MongoDB on Ubuntu 16.04.