Skip to main content

How to Install and Configure MongoDB on Ubuntu 16.04


What we will do in this tutorial:
  1. Install MongoDB
  2. Configure MongoDB
  3. Conclusion
What we will do in this tutorial:
  1. Install MongoDB
  2. Configure MongoDB
  3. Conclusion

Install MongoDB on Ubuntu 16.04

Step 1 - Importing the Public Key

GPG keys of the software distributor are required by the Ubuntu package manager apt (Advanced Package Tool) to ensure package consistency and authenticity. Run this command to import MongoDB keys to your server.

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

Step 2 - Create source list file MongoDB

Create a MongoDB list file in /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

Step 3 - Update the repository

update the repository with the apt command:
sudo apt-get update

Step 4 - Install MongoDB

Now you can install MongoDB by typing this command:
sudo apt-get install -y mongodb-org
We have to create a new mongodb systemd service file in the '/lib/systemd/system' directory. Go to that directory and create the new mongodb service file 'mongod.service' with vim.
cd /lib/systemd/system/
vim mongod.service

Now update the systemd service with command below:
systemctl daemon-reload

Start mongodb and add it as service to be started at boot time:
systemctl start mongod
systemctl enable mongod
Now check that mongodb has been started on port 27017 with the netstat command.
netstat -plntu
Check if MongoDB is running.

Configure MongoDB username and password

When the MongoDB packages are installed you can configure username and password for the database server:

Step 1 - Open mongo shell

Before you set up a username and password for MongoDB, you need to open the mongodb shell on your server. You can login by typing:
mongo
If you get error Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly, try the command:
export LC_ALL=C
mongo

Step 2 - Switch to the database admin

Once you`re in the MongoDB shell, switch to the database named admin:
use admin

Step 3 - Create the root user

Create the root user with this command :
db.createUser({user:"admin", pwd:"admin123", roles:[{role:"root", db:"admin"}]})
Desc: Create user admin with password admin123 and have the permission/role as root and the database is admin.
Create a admin user in MongoDB
Now type exit to exit from MongoDB shell.

Step 4 - Enable mongodb authentication

Edit the mongodb service file '/lib/systemd/system/mongod.service' with your editor.
vim /lib/systemd/system/mongod.service
On the 'ExecStart' line 9, add the new option '--auth'.
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
Save and exit.
Reload the systemd service:
systemd daemon-reload

Step 5 - Restart MongoDB and try to connect

Now restart MongoDB and connect with the user created.
sudo service mongod restart
and connect to the mongodb shell with this command:
mongo -u admin -p admin123 --authenticationDatabase admin
and you will see the output like this:
Test MongoDB Authentication.

Conclusion

A well-known NoSQL database that offers high performance, high availability, and automatic scaling is MongoDB. It is different from the RDBMS such as MySQL, PostgreSQL, and SQLite as it does not use SQL to set and retrieve data. MongoDB stores data in `documents` called BSON (binary representation of JSON with additional types of information). MongoDB is only available for 64-bit Long Term Support Ubuntu Release.

Comments

Popular posts from this blog

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly. Let’s discuss about anomalies first then we will discuss normal forms with examples. Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Rick Delhi D001 101 Rick Delhi D002 123 Maggie Agra D890 166 Glenn Chennai D900 166 Glenn Chennai D004 The above table ...

Downloading Cassandra in Ubuntu 16.04

Installation from Debian packages For the <release series> specify the major version number, without dot, and with an appended x . The latest <release series> is 311x . For older releases, the <release series> can be one of 30x , 22x , or 21x . Add the Apache repository of Cassandra to /etc/apt/sources.list.d/cassandra.sources.list , for example for the latest 3.11 version: echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list Add the Apache Cassandra repository keys: curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add - Update the repositories: sudo apt-get update If you encounter this error: GPG error: http://www.apache.org 311x InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY A278B781FE4B2BDA Then add the public key A278B781FE4B2BDA as follows: sudo apt-key adv --keyser...