We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsOperating SystemsLinux

How to Install and Configure MongoDB on Ubuntu 16.04

Ubuntu VPS
KVM VPS
Windows Cloud Server
Brazil VPS
Linux KVM Hosting
USA VPS
Linux Cloud Server
OpenVZ VPS
Germany Cloud Servers
cPanel KVM

MongoDB is a NoSQL database that offers a high performance, high availability, and automatic scaling enterprise database. MongoDB is a NoSQL database, so you can’t use SQL (Structured Query Language) to insert and retrieve data, and it does not store data in tables like MySQL or Postgres. Data is stored in a “document” structure in JSON format (in MongoDB called BSON). MongoDB was first introduced in 2009 and is currently developed by the company MongoDB Inc.

Follow These Steps to Install and Configure MongoDB on Ubuntu 16.04

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

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

Update the repository with the apt command:

sudo apt-get update

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

Paste script below:

[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 --config /etc/mongod.conf
 
[Install]
WantedBy=multi-user.target

Save the file and exit.

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

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

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

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

use admin

Create the root user with this command:

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

Now type exit to exit from MongoDB shell.

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

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 should be able to connect to the database.

OpenVZ VPS
Germany Cloud Servers
Brazil VPS
Linux KVM Hosting
cPanel KVM
USA VPS
Linux Cloud Server
KVM VPS
Ubuntu VPS
Windows Cloud Server
Rate this Tutorial:
No Comments Leave a Comment
Leave a Comment

Other (3)

Popular Keywords