Step-by-Step Guide: Installing MongoDB on Ubuntu with Remote Access and Authentication Setup
Part 1: Install MongoDB on Ubuntu
Step 1: Update the System
Before installing any software, make sure your system is up to date:
do apt update
sudo apt upgrade -yStep 2: Import MongoDB Public Key
MongoDB’s official GPG key is required to authenticate packages. Download and add the MongoDB GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add Step 3: Add MongoDB Repository
MongoDB provides its own repository for Ubuntu. Add the repository to your system’s sources list:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.listNote: Replace focal with the code name of your Ubuntu version (e.g., bionic for Ubuntu 18.04).
Step 4: Install MongoDB
Now, update your package list and install MongoDB:
sudo apt update
sudo apt install -y mongodb-orgStep 5: Start and Enable MongoDB
Start the MongoDB service and enable it to start automatically on boot:
sudo systemctl start mongod
sudo systemctl enable mongodVerify MongoDB is running:
sudo systemctl status mongodYou should see an active status (active (running)).

active (running)Part 2: Configure MongoDB Authentication
By default, MongoDB doesn’t require authentication, but it's essential for security in a production environment. Here’s how to enable authentication:
Step 6: Create an Admin User
Access MongoDB shell:
mongoshSwitch to the
admindatabase (required to create an admin user):use adminCreate an admin user (replace
MongoAdminandyour-passwordwith your desired username and password):db.createUser({ user: "MongoAdmin", pwd: "your-password", // Replace with your desired password roles: [{ role: "root", db: "admin" }] })Exit the MongoDB shell:
exit
This creates a superuser (MongoAdmin) with root privileges that can perform any action on MongoDB.
Step 7: Enable Authentication
Now that the user is created, you need to enable authentication.
Open the MongoDB configuration file:
sudo nano /etc/mongod.confFind the
#security:line and uncomment it. Then, add the following:security: authorization: "enabled"Save the changes (
Ctrl+X, thenY, andEnter).Restart MongoDB for the changes to take effect:
sudo systemctl restart mongod

Part 3: Allow Remote Connections (Optional)
By default, MongoDB binds to 127.0.0.1, meaning it only allows local connections. If you want to connect to MongoDB remotely (e.g., from your laptop), you need to adjust the bind IP.
Step 8: Modify Bind IP
Open the MongoDB configuration file again:
sudo nano /etc/mongod.confLocate the following line:
bindIp: 127.0.0.1Replace
127.0.0.1with0.0.0.0to allow connections from any IP address (or use your laptop’s IP address to restrict access):bindIp: 0.0.0.0Note: Using
0.0.0.0exposes your MongoDB instance to all IP addresses. For security, it's better to limit it to only trusted IPs.Save the file and exit the editor.
Restart MongoDB again for the change to take effect:
sudo systemctl restart mongod
Step 9: Open Firewall Port (If Applicable)
If your server has a firewall enabled, you'll need to allow traffic on MongoDB’s default port (27017):
sudo ufw allow from <your-laptop-ip> to any port 27017Alternatively, to disable the firewall (not recommended for production), run:
sudo ufw disable| Note:💡Don’t forgot to open the port 27017 on your server providers Dashboard (in here mine is AWS Security Group)
Part 4: Connect to MongoDB with MongoDB Compass
Now that MongoDB is set up and remote access is enabled, you can connect to it using MongoDB Compass.
Step 10: Get the Connection URL
The connection URL to use in MongoDB Compass is formatted as follows:
mongodb://<username>:<password>@<serverIP>:<port>/?authSource=<authDb>&tls=trueFor your setup:
Username:
MongoAdminPassword:
your-passwordHost:
your-server-ip(replace with your actual server IP)Port:
27017(default MongoDB port)Authentication Database:
admin
Example Connection URL:
mongodb://MongoAdmin:your-password@your-server-ip:27017/?authSource=admin&tls=true💡If you're not using TLS in remote server simply remove that part from connection URL
Step 11: Connect Using MongoDB Compass
Open MongoDB Compass on your laptop.
In the Connection String field, paste the connection URL.
Click Connect.
If everything is set up correctly, you should be able to access your MongoDB instance from Compass.
Part 5: Verify the Connection and Access
Once connected via MongoDB Compass, ensure the following:
Authentication: You’re prompted for the username and password you created earlier.
Roles and Permissions: You have access to create, read, update, and delete documents in MongoDB collections based on your user roles.
Troubleshooting
Firewall Issues: If you're unable to connect remotely, ensure that your firewall allows traffic to port
27017.Authentication Errors: If you encounter authentication errors, double-check the
authSourceand make sure you're using the correct username and password.Network Connectivity: Ensure that the server’s IP address is reachable from your laptop (e.g., no network issues or blocked ports).
MongoDB Logs: If MongoDB isn’t starting or giving errors, check the logs for more information:
sudo journalctl -u mongod
Security Considerations
TLS Encryption: If you’re connecting over the internet, enable TLS to encrypt connections. You can follow the MongoDB docs on TLS/SSL Configuration.
Restrict IP Access: Instead of
0.0.0.0, restrict thebindIpto your trusted IPs.User Roles: Assign specific roles based on your needs (e.g.,
readWrite,dbAdmin, etc.) instead of usingrootfor everything.
This guide should get MongoDB up and running on your Ubuntu server, with authentication enabled and access set up for remote connections using MongoDB Compass. Let me know if you have any questions or comments on this blog 🌱
TAGS:
MongoDB
Ubuntu
Database Installation
MongoDB Authentication
Remote MongoDB Connection
MongoDB Compass
Linux Server Setup
Database Security
MongoDB Tutorial
Ubuntu Server
Last updated