MongoDB »How to Install MongoDB on Ubuntu 20.04
MongoDB is a free open source document database. It belongs to the NoSQL family of databases, which differs from traditional table-based SQL databases such as MySQL and PostgreSQL.
In MongoDB, data is stored in flexible documents like JSON, where fields can differ from document to document. This does not require a predefined schema, and the data structure can change over time.
This article describes how to install and configure MongoDB Community Edition on Ubuntu 20.04.
The standard Ubuntu repos include the legacy MongoDB version. Installing the latest version of MongoDB on Ubuntu is pretty straightforward. We will enable the MongoDB repository, import the repository’s GPG key, and install the MongoDB server.
Installing MongoDB on Ubuntu 20.04
To install MongoDB on Ubuntu, follow these steps as root or as a user with sudo rights:
- Install the dependencies required to add a new repository over HTTPS:
sudo apt update sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
- Import the repository GPG key and add the MongoDB repository with:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'
At the time of this writing, the latest version of MongoDB is 4.4. To install a different version, replace it with your preferred version.
- After enabling the repository, install the mongodb-org meta package by typing:
sudo apt install mongodb-org
The following packages will be installed on your system:
- mongodb-org-server – The mongod daemon and related init and configuration scripts.
- mongodb-org-mongos – The mongos daemon.
- mongodb-org-shell – The mongo shell, an interactive JavaScript interface for MongoDB. It is used to perform administrative tasks from the command line.
- mongodb-org-tools – Contains several MongoDB tools for importing and exporting data, statistics, and other utilities.
- Start the MongoDB daemon and enable it to start at boot by typing:
sudo systemctl enable --now mongod
- To check if the installation completed successfully, connect to the MongoDB database server using the mongo tool and print the connection status:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
The result will look something like this:
MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") } MongoDB server version: 4.4.0 { "authInfo" : { "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }
A value of 1 for the ok field indicates success.
MongoDB setup
The MongoDB configuration file is named mongod.conf and is located in the / etc directory. YAML file.
In most cases, the default configuration settings are sufficient. However, for production environments we recommend uncommenting the security section and enabling authorization as shown below:
sudo nano /etc/mongod.conf
/etc/mongod.conf
security: authorization: enabled
The authorization parameter enables role-based access control (RBAC), which controls user access to database resources and operations. If this option is disabled, each user will have access to all databases and perform any actions.
While editing the MongoDB config file, restart the mongod service for the changes to take effect:
sudo systemctl restart mongod
To find more information on the configuration options available in MongoDB 4.4, visit the configuration file options documentation page.
Create MongoDB Administrator
If you have enabled MongoDB authentication, you need to create an admin user who can access and manage the MongoDB instance.
Mongo shell access:
mongo
From within the MongoDB shell, enter the following command to connect to the admin database:
use admin
switched to db admin
Run the following command to create a new user named mongoAdmin, password changeMe, and userAdminAnyDatabase role:
db.createUser( { user: "mongoAdmin", pwd: "changeMe", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
Successfully added user: { "user" : "mongoAdmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
Remember to set a stronger password. You can name the MongoDB admin user whatever you want.
After that, exit the mongo shell with:
quit()
To test your changes, log into the mongo shell using your previously created admin:
mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin
Run show users and you should see information about the newly created user:
show users
{ "_id" : "admin.mongoAdmin", "userId" : UUID("49617e41-ea3b-4fea-96d4-bea10bf87f61"), "user" : "mongoAdmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
You can also try to access the mongo shell without any arguments (just type mongo) and see if you can list the users using the same commands as above.
Output
We showed you how to install and configure MongoDB on Ubuntu 20.04. For more information on this topic, visit the MongoDB manual.
If you run into an issue or want to provide feedback, please leave a comment below.