
How to Enable Remote Access and Secure MongoDB Database
In the recent guide, we implemented how to install MongoDB on Ubuntu 22.04 Jammy Jellyfish distribution. MongoDB, a popular NoSQL database, offers flexible and scalable solutions for modern applications. Enabling remote access on MongoDB allows you to access your database from external sources, enabling collaboration, monitoring, and administration.
In this article, we will guide you through the process of enabling remote access on MongoDB using Ubuntu 22.04, ensuring secure and efficient management of your database.
Step 1: Update and Install MongoDB:
The first step is to ensure that your Ubuntu 22.04 system is up to date. Press ‘Ctrl+Alt+t’ to open the terminal window:
$ sudo apt update
Step 2: Configure MongoDB for Remote Access:
To enable remote access to the MongoDB database, open the configuration file for MongoDB ‘/etc/mongd.conf’ using nano or another source code editor:
$ sudo nano /etc/mongod.conf
Locate the bindIp directive:
Now, change its value from 127.0.0.1 to 0.0.0.0 to allow all IPv4 and IPv6 addresses. It means this step allows MongoDB to accept connections from any IP address. Press ‘Ctrl+O’ or ‘Ctrl+S’ to save changes and close the file using ‘Ctrl+x’.
Restart MongoDB to apply the changes:
$ sudo systemctl restart mongod
Step 3: Set Up Firewall Rules
To ensure the security of your MongoDB server, it’s essential to configure your firewall to allow incoming connections on the MongoDB port (default is 27017). Follow these steps:
Check the status of the Uncomplicated Firewall (UFW) by running:
$ sudo ufw status
If the firewall is inactive, enable it:
$ sudo ufw enable
Allow incoming connections on the MongoDB port:
$ sudo ufw allow 27017
Step 4: Configure MongoDB User Authentication
To secure the MongoDB database, it is crucial to set up user authentication. Follow these steps to create a user and grant remote access privileges:
Access the MongoDB shell by typing:
$ mongosh
Switch to the database administration:
>use admin
Create an administrative user with a username and password:
db.createUser({ user: "username_admin", pwd: "password", roles: [{ role: "root", db: "admin" }] })
Exit the MongoDB shell:
> exit
Step 5: Restart MongoDB with Authentication Enabled
Now that you have configured user authentication, restart MongoDB and enforce the authentication process:
$ sudo nano /etc/mongod.conf
Locate the line with the word ‘#security’ and add the following code:
security:
authorization: enabled
Save and close the file.
Restart MongoDB to apply the changes:
$ sudo systemctl restart mongod
Step 6: Test Remote Access
To verify that remote access is functioning correctly, follow these steps:
On a remote machine, open the terminal and execute this command:
$ mongo --host your_server_ip --port 27017 -u admin_username -p
Replace ‘your_server_ip’ with the IP address of your Ubuntu 22.04 server and ‘admin_username’ with the administrative username you created earlier.
Enter the password when prompted.
If you successfully connect, you have enabled remote access to your MongoDB server.
Conclusion
Enabling remote access on MongoDB with Ubuntu 22.04 opens up different possibilities for managing and collaborating on your database. By following the above step-by-step guide, you can securely configure your MongoDB server, allowing remote connections, setting up user authentication, and strengthening the overall security of your database. With these steps, you’ll be well on your way to leveraging the full potential of MongoDB for your applications.