Code on a monitor.
Image: Maximusdn/Adobe Stock

MongoDB is a powerful and flexible NoSQL server that can be used for many types of modern apps and services. MongoDB is also scalable and can handle massive troves of unstructured data.

SEE: Hiring Kit: Database engineer (TechRepublic Premium)

I’ve outlined how to install MongoDB on both Ubuntu and RHEL-based Linux distributions, but one thing that was left out was how to configure it for remote access.

Note that the installation for RHE-based distributions has changed to accommodate the latest version of MongoDB. The new installation requires a different repository and installation command. The repository is created with the command:

sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo

The content for that repository is:

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

Finally, the installation command is:

sudo dnf install mongodb-org mongodb-mongosh -y

Now that you have MongoDB installed and running, you need to configure it for remote access. Why? Because you might want to use the MongoDB server as a centralized location to serve data to other remote machines.

What you’ll need to enable remote access in MongoDB

To enable MongoDB for remote access, you’ll need a running instance of MongoDB and a user with sudo privileges.

How to enable remote access for MongoDB

The first thing we must do is enable authentication. To do that, access the MongoDB console with the command:

mongosh

Change to the built-in MongoDB admin with:

use admin

Create a new admin user with the following:

db.createUser(
  {
    user: "madmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)

You can change madmin to any username you like. You’ll be prompted to create a new password for the user. A word of warning: You only get one chance to type that password, so type it carefully.

Next, open the MongoDB configuration file with:

sudo nano /etc/mongod.conf

Locate the line:

#security:

Change that line to:

security:
    authorization: enabled

Save and close the file.

Restart MongoDB with:

sudo systemctl restart mongod

Now, we can enable remote access. Once again, open the MongoDB configuration file with:

sudo nano /etc/mongod.conf

In that file, locate the following section:

net:
  port: 27017  bindIp: 127.0.0.1  

Change that section to:

net:  port: 27017
  bindIp: 0.0.0.0

Save and close the file. Restart MongoDB with:

sudo systemctl restart mongod

If you’re using the firewall on your server, you’ll need to open it for port 27017. For example, on Ubuntu-based distributions, that would be:

sudo ufw allow from remote_machine_ip to any port 27017

Reload the firewall with:

sudo ufw reload

Remote access granted

At this point, you should be able to connect to your MongoDB on port 27017 using the new admin user and password you created above. That is all there is to enable MongoDB for remote access. When you want to use that server as a centralized DB platform, this can make that possible.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Check these resources from TechRepublic Academy to learn more about MongoDB:
Build RESTful APIs with Node, Express, & MongoDB
Fullstack Web Development: Build Websites with Node & MongoDB NoSQL Databases
The Data Analytics Expert Certification Bundle

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays