MySQL Query Service 3.0.0
⚠️ Breaking change — read this before you update
queryandexecuteno longer accept the same statements. Until now both
services ran anything and differed only in what they returned, so a service
calledquerywould happily run aDELETE. From 3.0.0 each does one thing:
mysql_query.queryruns only statements that read and refuses anything else.mysql_query.executeruns only statements that change something and refuses a read.If you call the wrong one, the error tells you which one you wanted. Fixing it
is a one-word change per call — see Migration below.
Migration
If you use query for writes — change the service name to mysql_query.execute.
Nothing else changes. The fields query, values, db4query and config_entry
are identical, and the rows are still under result.
One thing to know: query stops your automation when a statement fails, while
execute reports the failure in its response as succeeded: false. If you were
relying on the automation stopping, add raise_on_error: true.
# Before (2.x)
action: mysql_query.query
data:
query: DELETE FROM readings WHERE logged_at < %s
values: ["{{ (now() - timedelta(days=30)).isoformat() }}"]
# After (3.0.0)
action: mysql_query.execute
data:
query: DELETE FROM readings WHERE logged_at < %s
values: ["{{ (now() - timedelta(days=30)).isoformat() }}"]
raise_on_error: true # only if you relied on query aborting on failureIf you use execute for reads — change the service name to mysql_query.query.
You lose nothing. query now returns succeeded, rows_found, column_names,
execution_time_ms and error alongside result: exactly the fields that made
execute worth using for a SELECT.
If you already read through query and write through execute — nothing to do.
If you send several statements in one call — split them into separate calls.
A call carrying more than one statement is now refused on both services. The
driver has multi-statement support switched on and cannot be told otherwise, so
SELECT 1; DELETE FROM states used to run both while reporting only the result
of the first, and the integration called that a successful SELECT. A trailing
semicolon is still fine, and so is a semicolon inside a quoted value.
raise_on_error is now a flag on both services
Only the default differs, so a call that does not mention it behaves the way that
service always did:
| Service | Default | Behaviour on a failed statement |
|---|---|---|
mysql_query.query
| raise_on_error: true
| Raises; the automation stops |
mysql_query.execute
| raise_on_error: false
| Returns succeeded: false, fills error
|
Set it explicitly and you get the other behaviour, on either service.
New: read-only connections
A connection can now be marked Read-only, in the setup form and under
Configure. When it is on, every call to mysql_query.execute on that
connection is refused, whatever the statement contains.
This catches the other half of the problem. The service split catches reaching
for the wrong verb: a DELETE sent to query is refused because query does
not write. The read-only flag catches reaching for the wrong connection: a
perfectly well-formed DELETE sent to execute is refused because that
connection is not supposed to be written to at all. It needs no statement
analysis, so nothing can be phrased around it.
It exists for the connection you created to feed dashboards or reports. Off by
default, so existing connections are unaffected.
What counts as read-only
query accepts SELECT, WITH, SHOW, DESCRIBE/DESC, CHECKSUM TABLE,
HELP, and the MySQL 8 TABLE and VALUES shorthands. Everything else goes to
execute, including maintenance statements that look informational but rewrite
something: ANALYZE TABLE, CHECK, OPTIMIZE and REPAIR.
EXPLAIN and ANALYZE are prefixes in front of another statement, not statements
of their own, and they are not handled the same way:
EXPLAINis always read-only, whatever follows it. It only produces a plan
and never carries out what it describes, soEXPLAIN DELETEgoes toquery
just asEXPLAIN SELECTdoes.ANALYZEtakes over the classification of the statement behind it, because
it really runs that statement.ANALYZE SELECTgoes toquery,ANALYZE DELETE
toexecute.ANALYZE TABLEis the exception: the maintenance command, which rewrites
index statistics, so always a write.
What this protection is not
The check strips comments and reads the leading keywords of the statement. It
guards against reaching for the wrong service, not against a determined write. A
SELECT can still write through INTO OUTFILE or a stored function with side
effects, and MySQL 8 accepts a CTE in front of an UPDATE. If a connection must
never write, give its database user SELECT rights only, and consider marking the
connection read-only.
Also in this release, if you are coming from 2.2.0
Version 2.3.0 was prepared but never published, so its feature arrives here too:
Encrypt the connection (TLS). A new option per connection that encrypts the
traffic to the database. The server certificate is not verified — a database
on a home network nearly always carries a self signed one — so this defends
against passive eavesdropping, not against an attacker who can actively intercept
the connection and present a certificate of their own. Turning it on against a
server without TLS fails with a message saying so, rather than quietly connecting
unencrypted. Off by default.
Housekeeping
The minimum Home Assistant version is described more precisely. The README
claimed HACS would not offer the update on an older installation. Only half of
that holds up: HACS compares the minimum declared in hacs.json against your
running version in can_download and refuses the download, but whether the
update notification is also hidden is not documented behaviour. On Home
Assistant older than 2025.3.0 you may therefore be told an update exists while
the download itself is blocked.
Two stray tags were tidied up. v0.1.0-prerelease pointed at exactly the
same commit as v0.1.0, which has a proper release, so the duplicate is gone.
v1.8.0 is deliberately kept as a tag without a release: the changelog links to
it, and publishing a release for it would put a pre-2.0 version — with the old
database driver — back into the list of versions HACS offers to install.
Upgrading
Existing connections keep working and nothing needs reconfiguring. The work is in
your automations and scripts: check every mysql_query.query call that writes,
and every mysql_query.execute call that only reads.
Full changelog: v2.2.0...v3.0.0