Come chat with us and we will get back to you as soon as possible!
Contact SupportHolyHosting
Holy Team

MongoDB is one of the most popular open-source NoSQL database management systems. It stores data in a document format instead of a tabular format. Since it doesn't have a rigid schema, MongoDB offers greater scalability and flexibility than SQL.
This database management system (DBMS) is popular among developers due to its performance and compatibility with various programming languages. However, to install MongoDB, Ubuntu users need to run several commands, making the process challenging.
With this in mind, we will explain how to install MongoDB on a virtual private server (VPS) with Ubuntu. We will also cover the steps to create a new database, configure a user, and enable remote authentication.
What Is MongoDB?
MongoDB is one of the most popular open-source NoSQL database management systems. It is widely used for large-scale applications or websites.
Unlike SQL, MongoDB stores data in a BSON document with a flexible schema, resulting in greater scalability. It is also compatible with various operating systems, programming languages, and frameworks.
Prerequisites for MongoDB Installation on Ubuntu
Before installing the official MongoDB package, verify your hardware and software compatibility. Your VPS hosting plan must be compatible with Ubuntu, offer an SSH connection, and provide full root access.
HolyHosting's VPS hosting offers several Linux distributions, including Ubuntu. You can easily install it by going to VPS Panel → Operating System and Panel.
We recommend using the latest operating system to ensure compatibility and security. However, the steps to install MongoDB on Ubuntu 20.04 or other versions are similar.
Our VPS hosting plans also allow you to connect remotely via SSH. In addition to using an application like PuTTY and Terminal, you can run Linux commands directly from your web browser using our Browser Terminal.
HolyHosting's VPS also provides full root access, allowing users to run MongoDB installation commands without permission issues.
How to Install MongoDB on Ubuntu
In this section, we will explain how to install MongoDB on Ubuntu 20.04 or other versions.
Before continuing, make sure you have connected to your VPS via SSH using a root or superuser account.
1. Install MongoDB
Before installing the MongoDB package, download GnuPG and the cURL utility by running the following command in your command-line interface:
```bash
sudo apt-get install -y gnupg curl
```
Use cURL and GnuPG to import the MongoDB public GPG key and obtain the installation package:
```bash
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
```
Note that the URL varies depending on the MongoDB packages. In this tutorial, we will install MongoDB Community Edition 7.0, the latest stable version at the time of writing.
After importing the official MongoDB packages, create a list file for the installation. The command differs depending on your Ubuntu version. For Ubuntu 22.04 or later, run:
```bash
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
```
Meanwhile, run the following to set up the list file on Ubuntu 20.04:
```bash
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
```
Update the APT repository to sync the local package database:
```bash
sudo apt-get update
```
Run the following command to install the latest stable version of MongoDB using the APT package management system:
```bash
sudo apt-get install -y mongodb-org
```
Optionally, choose a specific version of the official mongodb-org package. For example, run the following command to install MongoDB version 7.0.2:
```bash
sudo apt-get install -y mongodb-org=7.0.2 mongodb-org-database=7.0.2 mongodb-org-server=7.0.2 mongodb-mongosh=7.0.2 mongodb-org-mongos=7.0.2 mongodb-org-tools=7.0.2
```
If you only specify the MongoDB version and not the other components, APT will install the newest package.
Since APT automatically updates the component package, pin the installation to keep the current version. Here are example commands:
```bash
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
```
Pro Tip: If you don't import the public GPG key to your local package database, APT will install the unofficial MongoDB package. This could lead to compatibility, security, and legal issues, as well as lack of support.
2. Start the MongoDB Service
After installation, run MongoDB Community Edition by entering the following command:
```bash
sudo systemctl start mongod
```
The systemctl command is essential for MongoDB service management. For example, if you encounter an error, run the following to reload running services and try starting the DBMS again:
```bash
sudo systemctl daemon-reload
```
To verify if the MongoDB database server loaded correctly, check its status with this command:
```bash
sudo systemctl status mongod
```
You should
see "active (running)" if the MongoDB service is running, similar to the following output.

By default, the service does not start at boot. To automatically load it at startup, enable MongoDB with the following command:
```bash
sudo systemctl enable mongod
```
Check the service status with systemctl. If it shows "Enabled" instead of "Loaded," MongoDB is ready to use.
The main MongoDB daemon process is called mongod. It handles data requests, manages access, and runs background management operations.
3. Configure MongoDB
Change MongoDB database settings by modifying the main configuration file. It is automatically created during the installation process and accessible through /etc/mongod.conf.
Use a text editor like nano to open and modify the file. Here is the command:
```bash
sudo nano /etc/mongod.conf
```
There are several configuration options that determine the behavior of the MongoDB server. For example, systemLog defines the logging settings for your database, while net allows you to modify network-related settings.
Warning! Be careful when editing the mongod configuration file, as you could break the service or expose your server to security risks.
All options have various parameters. For example, the systemLog option has the path parameter that determines the default log storage location, i.e., /var/log/mongodb/mongod.log.

Instead of deleting parameters and options, add a hash symbol (#) to deactivate them. It's a good practice, as you can easily reactivate them by removing the character.
After editing the configuration file, restart MongoDB using this command to apply the changes:
```bash
sudo systemctl restart mongod
```
4. Create a New Database
The MongoDB installation process automatically creates the admin database. Since it is generally used for administrative purposes like storing authentication data, we recommend creating a new one.
To do so, enter the Mongo shell by running the following command:
```bash
mongosh
```

MongoDB has the same command for creating and accessing databases. If one with the specified name exists, the shell will switch to it.
For example, the following will take you to the default database since the name exists:
```bash
use admin
```
Meanwhile, run this command in the MongoDB shell to create the customers database:
```bash
use customers
```
To verify all MongoDB databases on your system, run the following:
```bash
show dbs
```
The terminal lists all MongoDB databases.
5. Create a New User
Since MongoDB doesn't have a default account, you must create a new user for each database and set their privileges. You can do this using the db.createUser() function.
For example, we will create a new MongoDB root user account with full permissions. After switching to the admin database mode with the use command, enter:
```bash
db.createUser(
{
user: "root",
pwd: "$tr0ngPa$$w0rD",
roles: [ { role: "root", db: "admin" } ]
}
)
```
You can write the function in one or multiple lines. Remember to pay attention to capitalization as it is case-sensitive.
MongoDB has several roles, such as dbAdmin, dbOwner, and readWrite. We recommend granting minimal permissions to the user account to prevent unauthorized modifications and data access.
Important! MongoDB user management is database-based, which means you must run the use command before entering a function.
To display all users within your current database, use this command:
```bash
show users
```
The terminal displays users in the current MongoDB database.
To test the database connection, exit the MongoDB console by entering the exit command in the Terminal and run the following:
```bash
mongosh --port [port] -u [username] -p '[password]' '[database]'
```
For example, we will connect to the root user using the default port of 27017:
```bash
mongosh --port 27017 -u root -p '$tr0ngPa$$w0rD' 'admin'
```
6. Enable Remote Authentication
By default, MongoDB authorizes all logins from the host machine. While this is sufficient for local deployments, you may encounter issues when running an application from another device.
To access MongoDB from another system, modify the bindIP parameter of the mongod.conf file. Its default value is the local IP address, which denies access from machines other than the host server.
Warning! Be careful when changing IP addresses in the MongoDB configuration file, as you can make your database public, exposing your service to serious security risks.
Use nano to open the mongod.conf file and add your VPS public IP address to the bindIP parameter, like this:
```bash
bindIP: 127.0.0.1,123.123.12.1
```
We don't recommend changing the value to 0.0.0.0, as allowing all IP addresses from connected systems will expose your server to cyberattacks.
To ensure only permitted users can access your database, enable authentication by adding the following line under the security section:
```bash
authorization: enabled
```
Make sure you have removed the hash symbol at the beginning of the security option. Press Ctrl + X, Y, and Enter to save the changes. Restart MongoDB with the following command:
```bash
sudo systemctl restart mongod
```
After installing the Mongo utility on your local system, you should be able to connect to the database server by running this command syntax:
```bash
mongo "mongodb://user:password@ip:port/?authSource=database_name"
```
For example, we will connect to MongoDB using the root user, with the admin database as the authenticator. Here is the command:
```bash
mongo "mongodb://root:[email protected]:27017/?authSource=admin"
```
To improve remote access security, limit MongoDB's listening port to only your local system's IP address. On Ubuntu, you can do this using the Uncomplicated Firewall (UFW) command:
```bash
sudo ufw allow from your_local_ip_address to any port 27017
```
This means the firewall will only accept connections from the specified IP address and block all others.
More About VPS and Databases
- [How to Show Users in MySQL Using Linux Commands](#): Link
- [How to Install PostgreSQL on Ubuntu](#): Link
- [How to Install phpMyAdmin on Ubuntu](#): Link
Conclusion
MongoDB is a popular free and open-source NoSQL database management system for large-scale websites or applications. Since it doesn't use a fixed schematic structure to store data, it is more flexible and scalable than SQL.
In this article, we have provided the MongoDB installation guide for Ubuntu. Here is a summary:
1. Install MongoDB: Import the official MongoDB repository key using GnuPG and cURL. Create the list file and install it using the APT package manager.
2. Start MongoDB: Run the MongoDB instance using the systemctl command. Enable the daemon to ensure it starts automatically during system boot.
3. Configure MongoDB: Open the mongod.conf configuration file using a text editor. Modify your database settings and restart Mongod to apply the changes.
4. Create a New Database: Enter the Mongo shell with the mongosh command. Run the use command to create a new MongoDB database or access an existing one if the specified name already exists.
5. Create a New User: Access the MongoDB shell and enter the db.createUser() function to create a new user in your current database. Set their password and permissions.
6. Enable Remote Authentication: Modify the bindIP setting in the mongod.conf file to allow remote access to your database. To improve security, enable MongoDB authentication and limit the allowed IP address using UFW.
When deploying MongoDB, use a VPS with full root access and broad software compatibility like HolyHosting's to ensure a smooth setup process. We also offer a one-click OS installer and the Browser Terminal to make the task easier.
Come chat with us and we will get back to you as soon as possible!
Contact SupportAll the information has been carefully documented and made available in our most recent YouTube tutorial. You can watch it below.
The SkinRestorer plugin can be very useful if you are looking to use your server in non-premium mode. In this case, when configuring it as "online-mode:f
The PHP.ini file is a configuration file that contains your web server's PHP settings. Although it comes preconfigured, you may need to change the default PHP