MongoDB is a free & open-source No-SQL database, it uses JSON like document format to store data in the database which is unlike the SQL database, where data is stored in a row & column-based format.
MongoDB is very fast & has great performance when compared to the SQL database. MongoDB databases are very easy to scale & they also address various shortcomings that other SQL databased present. MongoDB is completely ideal for the types of DB required to handle the data that we have today i.e. big data, IoT, online gaming, etc.
Recommended Read: Important PostgreSQL commands that every beginner should know
Also Read: Complete guide to install MongoDB on CentOS/RHEL
In this tutorial, we will learn to install MongoDB on Ubuntu 16.04. There are two methods with which we can install MongoDB on Ubuntu 16.04,
1- Install using apt manager,
2- Install using tar packages.
1- Install MongoDB on Ubuntu 16.04 using apt
We need to first make sure that “gnupg” package is installed on the system or not, cause we will receive an error while importing the public keys for mongodb if it’s not installed. Install it with the following command,
$ sudo apt-get install gnupg
Next, we will import the public keys for MongoDB,
$ wget -qO – https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add –
Next, we will create the list file for MongoDB with the following command,
$ echo “deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
Now reload the repositories for packages,
$ sudo apt-get update
Now to install MongoDB on Ubuntu 16.04, execute
$ sudo apt-get install -y mongodb-org
Or to install any specific versions, use the command,
$ sudo apt-get install -y mongodb-org=4.2.2 mongodb-org-server=4.2.2 mongodb-org-shell=4.2.2 mongodb-org-mongos=4.2.2 mongodb-org-tools=4.2.2
2- Install MongoDB on Ubuntu 16.04 using tar packages
Before we can download & install mongodb using tar packages. We must have some packages installed on our servers,
$ sudo apt-get install libcurl3 openssl
We will create a directory for keeping the mongodb packages,
$ mkdir -p /data/mongodb
Next, we have to download the package using the following command,
$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.2.tgz
Next, we need to extract the file to the created directory,
$ tar -xvzf /mongodb-linux-x86_64-ubuntu1604-4.2.2.tgz -C /data/mongodb
Next, we will create symbolic links to the binaries directory,
$ sudo ln -s /data/mongodb/bin/* /usr/local/bin/
Now, we need to create a data directory & log directory as well,
$ sudo mkdir -p /var/lib/mongo
$ sudo mkdir -p /var/log/mongo
Next, we have change ownership of these folders to mongod,
$ sudo chown -R mongod:mongod /var/lib/mongo
$ sudo chown -R mongod:mongod /var/log/mongo
Note: When installing using apt package manager, all these folders are created by the system only with required permissions.
Starting & connecting MongoDB
Once we have installed mongodb, we can start the database with the following command,
$ sudo systemctl start mongod
Also, we can use the following command to start db,
$ mongod –dbpath /var/lib/mongo –logpath /var/log/mongodb/mongod.log –fork
Now to connect to the database,
$ mongo
Connecting database from remote systems
By default, we can only connect the database from the localhost & to be able to connect the database from other systems, we need to make configuration in mongodb configuration file,
$ sudo vi /etc/mongod.conf
& look for “net.bindIp”, then change it the following
net.bindIp 0.0.0.0
You can also change & mention a single remote IP address if you only need to connect from a single remote system. After making the changes, you need to restart the database to implement the changes made,
$ sudo systemctl stop mongod
$ sudo systemctl start mongod
Note:- If running database through CLI, we can mention the parameter “–bind_ip” followed by ‘0.0.0.0’ for connecting from all remote IPs or we can also mention a single IP address.
That’s it guys, we now end this tutorial on how to install MongoDB on 16.04. Please do send us your questions, queries & suggestions using the comment box below.