MongoDB is a popular, open-source NoSQL database known for its flexibility, scalability, and support for high-velocity data applications. Install MongoDB on a server like Ubuntu 22.04 gives developers a robust environment to host their applications. In this article, we’ll walk you through the process of installing MongoDB on an Ubuntu 22.04 server, setting up your database, and configuring it for secure, efficient performance.
What is MongoDB?
MongoDB is a NoSQL document database, meaning it stores data in flexible, JSON-like documents instead of traditional table-based structures. It is schema-less, which means it doesn’t require a fixed structure, making it perfect for dynamic and rapidly evolving data. MongoDB supports high-volume data handling and is popular for use cases including real-time analytics, content management, IoT applications, and more.
Key features of MongoDB:
- Document-Oriented: Facilitates a more dynamic and expressive data model compared to traditional tables.
- Horizontal Scalability: Supports sharding out of the box for distributing data across many machines.
- Flexible Schema: Allows changes to data structure without downtime.
- High Performance: Optimized for read and write operations with efficient storage.
- Strong Consistency: Writes are immediately visible to all reads in any data model within a single replica set.
In a nutshell, MongoDB stands as a versatile, powerful database choice that’s tailor-made for modern needs where scalability, flexibility, and performance align seamlessly.
Why Install MongoDB on Ubuntu 22.04?
Using MongoDB on Ubuntu 22.04 combines the latest features of MongoDB with the reliable and lightweight Ubuntu server environment. Advantages include:
- Scalability: Easily scale your applications as they grow without rigid data schemas.
- High-Performance: MongoDB’s flexible data structure offers optimized data handling.
- Extensibility: Store complex data structures without predefined schemas.
- Community Support: Ubuntu and MongoDB both have large, active user communities.
Buying a VPS Ubuntu 22.04 at vpswindows.com
Before you can install MongoDB, you need a reliable VPS. At vpswindows.com, we offer optimized VPS solutions, including those running Ubuntu 24.04, designed to meet the needs of developers and businesses. Our VPS plans come with high-performance SSD storage, customizable resource allocation, and full root access. With 24/7 support and seamless scalability, our VPS options are perfect for hosting MongoDB and any other applications you want to run efficiently.
Here’s why using VPSWindows.com is a strategic choice:
- Reliable Uptime: Ensures your server is accessible whenever you need it.
- Scalable Plans: Allows you to adapt your resources according to your evolving requirements.
- Responsive Support: Provides assistance and troubleshooting to keep your setup running smoothly.
- Cost-Effective Pricing: Offers a practical approach for startups and businesses of all sizes.
Prerequisites
Before starting, ensure you have:
- An Ubuntu 22.04 server: MongoDB performs best on a dedicated server or VPS.
- Root or Sudo Privileges: Use a user with root or sudo privileges to perform the installation and configuration.
- System Update: It’s always good to update your system packages for compatibility.
Install MongoDB on Ubuntu 22.04
The first step is to install the prerequisite packages needed during the installation. To do so, run the following command.
sudo apt install software-properties-common gnupg apt-transport-https ca-certificates -y
To install the most recent MongoDB package, you need to add the MongoDB package repository to your sources list file on Ubuntu. Before that, you need to import the public key for MongoDB on your system using the wget command as follows:
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg – – dearmor
Next, add MongoDB 7.0 APT repository to the /etc/apt/sources.list.d directory.
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
The command adds the MongoDB 7.0 sources list file to the /etc/apt/sources.list.d/ directory. This file contains a single line that reads:
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”
Once the repository is added, reload the local package index.
sudo apt update
The command refreshes the local repositories and makes Ubuntu aware of the newly added MongoDB 7.0 repository.
With that out of the way, install the mongodb-org meta-package that provides MongoDB.
sudo apt install mongodb-org -y
The command installs the MongoDB database server along with the database core components including the shell tools. Once the installation is complete, verify the version of MongoDB installed:
mongod –version
This lists a bunch of information including the version, Git, and OPenSSL version among other details.
Start MongoDB service
The MongoDB service is disabled upon installation by default, and you can verify this by running the below command:
sudo systemctl status mongod
To start the MongoDB service, execute the command:
sudo systemctl start mongod
Once again, confirm if the service is running:
sudo systemctl status mongod
From the above output, you can see that MongoDB is up and running. Additionally, you can confirm that the database is up and running by checking if the server is listening on its default port which is port 27017.
To do so, run the following ss command:
sudo ss -pnltu | grep 27017
You should see the following output on your terminal.
After you have verified the service is running as expected, you can now enable MongoDB to start on boot as shown.
sudo systemctl enable mongod
So far, MongoDB has successfully been installed and configured to start on boot.
Securing MongoDB on Ubuntu
Authentication is not enabled by default in MongoDB, which means that any user with access to the database server can view, add, and delete data. This vulnerability can cause a serious breach of your data, which is why securing MongoDB is important. In this section, we will show you how you can secure MongoDB on Ubuntu 22.04.
Access Mongo Shell
The first step is to create an administrative user, and to do so, first access Mongo Shell.
mongosh
Connect or switch to the admin database.
use admin
Create an admin user named Adminvps
Then create a database user by pasting these lines and pressing ENTER on your keyboard.
admin> db.createUser(
{
user: “Adminvps”,
pwd: passwordPrompt(),
roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “readWriteAnyDatabase” ] }
)
In there:
- user: “Adminvps” : creates an admin user named Adminvps.
- pwd: passwordPrompt() : will prompt you for the admin user’s password. This is a more secure alternative to the pwd: field which requires you to enter the password in clear text.
- roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “readWriteAnyDatabase” ] : the line defines the roles granted to the admin user. Here, the Admin user is granted read and write access to the admin database. Since this role is defined in the admin database, the admin user can read and modify all databases in the cluster.
Run the exit command or press CTRL + C to exit the Mongo Shell.
Enable Authentication:
With the Adminvps Administrator user already in place, the next step is to enable authentication. To do this, exit the MongoDB shell and open the mongod.conf file.
vi /etc/mongod.conf
Go to security section, remove comment (#) add authorization and set it to enabled
security:
authorization: enabled
Note that the authorization parameter is indented while security has no leading space.
Restart MongoDB
Save the changes and exit the configuration file. To apply the changes, restart MongoDB as shown.
systemctl restart mongod
Check Status
systemctl status mongod
Log into MongoDB Shell, now no longer see the warning as above
mongosh
However, if you try to perform any database related tasks, such as viewing the database, you will get some output suggesting that authentication is required.
show dbs
To authenticate, first log out of Mongo Shell by running the exit command.
Log in as the admin user as follows.
mongosh -u Adminvps -p –authenticationDatabase admin
From now on, only the admin user can view, create, and modify data in the database.
To exit the shell, run the exit command.
This tutorial covered how to install MongoDB on Ubuntu 22.04 and start using this powerful open-source database. On their official website, you can find further documentation on MongoDB’s advanced features and use cases.
Conclusion
Install MongoDB on Ubuntu 22.04 is straightforward and provides your server with a robust, high-performance database solution. MongoDB’s flexibility and scalability make it ideal for various applications, from small projects to large-scale, real-time data processing systems.
With this guide, you’ve not only install MongoDB but also configured it securely, performed basic commands, and learned how to back up and restore data. A properly configured MongoDB database on an Ubuntu 22.04 server will empower you to build dynamic, scalable applications that meet the needs of modern web and enterprise applications.
Thank you for reading this tutorial! For more in-depth guides and useful tech knowledge, follow the VPSWindows blog. Don’t forget VPSWindows offers affordable, reputable, and quality VPS services to enhance your server performance and reliability.