Sub topic 13
Logging and Monitoring the Database Server
Assume you already know Linux commands, systemd, basic networking, and how to edit configuration files with sudo. This chapter uses PostgreSQL examples and notes the MySQL equivalents.
What to monitor
A database server produces logs and metrics. A log is a time-ordered record of events such as startup, authentication failures, slow queries, and crashes. A metric is a measured value, such as active connections, disk usage, or transaction rate.
Monitor these areas:
- Service health: Is the database process running?
- Connections: Are clients connecting, or has the connection limit been reached?
- Errors: Are authentication, permission, or disk errors increasing?
- Performance: Are queries slow, or is the server waiting for CPU, memory, or storage?
- Capacity: Is the data directory running out of disk space?
- Backups: Did the latest backup finish and produce a usable file?
The database service being active does not prove that applications can use it. A running service might listen on the wrong address, reject a user, or have a full disk.
Inspecting service state and logs
Use systemctl to check whether PostgreSQL is running. Use journalctl to read messages collected by the systemd journal.
sudo systemctl status postgresql
sudo journalctl -u postgresql --since "15 minutes ago" --no-pager
sudo journalctl -u postgresql -p warning..alert --since today --no-pagerThe -u option selects a service. The --since option limits the time range. The priority filter shows warnings and more severe messages.
MySQL commonly uses a unit named mysql:
sudo systemctl status mysql
sudo journalctl -u mysql --since "15 minutes ago" --no-pagerPostgreSQL can also write to files when logging is enabled in postgresql.conf. Common settings include logging_collector, log_directory, and log_min_duration_statement. MySQL commonly writes an error log and may write a slow-query log. Check the distribution's configuration before assuming a file path.
Check listeners and database activity
Confirm the process listens where you expect:
ss -ltnp | grep -E ':(5432|3306)'For PostgreSQL, inspect current sessions:
SELECT pid, usename, client_addr, state, wait_event_type,
now() - query_start AS query_age, query
FROM pg_stat_activity
WHERE state <> 'idle'
ORDER BY query_start;pg_stat_activity is a system view, which is a read-only interface to server status data. MySQL provides similar information through:
SHOW FULL PROCESSLIST;
SHOW GLOBAL STATUS LIKE 'Threads_connected';Compare connection counts with the configured maximum. A connection pool can reduce the number of expensive database sessions, but it cannot fix a query that holds locks or runs out of memory.
A practical monitoring routine
- Alert when the service stops or restarts unexpectedly.
- Alert on low free disk space for the data and log filesystems.
- Track connection usage, query latency, error counts, and replication delay.
- Retain logs according to your incident and audit needs.
- Test that monitoring alerts reach a person who can respond.
Exercise
The service is active, but the application reports “connection refused.” Which two checks should you perform first?
Show answer
Check ss -ltnp to confirm the database is listening on the expected address and port, then inspect the recent service logs with journalctl for startup or binding errors.