• 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-org Is a metapackage that will automatically install the four component packages listed below.
mongodb-org-server Contains the mongod daemon and associated configuration and init scripts.
mongodb-org-mongos Contains the mongos daemon.
mongodb-org-shell Contains the mongo shell.
mongodb-org-tools Contains 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

This Article Has Been Shared 351 Times!

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

  • WordPress XML-RPC Attack & Fake PHP5-FPM Error

    WordPress XML-RPC Attack Can Bring DDoS Resulting in Random 502 PHP5-FPM Errors on Nginx Server or Can Make the Database Down. Here is Fix.

  • Setup SFTP Without Shell Access For WordPress, Cloud Server

    Often We Need To Host Account On VPS OR Cloud Server Where It Is Desired To Have A Setup SFTP Without Shell Access For WordPress Like cPanel.

  • Create Data Science Environment on Cloud Server With Docker

    Here Are the Steps, Commands to Create Data Science Environment on Cloud Server For Data Analysis Starting With a Blank Server With SSH.

  • How to Install and Set Up LXD on Ubuntu 16.04

    Here is How to Install and Set Up LXD on Ubuntu 16.04. LXD Works With a Directory Based Storage Backend. LXD is lxc with strong security.

  • HP Will Not Sell Cheap Cloud Servers To Amazon Like Companies

    Hewlett Packard Enterprise Will Not Sell Cheap Cloud Servers To Amazon Like Companies Who Order Building Servers at China To Save Dual Loss.

Additionally, 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

  • What is an Automatic Ethanol Fireplace February 8, 2023
  • Disadvantages of Cloud-Native Computing February 7, 2023
  • Projector Screen Basics February 6, 2023
  • What is Configuration Management February 5, 2023
  • What is ChatGPT? February 3, 2023

About This Article

Cite this article as: Abhishek Ghosh, "How to Install, Configure, Secure MongoDB on Ubuntu 16.04 LTS," in The Customize Windows, November 24, 2017, February 9, 2023, https://thecustomizewindows.com/2017/11/how-to-install-configure-secure-mongodb-on-ubuntu-16-04-lts/.

Source:The Customize Windows, JiMA.in

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT