[How to] Query MariaDB directly without Python or Shell

[How to] Query MariaDB directly without Python or Shell

I have been trying to write directly to MariaDB or read something from it since 8 am.

ALL forum entries so far have said that this is not possible because it is executed in isolation. Phyton and or shell scripts were always the solution.

I disagree with that now.

/homeassistant/configuration.yaml
shell_command:
  execute_sql: “/usr/bin/mariadb --ssl=0 -h core-mariadb -u homeassistant -phomeassistant -D homeassistant -e \”SELECT * FROM `statistics_runs` LIMIT 10;\” >> /config/sql_debug.log 2>&1”


/homeassistant/automations.yaml
- alias: “Execute SQL query”
  trigger:
    - platform: time
      at: “23:59:50”
  action:
    - service: shell_command.execute_sql

Maybe this will help someone at some point. It has now cost me half the day :frowning:

homeassistant as password must still be adjusted by you (if necessary).

The command can be executed directly for testing. It is only a SELECT statement.

The output is saved in sql_debug.log.
So /homeassistant/sql_debug.log

The logging can be updated (already in there)
>> /config/sql_debug.log

If you always want to have a fresh log, then remove a >
> /config/sql_debug.log

The rest should take care of itself :wink:

P.s. * Core 2025.1.0

  • Supervisor 2024.12.3
  • Operating System 14.1
  • Frontend 20250103.0

there was a huge update to the 2025 version yesterday. maybe something had been changed here.

Reading from the database can be accomplished more easily with the SQL sensor: