Introduction
MongoDB is a database application used in web development that provides high performance, high availability, and automatic scaling.
Traditional databases like MySQL are relational while MongoDB stores data as JSON documents. This is especially useful for storing complex data, or modifying documents without affecting existing data structures.
This guide will show you install MongoDB on Ubuntu 18.04.
Prerequisites
- A system running 64-bit Ubuntu 18.04
- A user account on Ubuntu with sudo privileges
- Terminal window/command line access
Install MongoDB from Default Ubuntu Repositories (Easy)
Installing MongoDB
Start by updating and refreshing the package lists:
sudo apt-get update
sudo apt-get upgrade
Allow the process to finish and then install the MongoDB application:
sudo apt-get install mongodb
The system prompts you to confirm the installation by typing y.
You can verify the installation by checking the version:
mongod ––version
In this example the MongoDB version installed from the repositories is v3.6.3.
Check to make sure the MongoDB service is running and enabled at startup:
sudo systemctl status mongodb
The output shown in the image below, confirms that the service is active.
Manage MongoDB Service
Use these commands to manage the MongoDB service:
To stop MongoDB:
sudo systemctl stop mongodb
To start MongoDB:
sudo systemctl start mongodb
To restart MongoDB when it’s already running (for example, to apply configuration changes):
sudo systemctl restart mongodb
To prevent MongoDB from launching at boot:
sudo systemctl disable mongodb
To set MongoDB to launch at boot:
sudo systemctl enable mongodB
How to Use the MongoDB Shell
To launch the MongoDB shell, enter:
mongo
To exit the Mongo shell, use either the keystroke:
Ctrl-C
or the command:
quit()
How to Uninstall MongoDB
To uninstall MongoDB, enter the commands:
sudo systemctl stop mongodb
sudo apt purge mongodb
sudo apt autoremove
Install MongoDB Community Edition (Complex)
MongoDB Community Edition is hosted on the MongoDB servers. It’s more challenging to install, but the version available from the developers is always up-to-date.
The first step is to import the public key to the Ubuntu Server:
sudo wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add –
The system confirms that the key is added, as seen below.
Note: If you get an error missing gnupg, enter the following command and then try again:
sudo apt-get install gnupg
Create a list file and add the MongoDB repository:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
Refresh the package database:
sudo apt-get update
Install MongoDB:
sudo apt-get install mongodb-org
By default, Ubuntu’s package manager will download and install updates as they are released. If you check the MongoDB version, the system confirms that you have installed the latest available version, v4.2.1.
To lock your version, use the following:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Manage MongoDB Community Edition Service
Use the following commands to manage the MongoDB service:
To stop MongoDB:
sudo systemctl stop mongod
To start MongoDB:
sudo systemctl start mongod
To restart MongoDB when it’s already running (for example, to apply configuration changes):
sudo systemctl restart mongod
To prevent MongoDB from launching at boot:
sudo systemctl disable mongod
To set MongoDB to launch at boot:
sudo systemctl enable mongod
The community edition uses mongod as the service name. This is a significant difference from the version in the default repositories. Otherwise the commands are identical.
How to Use the MongoDB Shell (Community Edition)
To launch the MongoDB shell, enter the following:
mongo
To exit the Mongo shell, use either the keystroke:
Ctrl-C
or enter the following command:
quit()
Uninstall MongoDB Community Edition
To uninstall MongoDB Community Editiion, enter the commands:
sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Configure Firewall for MongoDB
If you are working on the same system that hosts MongoDB, you may skip this step.
MongoDB uses port 27017 to communicate. You can open that port on your firewall, but doing so would allow unrestricted access and is not recommended.
Instead, specify the IP address of a system that will connect to MongoDB, and grant access only to a specific IP address. To do so use the following:
sudo ufw allow from remote_server_IP/32 to any port 27017
Replace remote_server_IP with the actual IP address of the system you’ll be connecting from.
Verify the change by entering:
sudo ufw status
Check the list – you should see traffic allowed on port 27017.
Note: These instructions are for the default ufw application. If you are using a different firewall, use its configuration commands instead.
Edit the mongodb configuration file to listen to another IP address:
sudo nano /etc/mongodb.conf
Find the entry labeled bind_ip, and add a comma and the IP address of your remote system:
bind_ip = 127.0.0.1, remote_server_IP
#port = 27017
Save the file, and restart the MongoDB service as above.
Basic MongoDB Setup
Open a Mongo shell by entering the following:
mongo
Show a list of all available databases:
show dbs
MongoDB comes with authentication turned off. You can secure it by turning on authentication, creating a root user and password. Start by switching to the admin database:
use admin
Create a root user by entering the following:
db.createUser({user:"root", pwd:"complex_password", roles:[{role:"root", db:"admin"}]})
Note: Replace complex_password with a complex and secure password of your choosing.
Exit the Mongo shell by pressing CTRL+C. Next, edit the mongodb.service file to enable authentication:
sudo nano /lib/system/system/mongodb.service
Find the Service section, and under that the ExecStart entry.
Add the ––auth immediately after /usr/bin/mongod, so it looks as follows:
ExecStart=/usr/bin/mongod --auth --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS
Save the file and exit. Reload the system and MongoDB services:
sudo systemctl daemon-reload
sudo systemctl restart mongodb
sudo systemctl status mongodb
In order to log in to Mongo, you need to authenticate your user account. Enter the following:
mongo –u “root” –p ––authenticationDatabase “admin”
When prompted, type the password you set above. The Mongo shell should open as before.
Conclusion
By following this tutorial you should have successfully installed MongoDB on your Ubuntu server. You have also installed tools that enable secure remote access and require additional user authentication.
Ready to create a MongoDB database?
If you are searching for an alternative NoSQL database, you probably came across Cassandra. To find out the differences between these two databases, check out our article on Cassandra Vs. MongoDB.