Default Port for MongoDB: What Port 27017 Does and How to Change It Safely
The default port for MongoDB is 27017. Learn what it does, when to change it, how to configure it safely, and how to fix the most common connection errors.

Default Port for MongoDB: What Port 27017 Does and How to Change It Safely
The default port for MongoDB is 27017. A port is the numbered endpoint on a host that a service listens on, so when you connect to mongodb://localhost:27017 you are telling the client which process on that machine should receive the request. MongoDB's server process, mongod, binds to 27017 unless you tell it otherwise, and every official driver assumes that number when the connection string omits it. Related defaults matter too: mongos, the query router in a sharded cluster, also uses 27017, shard members conventionally use 27018, and config servers conventionally use 27019.
This looks like trivia until something breaks. Connection refused errors, a cluster that will not form, a firewall silently dropping traffic, or a database unexpectedly reachable from the internet are all port-level problems, and they are all diagnosable in a few minutes once you understand what binding to a port actually means. This guide covers the defaults, when changing the port is genuinely useful, how to change it correctly, and how to debug the errors that follow.
Quick Answer: MongoDB's default port is 27017. Sharded cluster shard members typically use 27018 and config servers 27019. You can change the port with the--portflag or thenet.portsetting in mongod.conf, but changing it is not a security measure — authentication and network restrictions are.
Where WebPeak Helps With MongoDB Ports, Access and Deployment Setup
Port and network configuration is where application code meets infrastructure, and it is a frequent source of environment-specific bugs that work locally and fail in staging. Teams that want that layer handled properly alongside their application often bring in engineering partners rather than guessing. WebPeak covers web development, web application development and cloud deployment work for clients worldwide, and in database engagements they routinely standardise ports across environments, replace hardcoded connection strings with injected secrets, lock bindIp down to private interfaces, and verify that firewall rules match the intended topology. Their services are documented at webpeak.org for teams that would rather have this configured once, correctly, than rediscover it during an outage.
What Is MongoDB's Default Port and Why Does It Exist?
MongoDB listens on TCP port 27017 by default, and that number is registered convention rather than a technical requirement. Understanding the surrounding defaults prevents most cluster setup confusion.
27017 — mongod and mongos. This is the standard port for a standalone server, for every member of a plain replica set, and for the mongos router that applications talk to in a sharded cluster.
27018 — sharded cluster shard members. When a mongod runs as part of a shard, convention places it on 27018 so it is clearly distinct from the router your application connects to.
27019 — config servers. Config servers store cluster metadata for a sharded deployment and conventionally listen on 27019.
Binding matters as much as the number. The bindIp setting controls which network interfaces MongoDB accepts connections on. Modern MongoDB packages default to 127.0.0.1, meaning only the local machine can connect. Changing that to 0.0.0.0 exposes the server on every interface, which is the single most consequential setting on this page — it should only ever be paired with authentication, TLS and firewall rules.
Ports are not authentication. Moving MongoDB to a non-standard port removes it from casual automated scans, but any real scan enumerates ports. Treat a port change as noise reduction, never as protection.
When Should You Change the Default MongoDB Port?
There are legitimate reasons and one bad reason. These are the situations where a change is justified:
- Running multiple instances on one machine. Two
mongodprocesses cannot share a port, so a local replica set for testing needs 27017, 27018 and 27019 assigned explicitly. - 27017 is already in use. A leftover process or another service holding the port will cause startup to fail; assigning a free port is the fast fix while you investigate.
- Organisational port policy. Some environments allocate ports centrally, and the database must comply with that allocation.
- Container and orchestration mapping. In Docker or Kubernetes you frequently keep 27017 inside the container while publishing a different host port to avoid collisions.
- Reducing automated scan noise on an exposed host. Acceptable as a secondary measure, and only ever in addition to authentication and network restrictions.
The bad reason is using an unusual port as your security strategy. If a MongoDB instance is reachable from the internet, the port number will not save it — authentication, TLS, a private network and a strict IP allow list will.
MongoDB Port Reference and Configuration
| Component or Setting | Default or Convention | What It Controls |
|---|---|---|
| mongod (standalone or replica set) | 27017 | Main database server listening port |
| mongos router | 27017 | Entry point applications connect to in a sharded cluster |
| Shard member mongod | 27018 | Individual shard nodes in a sharded deployment |
| Config server | 27019 | Cluster metadata storage for sharded clusters |
| net.port in mongod.conf | 27017 | Port the server binds to on startup |
| net.bindIp in mongod.conf | 127.0.0.1 | Network interfaces that may accept connections |
| Connection string port | 27017 when omitted | Port the driver dials if none is specified |
To change the port, either pass it at startup as mongod --port 27020, or set net.port: 27020 under the net section of your configuration file and restart the service. Then update three things together: the firewall rule allowing the new port, every application connection string, and any replica set member host definitions that reference the old port. Missing any one of those three is what turns a simple change into an outage.
Diagnosing Port Problems: Analysis From Real Debugging
Port issues produce a small set of recognisable symptoms, and each maps to a specific cause. This is pattern recognition from hands-on debugging rather than a statistic.
"Connection refused" almost always means nothing is listening. The packet reached the host and was actively rejected, so either mongod is not running, it is running on a different port, or it is bound only to localhost while you are connecting from elsewhere. Verify with ss -tlnp | grep mongo or lsof -i :27017, then check the service status.
A connection that hangs and times out means traffic is being dropped, not rejected. That points at a firewall, security group or network ACL rather than at MongoDB. The database is likely healthy; the path to it is not.
"Address already in use" on startup means the port is held by another process, often a previous mongod that did not shut down cleanly. Identify the owner before reassigning the port, because starting a second instance on a different port pointed at the same data directory causes worse problems than a failed start.
A replica set that will not form usually has mismatched host and port entries. Every member must be reachable by all other members at the exact host and port recorded in the configuration, so a member listening on 27018 but registered as 27017 will never be reachable.
An unexpectedly reachable database is a bindIp problem, not a port problem. If the instance answers from outside its network, bindIp is too permissive. Restrict it to the interfaces that need it and put the cluster behind a private network.
Two broader observations from operating these systems: standardising on 27017 everywhere except where a genuine collision forces a change removes an entire class of environment-specific bugs, and hardcoded ports scattered through application code are a recurring source of failed deployments — inject the full connection string as a single environment variable instead. For teams building out this kind of infrastructure discipline while also hiring for specialised platform and data roles, industry guidance on recruiting hard-to-find technical talent is a useful companion read.
Key Takeaways
- MongoDB's default port is 27017 for both
mongodandmongos; sharded clusters conventionally use 27018 for shards and 27019 for config servers. - Change the port with
--portat startup ornet.portin mongod.conf, then update firewall rules, connection strings and replica set member entries together. - A non-standard port reduces scan noise but provides no real security — authentication, TLS and network restrictions do.
- The
bindIpsetting, not the port number, determines who can reach your database, and it defaults to localhost for good reason. - "Connection refused" points to nothing listening; a hanging timeout points to a firewall dropping packets — the two errors have different fixes.
Frequently Asked Questions
What is the default port for MongoDB?
The default port for MongoDB is TCP 27017. Both the mongod server process and the mongos router use it unless configured otherwise, and every official driver assumes 27017 when your connection string does not specify a port explicitly.
Can I run MongoDB on a different port?
Yes. Start the server with mongod --port 27020 or set net.port in your configuration file and restart. Remember to update firewall rules, every application connection string and any replica set member definitions, otherwise clients and cluster members will keep dialling the old port.
Why is MongoDB refusing connections on port 27017?
Connection refused means nothing is listening on that port from your vantage point. Check that the service is running, confirm the actual listening port, and inspect bindIp — a server bound only to 127.0.0.1 will refuse connections from any other machine even when it is perfectly healthy.
Is changing the MongoDB port a good security practice?
It is a minor one at best. Changing the port reduces automated scan noise but any determined scan enumerates ports anyway. Real protection comes from enabling authentication, using TLS, keeping the database on a private network and restricting access with a strict IP allow list.
What ports do sharded MongoDB clusters use?
By convention, the mongos router listens on 27017 and is what applications connect to, shard member mongod processes use 27018, and config servers use 27019. These are conventions rather than requirements, but following them keeps cluster topology readable and reduces configuration mistakes.
Conclusion
The most important thing to internalise about MongoDB ports is that the port number decides where your database listens, while bindIp, authentication and firewall rules decide who can actually reach it. Confusing those two is what leads teams to change a port and believe they have hardened a system they have only relabelled. Your next step is a two-minute check: run ss -tlnp | grep mongo to confirm the real listening port and interface, then verify that authentication is enabled and that your firewall permits only the sources that genuinely need access. Get those two facts straight and both your connection errors and your exposure risk shrink dramatically.
Related articles
Web DevelopmentMongoDB vs SQLite: A Practical Guide to Choosing the Right Database for Your Project
MongoDB vs SQLite compared on architecture, concurrency, scaling, and cost, with clear guidance on when an embedded database beats a full document server.
Web DevelopmentHow to Choose Between WordPress and Custom Web Development
Compare WordPress and custom web development to find the right solution for your business based on cost, scalability, and long-term needs.
Web DevelopmentMongoDB Is Web Scale: What the Meme Got Wrong and What It Got Right
The phrase MongoDB is web scale began as a joke, but the scaling questions behind it are real. Here is what genuinely scales and what quietly breaks first.
