MongoDB Installation
Statinfer
Tested on Ubuntu 14.04 LTS
MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL and Microsoft SQL. MongoDB features include: full index support, replication, high availability and auto-sharding. To install MongoDB on your system, open a terminal and follow the below steps:
Step 1: Setup a the Package Database
First you have to import the MongoDB public key used by the package management system:
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Then you have to create a list file for MongoDB:
$ echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Now reload the package database:
$ sudo apt-get update
Step 2: Install Latest Stable Version MongoDB
If there is warning in the command $ sudo apt-get install -y mongodb-org then you need not to worry and do continue to install it.
$ sudo apt-get install -y mongodb-org
$ sudo apt-get install -y --force-yes mongodb-org
The image given below is the output of the above commands:
Step 3: Get MongoDB Running
Start-Up MongoDB
$ sudo service mongod start
Check MongoDB Service Status
$ sudo service mongod status
Summary List of Status Statistics (Continuous) as shown in the image below.
$ mongostat
![](Image/mongo_stat.png)
Summary List of Status Statistics (5 Rows, Summarized Every 2 Seconds)
$ mongostat --rowcount 5 2
As shown in the below image.
Enter the MongoDB Command Line
$ mongo
By running this command MongoDB server will start to listen on port 27017 on the localhost interface.
Shutdown MongoDB
$ sudo service mongod stop
Restart MongoDB
$ sudo service mongod restart
Step 4: Enable MongoDB Authentication
$ sudo nano /lib/systemd/system/mongod.service
Add the following lines:
[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 --auth --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
Use Ctrl+X and Y to save.
Restart the mongod service and login with authentication:
$ sudo service mongod restart
$ mongo localhost:27017 -u admin -p admin123 --authenticationDatabase admin
If you get an error as shown in the image given below:
Do the following to get rid of this error:
$ mongo localhost:27017
> use admin
switched to db admin
> db.auth("admin","admin123");
1
> exit;
You can take help from the image given below:
Again, restart the mongo service and login with authentication:
$ sudo service mongod restart
$ mongo localhost:27017 -u admin -p admin123 --authenticationDatabase admin
If everything is set right, then you will get the screen as the image given below:
If the authentication error is still there, then go to step number 5 or else you can skip to step number 6.
Step 5:
If above error of authentication is not resolved which is shown in the image below .
Then do the following steps:
$ mongo localhost:27017
> use admin
> db.createUser(
{
user: "admin",
pwd: "admin123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
> exit;
Then exit the mongo shell and restart it by using following commands.
$ sudo service mongod restart
To authenticate after connecting, use the following commands:
$ mongo localhost:27017
> use admin
switched to db admin
> db.auth("admin","admin123");
1
> exit;
This below command will “authenticate during connection”.
$ mongo localhost:27017 -u admin -p admin123 --authenticationDatabase admin
If everything is set right, then you will get the screen as the image given below:
Step 6:
To check the database
> show dbs
It will show the below outputs:
admin 0.078GB
local 0.078GB
test 0.078GB
>