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 -y

Step 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.list

Note: 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-org

Step 5: Start and Enable MongoDB

Start the MongoDB service and enable it to start automatically on boot:

sudo systemctl start mongod
sudo systemctl enable mongod

Verify MongoDB is running:

sudo systemctl status mongod

You should see an active status (active (running)).

mongodb 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

  1. Access MongoDB shell:

    mongosh
  2. Switch to the admin database (required to create an admin user):

    use admin
  3. Create an admin user (replace MongoAdmin and your-password with your desired username and password):

    db.createUser({
      user: "MongoAdmin",
      pwd: "your-password", // Replace with your desired password
      roles: [{ role: "root", db: "admin" }]
    })
  4. 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.

  1. Open the MongoDB configuration file:

    sudo nano /etc/mongod.conf
  2. Find the #security: line and uncomment it. Then, add the following:

    security:
      authorization: "enabled"
  3. Save the changes (Ctrl+X, then Y, and Enter).

  4. Restart MongoDB for the changes to take effect:

    sudo systemctl restart mongod
/etc/mongod.conf

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

  1. Open the MongoDB configuration file again:

    sudo nano /etc/mongod.conf
  2. Locate the following line:

    bindIp: 127.0.0.1

    Replace 127.0.0.1 with 0.0.0.0 to allow connections from any IP address (or use your laptop’s IP address to restrict access):

    bindIp: 0.0.0.0

    Note: Using 0.0.0.0 exposes your MongoDB instance to all IP addresses. For security, it's better to limit it to only trusted IPs.

  3. Save the file and exit the editor.

  4. 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 27017

Alternatively, 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=true

For your setup:

  • Username: MongoAdmin

  • Password: your-password

  • Host: 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

  1. Open MongoDB Compass on your laptop.

  2. In the Connection String field, paste the connection URL.

  3. 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:

  1. Authentication: You’re prompted for the username and password you created earlier.

  2. Roles and Permissions: You have access to create, read, update, and delete documents in MongoDB collections based on your user roles.


Troubleshooting

  1. Firewall Issues: If you're unable to connect remotely, ensure that your firewall allows traffic to port 27017.

  2. Authentication Errors: If you encounter authentication errors, double-check the authSource and make sure you're using the correct username and password.

  3. Network Connectivity: Ensure that the server’s IP address is reachable from your laptop (e.g., no network issues or blocked ports).

  4. 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 the bindIp to your trusted IPs.

  • User Roles: Assign specific roles based on your needs (e.g., readWrite, dbAdmin, etc.) instead of using root for 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