What is the Default Port of MongoDB and How to Change It

Updated: February 1, 2024 By: Guest Contributor Post a comment

Overview

MongoDB, a prominent NoSQL database used for high volume data storage, operates over a default port which is essential for connections between your application and the database server. For security, flexibility or due to network architecture requirements, at times it becomes necessary to change the default port. This guide will take you through understanding what the default port is, why it might need to be changed, and how to change the MongoDB default port with step-by-step instructions and examples.

Understanding the Default Port of MongoDB

By default, MongoDB instances run on port 27017. This port is set in the MongoDB configuration file, which might vary in location depending on the operating system. The default setup is sufficient for development or if you’re running the database in a controlled environment. However, changing the default port can be part of security by obscurity strategies, help avoid conflicts with other services, and set up specific network routing rules.

Checking the Current Port Configuration

Before changing the MongoDB port, you may want to verify the current port configuration. You can do this by connecting to the MongoDB shell and executing the db.serverCmdLineOpts() command, which returns the configuration of the running mongod instance:

mongo --port 27017
> db.serverCmdLineOpts()

The output should provide a segment like this:

{
  "argv" : [
    ...
  ],
  "parsed" : {
    "net" : {
      "port" : 27017
    },
    ...
  },
  ...
}

This indicates that the database is running on port 27017.

Changing MongoDB’s Default Port

There are several methods to change MongoDB’s default port. The following steps illustrate each method from basic to advanced configurations.

Via Configuration File

The most common way to change the default port is by editing the MongoDB configuration file, usually named ‘mongod.conf’. Find and modify the net.port value to the desired port number:

net:
  port: 27018

After saving the changes, restart the MongoDB service to apply the new configuration:

sudo service mongod restart

Via Command Line

If you are starting MongoDB from the command line instead of a service, you can specify the port directly with the --port argument:

mongod --port 27018

Via Docker

When running MongoDB within a Docker container, you can set the port mapping with the -p flag:

docker run --name mymongo -d -p 27018:27017 mongo:latest

This maps the default MongoDB port inside the container (27017) to port 27018 on the host machine.

Via Environment Variables

In managed hosting environments or docker-compose setups, you may need to use environment variables to change the port. Use the environment directive to set the service’s port:

services:
  mongo:
    image: mongo
    environment:
      - MONGO_PORT=27018
    ports:
      - "27018:27018"

Via Firewalls and Security Groups

If you cannot change the MongoDB configuration directly, such as in some managed services, you can reroute the port using firewalls or security groups. This method involves mapping incoming traffic on the new port to the default MongoDB port.

For example, if using iptables on Linux:

sudo iptables -t nat -A PREROUTING -p tcp --dport 27018 -j REDIRECT --to-port 27017

Advanced Configuration

Advanced users may want to implement port changes by coupling with security features such as binding to specific IP addresses or implementing authentication.

net:
  port: 27018
  bindIp: 127.0.0.1,192.168.1.100
security:
  authorization: enabled

This will change the port, limit connections to the listed IP addresses, and enable MongoDB authentication.

Verifying the Port Change

After changing the port, verify that MongoDB is running on the new port by using the lsof or netstat commands:

sudo lsof -i :27018
sudo netstat -plnt | grep 27018

These commands list the services running on port 27018, confirming the change.

Troubleshooting

Common issues when changing the port may include permissions errors, conflicts with other services, or forgetting to allow the new port through firewalls. Ensure you restart the MongoDB service after making changes, and always back up configuration files before modifying them.

Conclusion

Changing the MongoDB default port can be straightforward when following the proper procedures. It enhances security and allows for more flexible network configurations. Always ensure you take into account potential network conflicts and security implications when applying these changes.