Setting up MongoDB on Ubuntu 16.04
MongoDB is an open-source, document database designed for ease of development and scaling.
Introduction
In this post we will learn how to setup MongoDB on Ubuntu Server. If you are looking for instructions on how to setup MongoDB on Windows check out my previous post on the topic. At the end of this post, we would have accomplished the following objectives:
- Setup and configured MongoDB as a service on Ubuntu
- Accessed MongoDB remotely using ufw
Adding the Repository
To follow this tutorial you need:
- Ubuntu 16.04 LTS
Ubuntu includes its own MongoDB Community Edition packages, however they are mostly not up-to-date. For this tutorial we will be using
the official MongoDB Community Edition packages which are generally more up-to-date.
MongoDB only provides packages for 64-bit LTS
(long-term support) Ubuntu releases.
Ubuntu ensures the authenticity of software packages by verifying that they are signed with GPG keys, so we first have to import the key for the official MongoDB repository:
Next we will create a list file for MongoDB. Create the /etc/apt/sources.list.d/mongodb-org-3.4.list
list file using the following command:
We will now reload the local package database:
Installing the MongoDB packages
Now that we got the boring stuff out of the way, let us install MongoDB. Issue the following command to install the latest stable version of MongoDB:
The MongoDB instance stores its data files in /var/lib/mongodb
and its log files in /var/log/mongodb
by default, and runs using the
mongodb
user account. You can specify alternate log and data file directories in /etc/mongod.conf
.
If you change the user that runs the MongoDB process, you must modify the access control rights to the /var/lib/mongodb
and
/var/log/mongodb
directories to give this user access to these directories.
Start MongoDB by issuing the following command:
Verify that the mongod process has started successfully by checking the contents of the log file at
/var/log/mongodb/mongod.log
:
Look for a line reading:
where <port>
is the port configured in /etc/mongod.conf
, 27017
by default.
Stopping and Restarting MongoDB
You can stop the mongod process by issuing the following command:
In case MongoDB is running and you want to reload configuration, you restart the mongod process with:
The status of the mongod process can checked with the following command:
To enable start of MongoDB when the system starts, run the following command:
Adjusting the Firewall
In order to access our MongoDB server from a remote system, we need to modify our firewall rules. Ubuntu 16.04 servers can use the
UFW firewall to make sure only connections to certain services are allowed. We can set up a basic firewall
very easily using this application.
First we list all applications registered with UFW:
We need to make sure that the firewall allows SSH connections so that we can log back in next time. We can allow these connections by typing:
Afterwards, we can enable the firewall by typing:
Type "y"
and press ENTER
to proceed. You can see that SSH connections are still allowed by typing:
With this out of the way let us allow port 27017
over the firewall. MongoDB default listening port is 27017
:
You can verify the change in firewall settings with sudo ufw status
.
You should see traffic to 27017
port allowed in the output. If you have decided to allow only a certain IP address to connect to
MongoDB server, the IP address of the allowed location will be listed instead of Anywhere in the output.
The final bit of configuration is to allow remote connections to MongoDB:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# 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
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
On line 24
just below port: 27017
comment out bindIp: 127.0.0.1
.
NOTE
It may be a different line number on your system.
Run sudo service mongod restart
to activate the configuration changes.
Conclusion
In this blog post, we learned how to setup MongoDB as a service on Ubuntu. We also learned how to enable MongoDB through network firewall.
I enjoyed writing this post, I hope you did too and until the next post, keep doing cool things .