Sub topic 6
Controlling Which Addresses the Server Listens On
Assume you are comfortable with Linux commands, sudo, systemd, IP addresses, ports, and basic database administration. This chapter focuses on network exposure. Listening settings decide where a database accepts network traffic, but they do not decide which users may log in. Authentication and firewall rules provide additional controls.
The listening model
A database server listens on an IP address and port. A client connects to that address and port. For example, PostgreSQL normally uses TCP port 5432, while MySQL normally uses TCP port 3306.
Common choices include:
127.0.0.1: accepts IPv4 connections only from the same host.::1: accepts IPv6 loopback connections from the same host.- A private address such as
10.0.0.5: accepts connections through that network interface. 0.0.0.0or*: listens on every IPv4 interface. This is usually broader than necessary.
Use a private interface when an application runs on another host in the same private network. Avoid listening on a public interface unless you have a specific reason and additional protections.
Configure the bind address
PostgreSQL uses listen_addresses in postgresql.conf. The file is commonly under /etc/postgresql/<version>/main/, but the exact path depends on the distribution. PostgreSQL must be restarted after changing this setting.
# postgresql.conf
listen_addresses = 'localhost,10.0.0.5'
port = 5432This example permits local connections and connections arriving at 10.0.0.5. It does not automatically permit every client. PostgreSQL also checks pg_hba.conf, which contains host-based authentication rules.
MySQL uses bind-address in its server configuration, often under /etc/mysql/mysql.conf.d/mysqld.cnf. The following configuration binds MySQL to one private IPv4 address:
[mysqld]
bind-address = 10.0.0.5
port = 3306After editing either configuration, restart the matching service:
sudo systemctl restart postgresql
# Or:
sudo systemctl restart mysql
ss -ltnp | grep -E ':(5432|3306)'The ss command shows listening TCP sockets. Check the displayed address, not only the port. 127.0.0.1:5432 is local-only, while 0.0.0.0:5432 is exposed on every IPv4 interface.
Check exposure from both sides
A local socket check confirms what the process opened. A test from an application host confirms whether routing and firewall rules allow the connection.
| Address shown by `ss` | Meaning |
|---|---|
| `127.0.0.1:5432` | PostgreSQL accepts IPv4 connections only from the local host |
| `10.0.0.5:5432` | PostgreSQL accepts connections through that private address |
| `0.0.0.0:5432` | PostgreSQL accepts connections on every IPv4 interface |
Exercise
ss reports 0.0.0.0:3306, but MySQL should be reachable only from the private network. What should you inspect first?
Show answer
Inspect bind-address in the MySQL server configuration, change it to the intended private address, restart MySQL, and check ss again. Then verify the firewall and MySQL account host restrictions.