MongoDB is a NoSQL database that stores data in a flexible, document-oriented format. This makes it different from traditional relational databases like MySQL or PostgreSQL. It is a powerful, scalable, and flexible database commonly used for applications that need to manage large volumes of diverse data. This article will show you how to install MongoDB on Ubuntu 24.04, manage its services, and allow remote connections on an Ubuntu VPS.
Table of Contents
Step 1. Update the System
Before we start with the MongoDB installation, updating the system packages to their latest versions is recommended. To do that, execute the command below:
apt update -y && apt upgrade -y
Install gnupg and curl if they are not already installed:
apt-get install gnupg curl -y
Step 2. Add Key and Repository
In this article, we will install the latest version of MongoDB 8.0 from the official MongoDB repository. So we will need to create a MongoDB repository for that.
Before we add the repository, we need to add the GPG key with the command below:
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \ gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \ --dearmor
Then, add the MongoDB repository:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
Once this is done, update the system:
apt update -y && apt upgrade -y
Step 3. Install MongoDB on Ubuntu 24.04
Now, we are ready to start installing the latest version of MongoDB simply by executing the following command on the server.
apt install mongodb-org -y
After this, start and enable the MongoDB service:
systemctl start mongod && systemctl enable mongod
To check the status of MongoDB, execute the command below:
systemctl status mongod
You should receive the following output:
root@host:~# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled)
Active: active (running)
Docs: https://docs.mongodb.org/manual
Main PID: 26761 (mongod)
Memory: 86.8M (peak: 87.0M)
CPU: 1.074s
CGroup: /system.slice/mongod.service
└─26761 /usr/bin/mongod --config /etc/mongod.conf
Once the installation is complete, verify the version of MongoDB installed:
mongod -version
Output:
db version v8.0.4 Build Info: { "version": "8.0.4", "gitVersion": "bc35ab4305d9920d9d0491c1c9ef9b72383d31f9", "openSSLVersion": "OpenSSL 3.0.13 30 Jan 2024", "modules": [], "allocator": "tcmalloc-google", "environment": { "distmod": "ubuntu2404", "distarch": "x86_64", "target_arch": "x86_64"
Step 4. Configuring MongoDB administrator username
To set up the MongoDB administrator username and password, we need to open the MongoDB shell and type the following:
mongosh
Inside the Mongo shell, type this command to switch to the admin database:
> use admin
Now, let’s create the administrator username and set a password for the username:
> db.createUser({user:"admin", pwd:"T7nm8Jfr6BnJ", roles:[{role:"root", db:"admin"}]})
Type this command in the shell to exit the shell:
> exit
Step 5. Enable MongoDB authentication
To enable password authentication, we need to edit the MongoDB configuration file.
The configuration file is at /etc/mongod.conf by default. Open the file:
nano /etc/mongod.conf
Scroll down and find the security section. Uncomment it and add “authorization: enabled”:
security: authorization: enabled
To apply the changes, restart the MongoDB service.
systemctl restart mongod
Now if you try to connect to MongoDB by typing “mongosh” without any arguments. You will get some output suggesting that authentication is required.
test> show dbs MongoServerError[Unauthorized]: Command listDatabases requires authentication
To authenticate, log in using the administrative user with the following command:
mongosh -u admin -p --authenticationDatabase admin
You’ll get prompted for a password. Enter the password you set above.
Now, only the administrative user can view, create, and modify the data in the database.
Step 6. Configure MongoDB for remote access
MongoDB, by default, listens for connections on port 27017 on localhost (IP 127.0.0.1) only.
In order to allow a remote MongoDB connection, you need to add your server IP address to the MongoDB configuration file.
nano /etc/mongod.conf
Locate the network interfaces section and add your server IP address:
bind_ip = 127.0.0.1, your_server_ip
After we save the changes in the MongoDB configuration file, we need to restart the MongoDB service with the following command for the changes to take effect:
systemctl restart mongod
You can log in remotely using the following command:
mongosh "mongodb://admin@your_server_ip:27017"
The shell automatically prompts you for the admin user’s password.
Step 7: Uninstall MongoDB
If, for any reason, you do not like MongoDB and you need to remove it from your system, you can follow these commands:
sudo service mongod stop sudo apt purge mongodb-org* sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb
Congratulations! You have successfully installed MongoDB on Ubuntu 24.04.
Of course, you don’t have to do any of this if you use one of our Ubuntu Hosting services. In that case, you can simply ask our expert Linux admins to set this up for you. They are available 24×7 and will take care of your request immediately.
If you liked this post, please share it with your friends or leave a comment below.