The default port for MongoDB is 27017.
Understanding MongoDB Default Ports
MongoDB utilizes different default ports depending on the specific instance type and configuration. While 27017 is the most common and widely recognized default port for general mongod
and mongos
instances, other specialized configurations use different ports.
Here's a breakdown of the default ports used by MongoDB:
Default Port | Description |
---|---|
27017 | This is the standard default port for mongod (the primary MongoDB database process) and mongos (the query router for sharded clusters) instances. This port is used in typical standalone deployments, replica sets, and sharded clusters for client connections. |
27018 | This port is the default for mongod instances when they are configured as a shard server. This occurs when mongod is started with the --shardsvr command-line option or when the clusterRole setting in the configuration file is set to shardsvr . |
How to Change the Default Port
While these are the default settings, MongoDB provides flexibility to change the port number to suit specific networking requirements or security policies. You can specify a different port when starting a mongod
or mongos
instance using:
- The
--port
command-line option. For example:mongod --port 27019
- The
port
setting in a configuration file. For example:net: port: 27019
Changing the default port is a common practice for:
- Security: Obscuring the default port can be part of a defense-in-depth strategy, though it should not be the sole security measure.
- Port Conflicts: Avoiding conflicts with other applications running on the same server that might already be using port 27017.
- Compliance: Meeting specific network or application deployment standards.
For more detailed information on MongoDB configuration and best practices, you can refer to the official MongoDB documentation.