How To Install MongoDB On Ubuntu

In this blog post , We will learn how to Install MongoDB on Ubuntu.

Also explained how to manage MongoDB and configure remote access.

What Is MongoDB?

MongoDB is an object-oriented, simple, dynamic, and scalable NoSQL database.

It is based on the NoSQL document store model. The data objects are stored as separate documents inside a collection — instead of storing the data into the columns and rows of a traditional relational database.

Installing MongoDB On Ubuntu

Ubuntu’s official software package repositories comes with the latest version of MongoDB , and MongoDB can be installed easily using APT package manager.

Lets update the system software packages list to have the latest version of repositories.

sudo apt-get update

Now We can directly install the mongodb package which will install along with the mongodb dependencies.

sudo apt-get install mongodb -y

The mongoDB packages are installed on the ubuntu system.

Lets start the mongodb service using the below command.

sudo systemctl start mongodb

To check the status of the mongoDB,

sudo systemctl status mongodb

By default , MongoDB process listens on the port 27017.To check,

sudo netstat -nltp |grep mongo

Lets enable the mongoDB service to start automatically on system boot,

sudo systemctl enable mongodb

As the mongoDB comes with the systemd service , We can easily manage it using systemd commands as shown below.

The systemd service file is located under /lib/systemd/system/ as mongodb.service file

cat /lib/systemd/system/mongodb.service

To manage the mongoDB service on the ubuntu system , The commands are as follows.

To stop the mongoDB service,

sudo systemctl stop mongodb

To start the mongoDB service,

sudo systemctl start mongodb

To restart the mongoDB service,

sudo systemctl restart mongodb

To disable mongoDB service on system startup,

sudo systemctl disable mongodb

Configuring Remote Access

By default , MongoDB will accept connections only from localhost and listens on the port 27017.

To accept connections from the remote servers , We need to configure mongoDB and allow port 27017 port on the firewall.

To allow remote access from anywhere,

sudo ufw allow 27107

To allow the remote connections from anywhere or for the particular IP address , We need to allow the IP address in the /etc/mongodb.conf configuration file.

Open the configuration file,

sudo nano /etc/mongodb.conf

You can find a config named , bind_ip , Allow required IP address here as shown below.

The above config will allow connections from the localhost and the IP address we have allowed.Any incoming requests other than this will be rejected.

To allow connections from any IP address,

Save and Close the file.

We need to restart the mongoDB service for the changes to take effect.

sudo systemctl restart mongodb

Now If you do netstat -nltp and check , The mongoDB will be listening on port 27017 (as usual) but will accept connections from anywhere.

sudo netstat -nltp |grep mongo

Connecting MongoDB

By default , MongoDB can be accessed without any authentication.

To launch the mongo shell , run the below command.

mongo

From the mongo shell, To list all the available database,

show dbs

To manage users , roles and database access such as granting or revoking database access to the users , We need to create a administrator (root user) in the admin database.

To connect to admin database,

use admin

Run the below command to create root user with admin privileges in the admin database.

db.createUser({user:"root", pwd:"sha((^%*&i(Qu", roles:[{role:"root", db:"admin"}]})

We have successfully created a root user who can manage other users and databases.

But To be able to login as admin user with authentication , We need to enable authentication on the mongoDB service which is not activated by default.

We know that the mongoDB service post installation will create systemd service file , but without –auth parameter.

Lets go ahead and configure –auth parameter on the /lib/systemd/system/mongodb.service file.

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

Under the [Service] section , You can find a parameter ExecStart

We need to change this line

From

ExecStart=/usr/bin/mongod --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS

To

ExecStart=/usr/bin/mongod --auth --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS

Once added , Save and Close the file.

We need to run the following commands for the changes to take effect.

sudo systemctl daemon-reload
sudo systemctl restart mongodb

If you check the status , You can see that the –auth is enabled.

sudo systemctl status mongodb

Lets connect to mongoDB admin database as the root user

sudo mongo -uroot -p --authenticationDatabase "admin"

You will prompted for the password , Enter the password and hit Enter.

You should be successfully logged in as root user with authentication.

We have successfully learnt to install mongoDB on the Ubuntu operating systems.

Also we know how to allow or restrict remote access for the mongoDB and enabled authentication.

Hope you find it helpful.

Please do check out my other articles.