Sling Academy
Home/MongoDB/What is the Default Port of MongoDB and How to Change It

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

Last updated: February 01, 2024

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.

Next Article: When not to use MongoDB and why?

Previous Article: Where does MongoDB store data and how to change it

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)