Migrating home assistant database from sqlite to mariadb.. ONLY if you are VERY familiar with Database administration

Hi all,

I’ve also just migrated from sqlite3 to mariadb based on the latest instructions, but on Home Assistant Operating System (HAOS) at the latest versions[1].

Pointers for those wanting to do the same:

Place your family in maintenance mode for a day (took me about half a day of downtime, of which importing the database itself took around 1 hour on a fast PC).

  1. Make & verify backups!

Loss of history between backup and working state after migration.

  1. Gain privileged access to the host on port 22222 (hint: pull a pre-configured SSH authorized_keys file from the home assistant host in console mode).
  2. Get the recorder working with mariadb.
  3. Perform the sqlite3-to-mysql actions on a separate debian or ubuntu system since I see no way to get sqlite3 on the mariadb container nor on the HAOS host.
  4. Copy the sqlite3 database into the host (I used a linux system with scp to pull the database from)
  5. Use docker to copy the database file into the addon_core_mariadb container

Downtime from here on.

  1. Stop all plugins but the SSH one (to free resources for the importing) and most imporantly, stop the core (log in using console or ssh into port 22 and execute ha core stop

  2. Import (e.g., use docker exec -it addon_core_mariadb bash
    to gain access to the mariadb container (this took over an hour on a proxmox VM allocated 4 cores of a AMD Ryzen 7 5800X CPU, so take this into account please. You can check if it is still running based on higher than normal CPU load using a parallel SSH session running docker stats or docker stats addon_core_mariadb. This is for 2 years of active HASS use with 800k records in tables ‘events’ and ‘states’ combined.

  3. Execute script as updated by Ignacio less than a week ago from this post.

WARNING: manually run the SELECT MAX statement for each table, add 1 to each number and replace ‘N+1’ with it before running the ALTER TABLE commands.

  1. Reboot and check for anomalies in logfiles, and, confirm that the history goes back to before the migration!

End of downtime, all good.

NB I do not have time to write a full guide, but I think that if you are interested in similar outcome you have roughly two options:

  1. Figure it out with the work and information from this post (which is perfectely doable with the information in this post if you have time and if you are interested in puzzling/learning).
  2. Wait. At some point, hopefully, there will be a migration path in the GUI that allows for these steps to take place in an automated fashion. I mean, it is a reliable process just someone needs to automate it in a future proof way.

Also, I’ll post back in a few weeks also if all remains OK for those more conservative amongst us.

[1] versions used:
Home Assistant 2022.8.6
Supervisor 2022.08.3
Operating System 8.5
Frontend 20220802.0 - latest

2 Likes

Thank you for your post. I updated my post following your suggestions.

Just one important note for every one else. I’ve HA on a RPI and I’ve put the database on a separate Linux Server. Why? because running a database like MariaDB on a SD card in a small RPI does not seems to be a good idea. For all the other tasks it is OK, except for influxDB that is another database I’ll migrate from the RPI to the Linux box.

Cheers
Ignacio

1 Like

Why? Does MariaDB or the addon perform more write cycles to the SD card than SQLite?

I doubt it. Using a SSD is in general a good idea. On the other hand: I run HA including InfluxDB on a Pi with a (Extreme) sd card for quite a while.

Well, that’s not exactly my point. I wanted to migrate the database out of the RPI for performance reasons. And due to the reason I will run the storage service in a separate server I’d no reason to keep using sqlite.

Okay that’s more an advanced or even semi-pro style of driving your data warehouse. Most of us are happy to run it on the same machine as HA. Less complexity is always a good thing.

/side discussion over :slight_smile:

I need to do the same migration. Events has 2 258 466 rows. The sqlite db is on schema_version 9. A new install is on schema_version 29. Recorder purge never worked apparently. Home assistant is running on the same NUC for 2 years…
importing haimport.sql is at 300000 rows after 2 hours. It would need several days to import the complete database
Tried restoring a backup on a new NUC with the latest Home Assistant 2022.8.7. The result is a complete disaster and a new(empty) home_assistant_v2.db

Dumped the statistics with
Sqlite3
sqlite> .output statistics.sql
sqlite> .dump statistics
and the other tables

Lets see if I can save the statistics

I really don’t understand why they didn’t opt for a second database for the statistics.

To those who have moved to mariadb, do you also experience the logbook failing to load events?

And, what could point into a troubleshooting direction please?

Today I also moved my Home assistant 2022.9.4 to MariaDB - Migration went (besides a couple of columns had a wrong datatype or missing index) well, but I also have the Energy Dashboard problem. My Graph looks quite good, and the measurements are also correct (either on UI and in DB) but as mentioned above my costs are ridiculous… 1 kWh is 86.63€ (even for me, living in Germany is the price a bit high ^^)
But it seems that the prices don’t have anything in common with the consumption… Like in the picture now (a couple of hours later) - I now have consumed 3.96 kWh, but the price is only “a bit higher” (8€ for the next 2 kWh)…

Did anyone find a solution for this? - I’ve tried creating a new sensor, but there is also the same problem. Even a new Migration did not solve this :confused:.
I even noticed that the “Non-Fossil energy consumed” Chart may be a bit too high – normally I’he more like 50 - 60% but never before 76% Percent…

Hope anyone already solved this issue :slight_smile:

I think the energy dashboard uses its own (hidden) sensors for keeping track of power usage and costs of provided input sensors. No idea where they are stored, just a wild guess…

I have a question for you - if the DB server is down, does HASS still work fine? I like your approach but if my main server dies (the one that would run the sql) i really don’t want the home to stop working well since I have a dedicated pi for HASS. Have you tested this? And if so, what happens when teh sql server comes back up?

@drknow if the DB server is down, the “recorder” service raises errors and some integrations might not work as expected, but other areas of HASS still works. For example, in my case I’m using mostly zigbee devices using the phoscon.de integration and hardware (in a separate RPi) that RPI is still sending events (via sockets connection) and the automations are fired so yes, they work. The problem is only that the “state” is not stored regularly to the database and the statistics will not collect all state changes

Taking the database apart from the same machine HA is running apparently introduces more dependencies and increases the possibility of things breaking. A half-working HA „just“ because the other machine hosting the database is gone for a moment (maybe due to network hiccups) is nothing I am looking forward, not speaking bout performance (network speed / latency etc.). Of course if you have perfect environment it might work really well, maybe even better than having everything in the same box.

Just my opinion, which is why I‘m personally not going to outsource my database but instead keep it running on the same host (HA OS).

But here it’s about migration (from SQLite to MySQL/MariaDB) :slight_smile:

1 Like

Today i migrate my SQLite DB to Maria DB. The convert neet 10 hours.
Export and Import without Problem.
Nice tutorial.

1 Like

Hi,
I do not have any issue. Only one was utf8mb4_unicode_ci what I described.
Since I did migration I work on the same instance without any problem.

Hi,
I do not have any issue. Only one was utf8mb4_unicode_ci what I described. Since I did migration I work on the same instance without any problem.

Same problem here. Please keep us posted on a solution for this home assistant specific energy crisis :wink:

Yea, energy cost calculations got messed up for me after migration to MariaDB. It now just accumulates all the sums, instead of all the differences. It’s possible to recalculate the history in the database, but it still continues to calculate wrongly. I posted a more detailed reply here.

Now I’ve just let it be, since I’ve already spent a day or two trying to fix it.

Thank you all for sharing. That’s obviously a no-go for some users willing to migrate, but not accepting the loss of LTS / energy dashboard data. Hope you get it fixed somehow.

Maybe all of you affected can summarize what migration method you used so that @jr3us perhaps can add a disclaimer/warning sign in the original post. Nobody lucky to find these great migration how-tos will be happy finding out the energy dashboard in the end got messed up.

This sounds like the type of data for longterm statistics
‘state_class: total_increasing’

got messed up during copy and maybe altered the type of state class .

do you know if there’s a way to update the state_class? these seem to be hidden entities