Sub topic 14
Common database ports
This chapter covers the default ports of common database servers, how clients reach them, and how to check and restrict them. It assumes you know what an IP address is, how a client connects to a server, and what a TCP port is. You should be able to run commands in a terminal and read a short Python script.
Why database ports need extra care
Most protocols in this tutorial are meant to be reachable by many users, such as web, DNS and mail. A database is usually the opposite. Only a small set of application servers should ever talk to it. Knowing the default port tells you what to expect in a configuration file or a firewall rule. It also tells you what should never be reachable from the internet.
Almost all database servers use TCP. A database session is long-lived and carries ordered queries and results, so it needs the reliable byte stream that TCP provides. The client picks an ephemeral port, and the server listens on the fixed port. This is the same socket pattern covered in the chapter on sockets and ephemeral ports.
Default ports at a glance
Most of these ports fall in the registered range (1024–49151). That is why they are conventions rather than universal rules, as the chapter on port ranges explained.
| Database | Default port | Transport | Notes |
|---|---|---|---|
| MySQL / MariaDB | 3306 | TCP | MySQL's X Protocol uses 33060 |
| PostgreSQL | 5432 | TCP | Set by `port` in `postgresql.conf` |
| Microsoft SQL Server | 1433 | TCP | SQL Browser answers on UDP 1434 |
| Oracle Database | 1521 | TCP | Port of the listener process |
| MongoDB | 27017 | TCP | Used by `mongod` and `mongos` |
| Redis | 6379 | TCP | Cluster bus uses the data port plus 10000 |
| Cassandra | 9042 | TCP | Native (CQL) clients; nodes talk on 7000 |
| Elasticsearch | 9200 | TCP | HTTP/REST API; node transport uses 9300 |
| Memcached | 11211 | TCP and UDP | A cache, not a full database |
| CouchDB | 5984 | TCP | HTTP API |
| Neo4j | 7687 | TCP | Bolt protocol; browser UI on 7474 |
Treat this table as a list of defaults. Any administrator can change them, and many do.
What a connection looks like
A client connects to the server's IP address and the database port. The result is a socket pair like this:
- Client:
10.0.1.15:50732(ephemeral port chosen by the operating system) - Server:
10.0.2.20:5432(PostgreSQL's fixed port)
Client tools take the port as an option or as part of a connection URL. When you omit it, the tool uses the default for that database.
# PostgreSQL: explicit host and port
psql -h 10.0.2.20 -p 5432 -U appuser appdb
# MySQL: use 127.0.0.1 (not "localhost") to force TCP instead of a Unix socket
mysql -h 127.0.0.1 -P 3306 -u appuser -p
# Connection URLs carry the port inline
# postgresql://[email protected]:5432/appdb
# mongodb://10.0.2.20:27017/appdb
# redis://10.0.2.20:6379/0Note that mysql -P is an uppercase P for the port and a lowercase -p for the password prompt. When the host is localhost, the MySQL client normally uses a Unix socket file, and the TCP port is never involved.
Edge cases worth knowing
One product, several ports
Many systems listen on more than one port, and each port serves a different purpose.
- Redis Cluster uses the normal data port (6379) and a cluster bus port. By default the bus port is the data port plus 10000, which is 16379. Cluster nodes must reach each other on both.
- Elasticsearch serves its REST API on 9200 and node-to-node traffic on 9300.
- Cassandra serves clients on 9042 and uses 7000 (or 7001 with TLS) between nodes.
- MySQL serves the classic protocol on 3306 and the X Protocol on 33060.
When you write firewall rules for a cluster, include the inter-node ports. Rules for the client port alone leave replication broken.
SQL Server instances and dynamic ports
SQL Server's default instance listens on TCP 1433. Named instances often pick a dynamic port instead. A client then asks the SQL Server Browser service on UDP 1434 which port the instance is using. A firewall that allows only TCP 1433 will therefore block named instances. Either pin the instance to a fixed port or allow the browser lookup too.
Databases that speak HTTP
CouchDB and Elasticsearch use HTTP on their ports. You can reach them with curl, and a browser can show their JSON replies. Their ports are not 80 or 443, but the protocol is the same HTTP you studied earlier. The port number does not decide the protocol. The software on the other end does.
Memcached and UDP
Memcached listens on TCP and, in older configurations, UDP 11211. Exposed UDP servers like this were abused in large amplification attacks, where a small spoofed request produced a much larger reply aimed at a victim. Modern versions disable UDP by default. Disable it yourself if your version does not.
Checking ports from the command line
Start on the database host. List what is listening and on which address:
# All listening TCP sockets with owning processes
sudo ss -tlnp
# Narrow to one port
sudo ss -tlnp 'sport = :5432'Read the "Local Address" column carefully. 127.0.0.1:5432 means only local processes can connect. 0.0.0.0:5432 or [::]:5432 means every interface accepts connections, and the firewall is then the only barrier.
Then test from a client machine. The script below attempts a TCP connection to each common database port on a host you own and reports one of three results.
import socket
import sys
DB_PORTS = {
1433: "SQL Server",
1521: "Oracle",
3306: "MySQL / MariaDB",
5432: "PostgreSQL",
6379: "Redis",
9042: "Cassandra",
9200: "Elasticsearch",
11211: "Memcached",
27017: "MongoDB",
}
def check(host: str, port: int, timeout: float = 1.0) -> str:
try:
with socket.create_connection((host, port), timeout=timeout):
return "open"
except ConnectionRefusedError:
return "closed (nothing listening)"
except OSError:
return "no answer (filtered or unreachable)"
host = sys.argv[1] if len(sys.argv) > 1 else "127.0.0.1"
for port, name in sorted(DB_PORTS.items()):
print(f"{host}:{port:<6} {name:<18} {check(host, port)}")Run it as python3 dbscan.py 192.0.2.10, with your own server's address in place of the example. Only scan systems you own or are authorized to test.
The three outcomes tell you different things:
- Open: a process accepted the TCP handshake.
- Closed: the host replied with a reset, so the path works but nothing listens on that port.
- No answer: packets were dropped or never arrived. This usually points to a firewall or a routing problem.
An open port proves that something is listening. It does not prove the database is healthy. For that you need a real login or query.
Firewall reasoning for databases
Write the rule from the traffic that must exist, then deny the rest. For the layout in the diagram, the reasoning is:
- The application servers (
10.0.1.0/24) must reach TCP 5432 on the database host. - A replica at
10.0.2.21must reach the primary on 5432 for replication. - Administrators reach the host through SSH on TCP 22, preferably from a management network.
- No other source needs access, so everything else is denied.
Restrict by source address as well as by port. A rule that opens 5432 to any address allows password guessing from the whole internet. Defense works in layers. Bind the server to the interface it needs, filter with a firewall, require authentication, and enable TLS so credentials and data are not sent in clear text.
Common errors
- "Connection refused": the host answered, but nothing listens on that port. Check the service status, the configured port, and whether it is bound to
127.0.0.1only. - "Connection timed out": packets are being dropped. Check host firewalls, cloud security groups and network ACLs along the path.
- Works locally, fails remotely: the server is bound to loopback only. Change the listen address setting (for example
listen_addressesin PostgreSQL orbind-addressin MySQL) and open the firewall for the right sources. - Wrong port in the client: connecting a MySQL client to 5432 yields a protocol error, not a refusal. The port is open, but the program on it speaks a different protocol.
Exercise
A scan from your laptop shows 5432 as "no answer", but ss -tlnp 'sport = :5432' on the server shows 127.0.0.1:5432. Name the problem you would fix first, and give a safer alternative to opening the port to the internet.
Show answer
The server is bound to the loopback address, so it only accepts local connections. Remote clients cannot reach it regardless of the firewall. Change the listen address to the server's private interface and allow TCP 5432 only from the application subnet. For administration from a laptop, use an SSH tunnel or a VPN instead of exposing the port to the internet.
Exercise
You deploy a three-node Redis Cluster whose nodes use data port 6379. Which TCP ports must be open between the nodes?
Show answer
Ports 6379 and 16379. The cluster bus port is the data port plus 10000, and nodes use it to exchange cluster state with each other.