Categories
Tutorials

How to install MongoDB on Ubuntu 16.04

Hey all

I haven’t found any rock solid guides for doing so, so I thought I’d make my own. Follow these steps and I will explain as best as I can.

 

To start off let’s install some dependencies to make sure everything will work (sort of mundane but just do it for the memes).

sudo apt-get install sudo nano -y

 

Now, we need to add MongoDB to our apt.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

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

 

Next, we update and install it.

sudo apt-get update 
sudo apt-get install mongodb-org -y

 

To make sure Mongo can work properly we need a systemd service setup.

sudo nano /etc/systemd/system/mongodb.service

 

And in that file paste the following

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

 

Okay, now we need to start MongoDB like so.

sudo systemctl start mongod

sudo systemctl enable mongod

and you should see something like this

Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service
to /lib/systemd/system/mongod.service.

 

Now we need to add an admin user and set the password, replace admin123 with your own password.

mongo

use admin

db.createUser({user:"admin", pwd:"admin123", roles:[{role:"root", db:"admin"}]})

 

This step is optional, but if you want to enable security and allow database access from remote locations do this.

nano /etc/mongod.conf

 

And edit the following lines (see bold parts):

net:
port: 27017
bindIp: 0.0.0.0

 

#security:
   authorization: "enabled"

 

 

And that’s it! Now restart Mongo for good measures and you’re done!

sudo systemctl restart mongod