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.

The MySQL Uncertainty Principle

Heisenberg's uncertainty principle for measurement of quantum phenomena has a MySQL analog. If you monitor MySQL's status to see how it changes over time, you may notice a curious effect for some of the indicators: Each time you take a measurement, you change the value you're measuring! For example, you can determine the number of queries the server has received by using the following statement:

SHOW STATUS LIKE 'Questions'

However, that statement is itself a query, so each time you issue it, you cause the Questions value to change. In effect, your performance assessment instrument contaminates its own measurements, something you may want to take into account.

Категории