Port 27017 (MongoDB)

Port 27017 is the default port of the MongoDB database, which applications and admin tools use to connect to the server, over TCP.

Key facts

Port
27017
Protocols
TCP
Range
Registered port, 1024-49151
IANA service name
mongodb

IANA assigns ports in this range to services on request, under RFC 6335.

About port 27017

Port 27017 is the default port of the MongoDB database, which applications and admin tools use to connect to the server, over TCP. According to the MongoDB documentation, it's the default port of the mongod and mongos processes.

IANA registers TCP port 27017 for the mongodb service. MongoDB also uses ports 27018 and 27019 by default on the servers of a sharded cluster.

What port 27017 is used for

  • Document-based applications: applications written in Node.js or Python, for example, store data in MongoDB in a JSON-like structure, and connect to port 27017.
  • Separate database server: application servers connect to port 27017 on the database server, usually over a private network.
  • Replica Set: the servers in a Replica Set, a group of servers that keep identical copies of the data, connect to each other on port 27017.
  • Admin tools: the mongosh tool and graphical admin tools connect to port 27017, directly or through an SSH tunnel.

Port 27017 in the firewall

By default, MongoDB listens only on localhost (the bindIp setting), so when the application and the database are on the same server, there's no need to open port 27017. When access from another server is needed, add the internal network address to bindIp, and allow only the authorized addresses in the firewall:

  • UFW: ufw allow from 203.0.113.10 to any port 27017 proto tcp
  • firewalld: firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="27017" protocol="tcp" accept'

For occasional work from your computer, an SSH tunnel lets you connect to the database without opening port 27017 at all.

Risks and security

The MongoDB documentation recommends allowing access to the database only from trusted networks, and listening on the private network address when the server has several network interfaces. Alongside limiting access, a few simple settings complete the protection:

  • Turn on access control, so every connection requires a username and password.
  • Create a user for each application, with privileges only on its own database.
  • When the connection crosses a network that isn't private, encrypt it with TLS.
  • Back up your databases regularly, and keep the backups off the server.

Commands to check and configure

  • ss -tlnp | grep ':27017 '

    Shows which address MongoDB is listening on: 127.0.0.1 means local access only.

  • nc -zv db.example.com 27017

    Checks whether port 27017 on the database server is reachable from your computer.

  • grep -A3 '^net:' /etc/mongod.conf

    Shows the port and the addresses (bindIp) set in the configuration file.

  • mongosh "mongodb://db.example.com:27017" --username appuser

    Connects to a remote MongoDB server as a specific user. The password is prompted for separately.

  • ssh -L 37017:127.0.0.1:27017 user@server.example.com

    Opens an SSH tunnel: the admin tool on your computer connects to 127.0.0.1 on port 37017, and the connection reaches the database on the server.

  • ufw allow from 203.0.113.10 to any port 27017 proto tcp

    Allows access to port 27017 only from the given IP address (UFW).

  • firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="27017" protocol="tcp" accept'

    Allows access to port 27017 in firewalld only from the given IP address. Then run firewall-cmd --reload.

Port 27017 in the IANA registry

Port Protocol Service IANA registry description
27017 TCP mongodb Mongo database system
27017 UDP Reserved Reserved

Data source: IANA Service Name and Transport Protocol Port Number Registry (opens in a new tab)

Port 27017 FAQ

Do I need to open port 27017 for my application to work?

No, not when the application and the database are on the same server. In that case, the application connects to localhost, and the port stays closed in the firewall. Open port 27017 only when an application on another server needs to connect, and only to that server's IP addresses.

How do I connect securely to MongoDB on my server from my computer?

The recommended way is an SSH tunnel. Popular MongoDB admin tools support connecting over SSH, so the connection travels encrypted over port 22 and port 27017 stays closed to the internet.

What are ports 27018 and 27019 for?

They are MongoDB's default ports in a sharded cluster: port 27018 for the cluster's data servers, and port 27019 for its config servers. On a single MongoDB server or a regular Replica Set, only port 27017 is used.

Can I run MongoDB alongside another database on the same server?

Yes. Each database listens on its own port, for example MongoDB on port 27017 and PostgreSQL on port 5432, so they can run together on the same server, as long as it has enough memory and resources.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement