Monitoring the MySQL Server
9.17.1 Problem
You want to find out how the server was configured or monitor its state.
9.17.2 Solution
SHOW VARIABLES and SHOW STATUS are useful for this.
9.17.3 Discussion
The SHOW VARIABLES and SHOW STATUS statements provide server configuration and performance information:
mysql> SHOW VARIABLES; +---------------------------------+-------------------+ | Variable_name | Value | +---------------------------------+-------------------+ | back_log | 50 | | basedir | /usr/local/mysql/ | | bdb_cache_size | 8388600 | | bdb_log_buffer_size | 0 | | bdb_home | | ... mysql> SHOW STATUS; +--------------------------+----------+ | Variable_name | Value | +--------------------------+----------+ | Aborted_clients | 319 | | Aborted_connects | 22 | | Bytes_received | 32085033 | | Bytes_sent | 26379272 | | Connections | 65684 | ...
This information can be useful for writing administrative applications. For example, you might write a long-running program that probes the server periodically to monitor its activity. A simple application of this type might ask the server to report the number of connections it's received and its uptime, to determine a running display of average connection activity. The queries to obtain this information are:
SHOW STATUS LIKE 'Connections'; SHOW STATUS LIKE 'Uptime';
If you want to avoid having to reconnect each time you issue the queries, you can ask the server for its client timeout period and probe it at intervals shorter than that value. You can get the timeout value (in seconds) with this query:
SHOW VARIABLES LIKE 'wait_timeout';
The default value is 28800 (8 hours), but it may be different on your system.