• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Install, Configure, Secure MongoDB on Ubuntu 16.04 LTS

By Abhishek Ghosh November 24, 2017 8:17 am Updated on April 25, 2018

How to Install, Configure, Secure MongoDB on Ubuntu 16.04 LTS

Advertisement

MongoDB is a modern, lightweight, scalable NoSQL Database. MongoDB stores data as JSON-like documents and data structure can be changed over time. These are few points which makes MongoDB great choice for wider usage in data sciences to mobile application. Server with 512 MB RAM server is enough for basic testing purpose. Here is step by step guide on how to install, configure, secure MongoDB from SSH. For this guide, we are using Ubuntu 16.04 LTS as server operating system.

 

How to Install, Configure, Secure MongoDB

 

SSH to the intended server and become root. In case it is public server for long term use, definitely you should follow some steps like we described before in this article to secure the server.

First update, upgrade the instance :

Advertisement

---

Vim
1
2
apt update
apt upgrade

We have to add the key as next step :

Vim
1
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

You’ll get this kind of correct response :

Vim
1
2
3
4
5
6
7
8
Executing: /tmp/tmp.B8ey1ENeI1/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
0C49F3730359A14518585931BC711F9BA15703C6
gpg: requesting key A15703C6 from hkp server keyserver.ubuntu.com
gpg: key A15703C6: public key "MongoDB 3.4 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

Next, we will add the repository at /etc/apt/sources.list.d/ with one line command :

Vim
1
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

Run update :

Vim
1
apt update

Install MongoDB :

Vim
1
apt install mongodb-org

We are installing community edition of MongoDB. MongoDB repository contains the following packages:

mongodb-orgIs a metapackage that will automatically install the four component packages listed below.
mongodb-org-serverContains the mongod daemon and associated configuration and init scripts.
mongodb-org-mongosContains the mongos daemon.
mongodb-org-shellContains the mongo shell.
mongodb-org-toolsContains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.

Run these two commands to check MongoDB started, running and enable on reboot :

Vim
1
2
3
4
sudo systemctl start mongod
sudo systemctl status mongod
# press q to quit
sudo systemctl enable mongod

If we run the command :

Vim
1
mongo

It starts MongoDB shell. If we run :

Vim
1
use admin

on that interface, we will become admin with this output :

Vim
1
switched to db admin

To create the user admin with password admin123 as admin run this :

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

Run :

Vim
1
exit

to exit. Now open /lib/systemd/system/mongod.service file :

Vim
1
nano /lib/systemd/system/mongod.service

Find the keyword ExecStart=. You’ll get the default line as :

Vim
1
ExecStart=/usr/bin/mongod --config /etc/mongod.conf

Change it to :

Vim
1
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Save the file. Now, open this file :

Vim
1
nano /etc/mongod.conf

Find the keyword #security. Add this line at that place :

Vim
1
2
security:
  authorization: "enabled"

Run :

Vim
1
2
3
systemctl daemon-reload
# sudo service mongod restart
sudo systemctl restart mongod

Now, if we run the command :

Vim
1
mongo

It will not refuse. But, if you run command like show dbs, it will refuse with this kind of error :

Vim
1
2
3
4
5
6
2017-11-24T07:39:13.890+0000 E QUERY    [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :

To correctly login, we have to use this kind of command :

Vim
1
mongo -u admin -p admin123 --authenticationDatabase admin

Now if you run show dbs, you’ll get some valid response like :

Vim
1
2
admin  0.000GB
local  0.000GB

Run a cat on the settings file :

Vim
1
cat /etc/mongod.conf

You’ll notice lines like :

Vim
1
2
3
4
5
6
7
8
9
10
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
 
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

You can add IP of the current server beside 127.0.0.1 against bindIp separated with a comma. As my test server’s IP is 96.126.101.36, the config will be :

Vim
1
2
3
4
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 96.126.101.36

Run :

Vim
1
2
# sudo service mongod restart
sudo systemctl restart mongod

now, we can login with this kind of command :

Vim
1
2
mongo -u admin -p admin123 --authenticationDatabase admin --host 127.0.0.1
# mongo -u admin -p admin123 --authenticationDatabase admin --host 96.126.101.36

MongoDB supports TLS/SSL to encrypt all of MongoDB’s network traffic ensuring that MongoDB network traffic is only readable by the intended client. Normally we do not use TLS/SSL to avoid complexity.

To configure TLS/SSL go to :

Vim
1
cd /etc/ssl/

Generate a self-signed certificate with 365 days validity :

Vim
1
openssl req -newkey rsa:2048 -new -x509 -days 365 -nodes -out mongodb-cert.crt -keyout mongodb-cert.key

Above operation generates a new, self-signed certificate with no passphrase. Once you have the certificate, concatenate the certificate and private key to a .pem file:

Vim
1
cat mongodb-cert.key mongodb-cert.crt > mongodb.pem

Again, open this file we opened before :

Vim
1
nano /etc/mongod.conf

We only had :

Vim
1
2
3
4
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 96.126.101.36

We can add more settings :

Vim
1
2
3
4
5
6
net:
   port: 27017
   bindIp: 127.0.0.1, 96.126.101.36
   ssl:
      mode: requireSSL
      PEMKeyFile: /etc/ssl/mongodb.pem

How to Install, Configure, Secure MongoDB on Ubuntu 16-04 LTS

Save the file. Run :

Vim
1
2
# sudo service mongod restart
sudo systemctl restart mongod

You’ll get detailed information on SSL/TLS configuration and also commands to login :

Vim
1
https://docs.mongodb.com/manual/tutorial/configure-ssl-clients/

Tagged With dockerfile for mongo ssl from ubuntu , how to enable ssl in mongodb ubuntu , how to setup ssh for mongodb on windows , No module named utils mongodb_client ubantu
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Install, Configure, Secure MongoDB on Ubuntu 16.04 LTS

  • Install Elastic Stack on Ubuntu 16.04, CentOS 7 Single Cloud Server

    Here is How to Install Elastic Stack on Ubuntu 16.04, CentOS 7 on Single Cloud Server Instance For Server Log Analysis, Big Data Processing.

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • GNU Privacy Guard (GPG) For Secure Cloud Computing

    GNU Privacy Guard (GPG) is used for code signing in Free Software. For secure Cloud Computing, GPG can be used for Emails and Messaging.

  • Command Prompt Commands : Alphabetical list of all commands in Windows 7

    Command Prompt Commands in Windows 7 provides the user access to 180+ command line commands. Here is a list of 200 Command Prompt Commands in Windows 7.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy