MongoDB is a popular open-source NoSQL database that is designed for storing and managing large amounts of data. In this article, we will explain how to install and secure MongoDB on an Ubuntu 22.04 server.
Before you begin, make sure that you have an Ubuntu 22.04 server that is connected to the internet, and that you have a non-root user with sudo privileges.
To install MongoDB, you need to add the MongoDB package repository to your server’s package sources. You can do this by running the following command:
sudo apt-get install gnupg
wget -qO - <https://www.mongodb.org/static/pgp/server-4.4.asc> | sudo apt-key add -
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
This will add the MongoDB package repository to your server, and download the MongoDB package signing key.
Next, you need to update the package list and install MongoDB by running the following commands:
sudo apt-get update
sudo apt-get install -y mongodb-org
This will update the package list and install the latest version of MongoDB, along with the necessary dependencies.
Once MongoDB is installed, you need to start the MongoDB service and enable it to start automatically on system boot by running the following commands:
sudo systemctl start mongod
sudo systemctl enable mongod
These commands will start the MongoDB service and enable it to start automatically whenever the server is restarted.
Now that MongoDB is installed and running, you need to secure it by enabling authentication and restricting access to the MongoDB database. To do this, you need to log in to the MongoDB shell and create a new administrative user. You can do this by running the following commands:
sudo mongo
use admin
db.createUser({ user: "admin", pwd: "strongpassword", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
exit
These commands will log you in to the MongoDB shell, switch to the admin
database, and create a new administrative user with the username admin
and the password strongpassword
. This user will have the userAdminAnyDatabase
role, which allows them to manage users and their permissions for any database in MongoDB.
Once the administrative user is created, you need to enable authentication by editing the MongoDB configuration file. You can do this by running the following commands:
sudo nano /etc/mongod.conf
This will open the MongoDB configuration file in the nano
text editor. You need to find the security
section in the file, and change the authorization
setting from disabled
to enabled
. Your security
section should look like this:
security:
authorization: enabled
Once you have made this change, save the file and exit the text editor. Then, you need to restart the MongoDB service by running the following command:
sudo systemctl restart mongod
This will apply the changes to the MongoDB configuration and enable authentication. From now on, you will need to provide the username and password of the administrative user when connecting to the MongoDB database.
To further secure your MongoDB installation, you can also restrict access to the MongoDB server by enabling the bind_ip
setting in the MongoDB configuration file. This setting allows you to specify which IP addresses are allowed to connect to the MongoDB server. By default, the bind_ip
setting is commented out, which means that MongoDB will accept connections from any IP address. To restrict access to the MongoDB server, you need to uncomment the bind_ip
setting and specify the IP addresses that are allowed to connect. For example, if you want to allow connections only from the localhost and the 192.168.1.10
IP address, you can set the bind_ip
setting like this:
net:
bindIp: 127.0.0.1,192.168.1.10
Once you have made this change, save the file and exit the text editor. Then, restart the MongoDB service by running the sudo systemctl restart mongod
command. This will apply the changes to the MongoDB configuration and restrict access to the MongoDB server to the specified IP addresses.
In conclusion, installing and securing MongoDB on Ubuntu 22.04 is a simple process that involves adding the MongoDB package repository, installing MongoDB, creating an administrative user, and enabling authentication and restricting access to the MongoDB server. By following the steps outlined in this article, you can quickly and easily set up a secure MongoDB installation on your server and start managing your data with MongoDB.