Sub topic 18
Troubleshooting Common Database Failures
Assume you are comfortable with the Linux command line, sudo, systemd, basic SQL, IP addresses, ports, and package managers. This chapter uses PostgreSQL and MySQL examples. Replace service names and paths when your distribution uses different packages.
Use a fixed troubleshooting order
A database connection depends on several layers:
- The database process must be running.
- The process must listen on the expected address and port.
- The firewall must allow the client address.
- The database must authenticate the client.
- The account must have permission to use the requested database.
- The client must use matching TLS and connection settings.
Check these layers in order. This prevents changing authentication rules when the service is not running at all.
Check startup failures
Start with systemd and the journal. A failed service often reports a syntax error, a missing directory, an unavailable port, or incorrect ownership.
sudo systemctl status postgresql
sudo journalctl -u postgresql --since "15 minutes ago" --no-pager
sudo systemctl status mysql
sudo journalctl -u mysql --since "15 minutes ago" --no-pagerIf the service is inactive, do not repeatedly restart it without reading the error. Configuration files can contain invalid parameters, duplicate settings, or values unsupported by the installed database version. A full data directory can also prevent startup.
Check disk space and inode space:
df -h
df -iCheck whether another process owns the database port:
sudo ss -ltnp | grep -E ':(5432|3306)\b'After correcting the cause, restart the affected service and confirm that it remains active:
sudo systemctl restart postgresql
sudo systemctl is-active postgresqlCheck connection failures
A service can be running while listening only on localhost. Inspect the actual listening sockets rather than trusting a configuration file:
sudo ss -ltnp | grep -E ':(5432|3306)\b'For PostgreSQL, test readiness from the server:
pg_isready -h 127.0.0.1 -p 5432For MySQL, test the server protocol:
mysqladmin ping -h 127.0.0.1 -P 3306A timeout usually indicates a route, firewall, or listening-address problem. An immediate “connection refused” usually means that no process is listening at that address and port. An authentication error means the network path worked, so inspect the username, password, host-based rules, TLS requirements, and account host pattern.
Inspect configuration and permissions
Find the active PostgreSQL settings from a local administrative session:
sudo -u postgres psql -Atc "SHOW config_file; SHOW hba_file; SHOW data_directory;"For MySQL, inspect the active data directory:
mysql -NBe "SELECT @@datadir, @@port, @@bind_address;"Do not make the data directory or its files broadly writable. Database processes normally require ownership by a dedicated service account. Check ownership before changing it:
sudo stat -c '%U:%G %a %n' /var/lib/postgresql /var/lib/mysqlCommon errors and safe responses
| Symptom | Likely cause | First check |
|---|---|---|
| Service exits during startup | Invalid configuration or permissions | `journalctl` |
| Connection times out | Firewall or network path | `ss`, firewall rules, and routes |
| Connection refused | Wrong port or no listener | `ss -ltnp` |
| Password authentication failed | Wrong credentials or host rule | Account and authentication configuration |
| Login succeeds but query is denied | Missing database or object privileges | Grants and selected database |
Exercise
A remote client receives “connection refused,” while systemctl status postgresql reports that PostgreSQL is active. What should you check next?
Show answer
Check ss -ltnp to see whether PostgreSQL is listening on the expected port and address. If it listens only on 127.0.0.1, review listen_addresses. If it listens on the private address, check the firewall and the client's destination port.