Skip to content

MySQL and MariaDB query (mysql_query) ​

Connects to a MySQL or MariaDB server, runs one read-only query of your own and reports how long it took, how many rows it returned and one value from the first row. The channels, the limits, expected, max_rows, the TLS parameters and the credential are the same as for postgres_query, which describes them in full and says why there is one kind for each engine. This page covers what is different here.

What it looks like ​

The page of a mysql_query sensor in the light theme

The sensor's page after a few polls, counting the connections of a MySQL 8.4 server. In the dark theme: the sensor's page. The form for adding it, with its parameters filled in: light, dark.

Who can attach it ​

Only administrators, as for postgres_query.

What keeps it read-only ​

  1. The account. The only protection that cannot be got around, and on MySQL the one to take most seriously: START TRANSACTION READ ONLY stops writes to tables, but a statement that implicitly commits (CREATE, ALTER, DROP, TRUNCATE) ends the transaction first and then runs. Give the sensor an account with SELECT on what it reads and nothing else.
  2. The transaction. The query runs after START TRANSACTION READ ONLY, and the sensor sets the server's own limit on the time it may run (max_execution_time on MySQL, max_statement_time on MariaDB). Multiple statements are not enabled on the connection, so the server accepts one statement and refuses a second.
  3. The check. The query must start with SELECT, WITH, VALUES, TABLE, SHOW, DESCRIBE or EXPLAIN and be one statement. INTO is refused, because SELECT ... INTO OUTFILE writes a file. MySQL's executable comments (/*! ... */) are refused, because the server runs their text. Strings and quoted names are read the way MySQL reads them, with backslash escapes and # comments.

The rows are read as the server sends them; the sensor stops at max_rows and closes the connection.

Parameters ​

As for postgres_query, with these differences:

ParameterDefaultMeaning
databasenoneThe database to open. Without one, no database is selected, which is enough for information_schema, performance_schema and queries that name the schema.
port3306TCP port.

The text a column holds is the server's own: 1.50 for a DECIMAL(10,2), 2024-03-01 10:11:12 for a datetime. Credential: a secret whose value is user:password. The connection uses character set utf8mb4.

Examples ​

Connections now, with the server's own status counter:

json
{
  "query": "SHOW GLOBAL STATUS LIKE 'Threads_connected'",
  "column": "Value",
  "value_name": "Connections"
}

Rows waiting in a queue table, a warning above 100 set on the channel:

json
{
  "database": "app",
  "query": "SELECT COUNT(*) AS waiting FROM queue WHERE state = 'waiting'",
  "tls": "optional"
}

On the server ​

sql
CREATE USER 'pwmonitor'@'192.0.2.%' IDENTIFIED BY '...' REQUIRE SSL;
GRANT SELECT ON app.queue TO 'pwmonitor'@'192.0.2.%';
GRANT PROCESS ON *.* TO 'pwmonitor'@'192.0.2.%';   -- only for queries on the process list or engine status

The sensor speaks the mysql_native_password and caching_sha2_password login methods, which are the ones MySQL 5.7 to 9 and MariaDB use by default. With caching_sha2_password the server wants the password in full the first time an account logs in, and only accepts that over TLS, so use tls: required or optional (MySQL 8 servers make their own certificate, so TLS is there; set verify_tls off or pin it). Over tls: off the sensor says so. An account that uses another login method (ed25519, unix_socket, sha256_password) is refused with a message that names the method; create the account with mysql_native_password or caching_sha2_password.

Failures ​

The ones that are the same as for postgres_query (the timeout, max_rows, the refused query, the certificate rules, a refused connection), and:

MessageMeaning
the server said: Access denied for user ... (error 1045) (kind auth_failed)Wrong user or password, or the account may not log in from this address.
the server wants the password in full (caching_sha2_password ...) (kind auth_failed)See above: use TLS.
the account logs in with the "..." authentication plugin, which the sensor does not speak (kind auth_failed)See above.
the server said: Unknown database ... (error 1049) (kind config)database names none.
the server said: ... (error 1064), 1146, 1054 (kind config)A syntax error, a table or a column that is not there.
the server said: ... (error 1142) (kind config)The account may not read what the query reads.
the server said: Query execution was interrupted, maximum statement execution time exceeded (error 3024) (kind timeout)The server's own limit stopped it.
the server does not offer TLS; ... (kind config)tls is required and the server has none.

Verification ​

Run against a real MySQL 8.4 server (with its own generated certificate) and a real MariaDB 11.4 server (with a self-signed certificate) in containers: a value and its column names, a count of rows, no rows, NULL, the row limit, a wrong password, a database that does not exist, a syntax error, a query cut off by the timeout, a refused address, a named database, SHOW, the transaction being read-only (MySQL reports its access mode, MariaDB that it is in a transaction), encryption without verification, optional encryption, refusal of an untrusted certificate, a wrong pin and the right one. The login scrambles and the packet parsing are checked with unit tests.

Not seen: MySQL 5.7, MySQL 9, MariaDB 10, Percona and other forks, a server behind a proxy that rewrites the handshake, the mysql_clear_password and sha256_password login methods. Written by hand and not run against a server: the first login with caching_sha2_password over plain text, which is refused on purpose.

Testing it yourself ​

PROBEWARD_TEST_MYSQL_ADDR (host:port), PROBEWARD_TEST_MYSQL_USER and PROBEWARD_TEST_MYSQL_PASSWORD, and the same with MARIADB for a MariaDB server; _TLS=1 says the server has TLS with a certificate that is not trusted. just dev-up starts both, and CI has them as services. The tests only run SELECTs of constants and of system views.

AGPL-3.0-only