MongoDB is an open-source document database for modern web applications. In this article, I will cover step by step guide to install MongoDB on Ubuntu 20.04.
So let’s dive in.
MongoDB installation Steps
Step 1: Installing MongoDB
You can download MongoDB stable version from the ubuntu repository, but it may not be the latest version. To download the latest version, run the following command:
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
This command will return OK
. Run the following command, which creates a file in the sources.list.d
:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Now run following commands to update all the packages:
sudo apt update
sudo apt install mongodb-org
Press Y
and then ENTER
to confirm that you want to install the package. After command finishes, MongoDB installed on your Ubuntu.
Step 2: MongoDB starting and Database Testing
To start the MongoDB service, run following command:
sudo systemctl start mongod.service
To check MongoDB status, run following command:
sudo systemctl status mongod
This command will return following output:
Output
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2021-05-06 16:27:07 UTC; 2s ago
Docs: https://docs.mongodb.org/manual
Main PID: 37128 (mongod)
Memory: 64.8M
CGroup: /system.slice/mongod.service
└─37128 /usr/bin/mongod --config /etc/mongod.conf
If the service is running perfectly, enable the MongoDB service to startup at boot. Run following command:
sudo systemctl enable mongod
To check the database operational or not, you can use the following command.
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Here, connectionStatus parameter will check the database connectivity and return the value. Parameter 1 is for server, if it is properly working or not.
Output will be:
Output
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1dc7f67x-0az5-4864-g9c3-8g2db7ol9g43") }
MongoDB server version: 4.4.0
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}
Here, Database is running under port no 27017 and the server address is 127.0.0.1 (localhost).
Step 3: MongoDB Quick Commands
To check the MongoDB services status use systemctl command.
sudo systemctl status mongod --> To check Mongo Status
sudo systemctl stop mongod --> To Stop Mongo Services
sudo systemctl start mongod --> To Start Mongo Services
sudo systemctl restart mongod --> To Restart Mongo Services
sudo systemctl disable mongod --> To disable automatic startup on boot
sudo systemctl enable mongod --> To enable automatic startup on boot
Conclusion
In this article, you have learned how to install MongoDB on Ubuntu 20.04. If you have any doubts or if you like this article, please share it on your social media.