CREATE DATABASE homeassistantdb
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Home Assistant OS MySQL Database Migration
Migrate your Home Assistant OS Database from the default SQLite to a robust MySQL Database, speeding up your load time and keeping all your old history.
If you have a very complete home automation setup, with many entities sendind data into home assistant, you might want to consider migrating from the default SQLite into a robust database such as MySQL.
This guide will walk you through migrating Home Assistant OS from SQLite to a MySQL/MariaDB server with SSL/TLS, keeping your history.
(Reference HA documentation)[https://www.home-assistant.io/integrations/recorder/], check it if you have any doubts or want to make changes.
|
Data migration is not officially supported.
This process is straight forward and has minimal risk. Keep your HAOS backup and |
1. Requirements
-
Home Assistant OS (Supervisor-managed).
-
MySQL 8+ or MariaDB 10.3+ server, preferably with SSL/TLS enforced.
-
Basic Linux and MySQL knowledge to understand what you are doing.
2. Prepare the remote database (TLS + schema)
|
If your DB already exists and is used only by Home Assistant, you may skip the database creation step. Ensure the schema is empty before migration. |
CREATE USER 'homeassistant'@'HAOS_HOST' IDENTIFIED BY 'HAMYSQL_PASSWORD';
GRANT ALL PRIVILEGES ON homeassistantdb.* TO 'homeassistant'@'HAOS_HOST';
FLUSH PRIVILEGES;
|
If you use client certificates (mutual TLS), also provide a client cert/key to Home Assistant later. If you rely on a private CA, pinning |
You should have a CA certificate for the DB server’s certificate. You’ll need place it under /ssl on HA.
# download the ca.pem from the db server and then upload it to the haos
scp db@DB_HOST:/etc/mysql/ssl/ca.pem ca.pem
scp ca.pem root@HAOS_HOST:/root/ca.pem
# On HAOS, move it into the shared /ssl mount (visible to HA Core)
mv /root/ca.pem /ssl/ca.pem
3. Back up Home Assistant and database extraction
-
Create a full Backup in Settings > System > Backups, make sure to include
Historyin the backup. -
Download the backup tar file and extract
homeassistant.tar.gzfrom it, inside, athomeassistant/datayou will find thehome-assistant_v2.dbdatabase file we need.
4. Migrate SQLite to MySQL/MariaDB
|
Ensure the MySQL target database ( |
pip install sqlite3-to-mysql
|
The next step will from several minutes to a couple of hours, find something to do in the meanwhile and relax. |
sqlite3mysql \
--sqlite-file home-assistant_v2.db \
--mysql-host db.example.com \
--mysql-port 3306 \
--mysql-database homeassistantdb \
--mysql-user mysqluser \
--prompt-mysql-password
5. Connecting Home Assistant to the remote DB
Edit /config/configuration.yaml and update the recorder: block to use the mysqlclient driver.
recorder:
db_url: "mysql://homeassistant:[email protected]/homeassistantdb?charset=utf8mb4&ssl_ca=/ssl/ca.pem"
db_max_retries: 16
db_retry_wait: 20
commit_interval: 15
The commit_interval is set to 15 seconds to reduce how often the db writes are commited, reducing disk I/O, at the cost of data loss in the even of power failure.
|
|
6. Restart HA and verify
-
Restart Home Assistant to make sure the changes are applied correctly.
-
Watch Settings > System > Logs for recorder connection and schema checks.
-
If you see warnings about DB Schema corrections and column modifications, then the connection was successful, but it will take several minutes to apply the changes. Data history should already be available.
7. Post-migration tuning (optional)
If the migration was successful and has been working with no issues, you can remove the home-assistant_v2.db from HAOS to free up disk space.
To keep the database lean and fast, we can config filters to purge old data that we hardly consult in history, modifying the configuration.yaml:
recorder:
auto_purge: true
purge_keep_days: 90
exclude:
entities:
- sensor.last_boot
- sensor.date
entity_globs:
- sensor.*_debug_*
- sensor.weather_*
Here we are recording data for all entities, except the weather and debug. Events older than 90 days are deleted.
The purge should keep long-term statistics from sensors declaring their metadata correctly, indefinitely with hourly resolution.
8. Rollback plan
If anything went wrong, modify the HA configuration.yaml back to use SQLite or restore the backup.
If Home Assistant failed to start due to an incorrect configuration, the config folder won’t be at /config, you will need to change directory into /mnt/data/supervisor/homeassistant/, modify there the config and then reboot.