How To Install MongoDB 3.4 on Ubuntu 16.04
In this tutorial we’ll learn how to install MongoDB 3.4 on Ubuntu 16.04. Managing MongoDB service, creating root user and uninstall MongoDB also explained.
Categories:
Introduction
In this guide, we will learn how to install MongoDB 3.4 on Ubuntu 16.04 LTS (Xenial Xerus). We Will also learn how to configure and secure our MongoDB installation,
What is mongoDB? from [MongoDB website][1]:
MongoDB is a document database with scalability and flexibility that you want with query and indexing that you need.
By following this tutorial you should be able to install MongoDB 3.4, configure and secure MongoDB installation on Ubuntu 16.04
Prerequisites
- Ubuntu 16.04 LTS (Xenial Xerus) with sudo access
Add MongoDB Repository
First of all, let’s add the MongoDB public key. This key is used by package management tool like apt
to ensure the consistency and authenticity of the package.
<span class="token function">sudo</span> apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
```
The output should be similar to the text below
Executing: /tmp/tmp.XZgiMpeM0c/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 <span class="token string">"MongoDB 3.4 Release Signing Key <packaging@mongodb.com>"</span> imported
gpg: Total number processed: 1
gpg: imported: 1 <span class="token punctuation">(</span>RSA: 1<span class="token punctuation">)</span>
<span class="token operator"><</span>/packaging@mongodb.com<span class="token operator">></span>
```
Create new file `/etc/apt/sources.list.d/mongodb-org-3.4.list` that contain mongodb 3.4 repository info using command below
<span class="token keyword">echo</span> <span class="token string">"deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse"</span> <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token function">tee</span> /etc/apt/sources.list.d/mongodb-org-3.4.list
```
Reload package database using the command below
<span class="token function">sudo</span> <span class="token function">apt-get</span> update
```
## Install MongoDB 3.4
To install the latest stable version of MongoDB 3.4 you can use the command below. At the time of this writing, the latest stable version of MongoDB 3.4 is 3.4.18.
<span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> mongodb-org
```
To install a previous stable version of MongoDB 3.4 you need to specify a version for each package. For example, if you need to install MongoDB 3.4.15 you can use the command below.
<span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> -y \
mongodb-org<span class="token operator">=</span>3.4.15 \
mongodb-org-server<span class="token operator">=</span>3.4.15 \
mongodb-org-shell<span class="token operator">=</span>3.4.15 \
mongodb-org-mongos<span class="token operator">=</span>3.4.15 \
mongodb-org-tools<span class="token operator">=</span>3.4.15
```
## Managing MongoDB Service
Now MongoDB installed let's check MongoDB service using command below
sudo service mongod status
[][2]
or we can use command below
<span class="token function">sudo</span> systemctl status mongod
```
We will get output similar to below which inform that mongod is not running.
[][3]
To start MongoDB service we can use command below:
<span class="token function">sudo</span> <span class="token function">service</span> mongod start
```
or we can also use systemctl to start MongoDB service
<span class="token function">sudo</span> systemctl start mongod
```
We will see output similar to output below
[][4]
MongoDB service already started but it's not enabled by default by seeing this line
Loaded: loaded <span class="token punctuation">(</span>/lib/systemd/system/mongod.service<span class="token punctuation">;</span> disabled<span class="token punctuation">;</span> vendor preset: enabled<span class="token punctuation">)</span>
```
Let's enable MongoDB service on boot by running
<span class="token function">sudo</span> systemctl <span class="token function">enable</span> mongodb
```
Now if we check MongoDB service status we will see that the service is enabled.
[][5]
## Checking MongoDB Service
Beside using `service` or `systemctl` command, we can check MongoDB service status using several tools.
To check where MongoDB service listening to we can use `netstat`
<span class="token function">sudo</span> <span class="token function">netstat</span> -naptu <span class="token operator">|</span> <span class="token function">grep</span> 27017
```
[][6]
We grep MongoDB default port `27017`.
As alternative we can also grep `mongod` application name
<span class="token function">sudo</span> <span class="token function">netstat</span> -naptu <span class="token operator">|</span> <span class="token function">grep</span> mongod
```
We can also use `ss` to do similar check like `netstat`
ss -at <span class="token operator">|</span> <span class="token function">grep</span> 27017
```
[][7]
We can check MongoDB service process using command below:
<span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> -m1 mongod
```
[][8]
For more detail resource usage of our MongoDB process we can use top and only show process run by MongoDB user.
top -u mongodb
[][9]
## Creating root and admin users
MongoDB user management is different compared to RDBMS user management like MySQL or PostgreSQL.
In MongoDB, the user is managed per database. If you want to create an administrative user you need to create a user in `admin` database.
Connect to MongoDB using mongo client
mongo
Use command below to create user root with root role. Don't forget to change the password.
db.createUser<span class="token punctuation">(</span><span class="token punctuation">{</span>user:<span class="token string">"root"</span>, pwd:<span class="token string">"changemeplease123123123"</span>, roles:<span class="token punctuation">[</span><span class="token punctuation">{</span>role:<span class="token string">"root"</span>, db:<span class="token string">"admin"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
```
To generate random string for password on command line you can use comand below
uuidgen <span class="token operator">|</span> sha256sum <span class="token operator">|</span> <span class="token function">awk</span> <span class="token punctuation">{</span><span class="token string">'print <span class="token variable">$1</span>'</span><span class="token punctuation">}</span>
```
or
uuidgen <span class="token operator">|</span> sha256sum <span class="token operator">|</span> <span class="token function">cut</span> -d <span class="token string">' '</span> -f 1
```
## Enabling Authentication
There are two ways to enable MongoDB authentication, by updating systemd service file or updating mongodb.conf file.
I recommend using the second method since mongodb service file might be overwrite by apt when we upgrade mongodb package.
### Updating mongod.service file
Open `/lib/systemd/system/mongod.service` file.
Find line
ExecStart<span class="token operator">=</span>/usr/bin/mongod --config /etc/mongod.conf
```
Replace the line with
ExecStart<span class="token operator">=</span>/usr/bin/mongod --auth --config /etc/mongod.conf
```
Reload systemd daemons
<span class="token function">sudo</span> systemctl daemon-reload
```
Restart MongoDB service.
<span class="token function">sudo</span> systemctl restart mongod
```
### Updating mongodb.conf file
Open ``/etc/mongodb.conf file
Find line
<span class="token comment">#security:</span>
```
Replace it with
security:
authorization: enabled
```
Restart MongoDB service to enable authentication
<span class="token function">sudo</span> <span class="token function">service</span> mongod restart
```
After enabling authentication we can connect using root user that we just created on previous step.
mongo -uadmin admin -p
```
## Uninstall MongoDB 3.4
In this section we'll learn how to uninstall MongoDB 3.4 from Ubuntu 16.04. Please be really careful when running command on this section.
Before we uninstall MongoDB 3.4 we need to stop MongoDB service first.
<span class="token function">sudo</span> <span class="token function">service</span> mongodb stop
```
To uninstall MongoDB 3.4 we can use command below
sudo apt-get purge mongodb-org*
Command above only remove MongoDB packages.
To remove MongoDB log directory use command below
<span class="token function">sudo</span> <span class="token function">rm</span> -r /var/log/mongodb
```
To remove MongoDB data directory use command below
WARNING : command below will remove your data and cannot be restored. be very very very careful when you're running command below.
<span class="token function">sudo</span> <span class="token function">rm</span> -r /var/lib/mongodb
```
## MongoDB 3.4 References
You can find references related to MongoDB 3.4 below
* [MongoDB 3.4 Manual][10]
* [MongoDB 3.4 Release Notes][11]
* [MongoDB 3.4 FAQ][12]
* [Configuration file options][13]
* [MongoDB 3.4 What's New (pdf)][14]
## Conclusion
In this article, We learned to install MongoDB 3.4 on Ubuntu 16.04 LTS (Xenial Xerus). We also learned how to manage MongoDB service, check MongoDB service status using multiple tools, create root user and also enable authentication.
We also learned how to uninstall MongoDB 3.4 from Ubuntu 16.04.
Now you can start building your application using MongoDB as a database.
[1]: https://www.mongodb.com/what-is-mongodb
[2]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-1.png
[3]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-2.png
[4]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-3.png
[5]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-4.png
[6]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-5.png
[7]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-6.png
[8]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-7.png
[9]: https://www.howtodojo.com/wp-content/uploads/2020/01/install-mongodb-3.4-ubuntu-16.04-8.png
[10]: https://docs.mongodb.com/v3.4/
[11]: https://docs.mongodb.com/v3.4/release-notes/3.4/
[12]: https://docs.mongodb.com/v3.4/faq/
[13]: https://docs.mongodb.com/v3.4/reference/configuration-options/
[14]: https://webassets.mongodb.com/mongodb_whats_new_3.4.pdf