How to Install and Use MongoDB on Ubuntu 20.04
This tutorial exists for these OS versions
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
- Ubuntu 16.04 (Xenial Xerus)
- Ubuntu 14.04 LTS (Trusty Tahr)
On this page
MongoDB is an open-source and cross-platform document-oriented database system written in C++. It stores data in collections of JSON-like, flexible documents and used to create powerful websites and applications. Due to its scalability and high performance, it is used for building modern applications that require powerful, mission-critical and high-availability databases.
In this tutorial, we will explain how to install and use MongoDB database system on Ubuntu 20.04 server.
Prerequisites
- A server running Ubuntu 20.04.
- A root password is configured on your server.
Install MongoDB
By default, the latest version of MongoDB is not available in the Ubuntu 20.04 default repository. So you will need to add the official MongoDB repository in your system.
First, install Gnupg package with the following command:
apt-get install gnupg -y
Next, download and add the MongoDB GPG key with the following command:
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
Next, add the MongoDB repository with the following command:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.2.list
Next, update the repository and install the MongoDB with the following command:
apt-get update -y
apt-get install mongodb-org -y
Once the installation has been completed, start the MongoDB service and enable it to start at reboot with the following command:
systemctl start mongod
systemctl enable mongod
You can now check the status of the MongoDB service with the following command:
systemctl status mongod
You should get the following output:
? mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-05-15 05:30:39 UTC; 18s ago Docs: https://docs.mongodb.org/manual Main PID: 106996 (mongod) Memory: 76.0M CGroup: /system.slice/mongod.service ??106996 /usr/bin/mongod --config /etc/mongod.conf May 15 05:30:39 ubuntu2004 systemd[1]: Started MongoDB Database Server. May 15 05:30:48 ubuntu2004 systemd[1]: /lib/systemd/system/mongod.service:11: PIDFile= references a path below legacy directory /var/run/, upd> lines 1-11/11 (END)
You can also verify the MongoDB version and the server address using the following command:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
You should get the following output:
MongoDB shell version v4.2.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("e1575445-f441-4b30-a5d7-4cf68852e68f") } MongoDB server version: 4.2.6 { "authInfo" : { "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }
Configure MongoDB
MongoDB default configuration file is located at /etc/mongod.conf. By default, each user will have access to all databases and perform any action. For production environments, it is recommended to enable the MongoDB authentication.
You can do it by editing the file /etc/mongod.conf:
nano /etc/mongod.conf
Add the following lines:
security: authorization: enabled
Save and close the file then restart the MongoDB service to apply the changes:
systemctl restart mongod
Create MongoDB Admin User
After enabling the MongoDB authentication, you will need to create an admin user to access and manage the MongoDB database.
First, access the MongoDB console with the following command:
mongo
You should see the following output:
MongoDB shell version v4.2.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("9ee0ea0c-cf95-4b83-9e88-00dc3a61e0a6") } MongoDB server version: 4.2.6 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user
Next, connect to the admin database with the following command:
> use admin
You should see the following output:
switched to db admin
Next, create a new admin user and set the password with the following command:
> db.createUser( { user: "admin", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
You should get the following output:
Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
Next, exit from the MongoDB console with the following command:
> quit()
Verify MongoDB Authentication
At this point, the MongoDB is configured with authentication.
Now, try to connect to MongoDB using the user which you have created earlier:
mongo -u admin -p --authenticationDatabase admin
You will be asked to provide the password as shown below:
MongoDB shell version v4.2.6 Enter password:
Provide your admin password and hit Enter. You should see the following output:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("00d6d1eb-d1c3-41ea-89c7-d46ec6d17bee") } MongoDB server version: 4.2.6
Now, change the database to admin with the following command:
> use admin
You should see the following output:
switched to db admin
Now, list the users with the following command:
> show users
You should get the following output:
{ "_id" : "admin.admin", "userId" : UUID("65907484-9d67-4e6e-bb88-a6666310e963"), "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } >
Conclusion
In the above guide, you learned how to install and configure MongoDB on Ubuntu 20.04 server. For more information, you can visit the MongoDB documentation at Mongo Doc.