How to enable MongoDB for remote access

Looking to use your MongoDB server from another machine? If so, you must configure it for remote access.

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.

For all the latest Technology News Click Here 

 For the latest news and updates, follow us on Google News

Read original article here

Denial of responsibility! TechNewsBoy.com is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – [email protected]. The content will be deleted within 24 hours.