Sub topic 14
Common database ports
This chapter assumes you know what a port is, the three port ranges, and the difference between TCP and UDP. It also assumes you can read short Python scripts and run commands in a terminal.
Why database ports matter
A database server listens on a TCP port. Applications connect to that port with a driver, and the driver speaks the database's own protocol. Database ports are mostly in the registered range (1024–49151), so IANA lists them but the operating system does not restrict who can bind them.
Databases usually belong on a private network. The most common database mistake is not a wrong port number. It is a database port reachable from the internet.
Default ports
| Database | Default port | Transport |
|---|---|---|
| Microsoft SQL Server | 1433 | TCP |
| Oracle Database listener | 1521 | TCP |
| MySQL and MariaDB | 3306 | TCP |
| PostgreSQL | 5432 | TCP |
| MongoDB | 27017 | TCP |
| Redis | 6379 | TCP |
Each of these is a default, not a rule. The administrator can change it in the server configuration.
Mapping a connection to a port
Most drivers use the default port when you leave it out of the connection string. If the server runs on another port, you must give it explicitly.
DEFAULT_PORTS = {
"mssql": 1433,
"oracle": 1521,
"mysql": 3306,
"postgresql": 5432,
"mongodb": 27017,
"redis": 6379,
}
def endpoint(engine, host, port=None):
"""Return host:port, falling back to the engine's default port."""
return f"{host}:{port or DEFAULT_PORTS[engine]}"
print(endpoint("postgresql", "db.internal"))
print(endpoint("mysql", "db.internal", 3307))The first call prints db.internal:5432. The second prints db.internal:3307.
Checking a database port
Run these on the database host to see what is listening, then from an application host to test reachability.
# On the database host: is anything listening on 5432?
ss -tlnp | grep 5432
# From the application host: can we reach it?
nc -vz 192.0.2.20 5432If ss shows the port listening but nc fails, a firewall or a bind address is usually the cause. A server bound to 127.0.0.1 accepts only local connections, even when the firewall is open.
Common errors
- Exposed database port. Opening 3306 or 6379 to the whole internet invites password guessing and, for services with no authentication by default, direct data access. Allow only the application hosts.
- Wrong assumption from the port number. A service on 5432 is probably PostgreSQL, but the number does not prove it. Check the service itself.
- Bind address mistakes. A server listening only on localhost looks "closed" to remote hosts.
- No encryption. Database protocols are not all encrypted by default. Enable the engine's TLS option when traffic crosses a network you do not control.
Exercise
A new application cannot connect to a PostgreSQL server at 192.0.2.20. On the server, ss -tlnp shows 127.0.0.1:5432 listening. From the application host, nc -vz 192.0.2.20 5432 fails. What is the likely cause, and what two changes are needed?
Show answer
PostgreSQL is bound only to the loopback address, so it refuses remote connections. Change its listen address to include the server's network address (for PostgreSQL, the listen_addresses setting) and restart it. Then allow TCP 5432 in the firewall from the application host only.
Exercise
Which default port does MongoDB use, and which transport protocol carries it?
Show answer
Port 27017 over TCP.