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

I think you mean : Created a database hass

Thank you, I have corrected the original post as well as the recent post to create database hass.

I’m facing some issues doing this…

Some of my sensors give an error with regards to the unit of measurement which seem to be broken during dump or import…

2022-01-27 17:52:12 WARNING (MainThread) [aiohue.util] Value {} of type <class 'dict'> is invalid for Device.identify, expected value of type typing.Optional[aiohue.v2.models.feature.IdentifyFeature]

2022-01-27 17:52:12 WARNING (MainThread) [aiohue.util] Value {} of type <class 'dict'> is invalid for Device.identify, expected value of type typing.Optional[aiohue.v2.models.feature.IdentifyFeature]

2022-01-27 17:52:12 WARNING (MainThread) [aiohue.util] Value {} of type <class 'dict'> is invalid for Device.identify, expected value of type typing.Optional[aiohue.v2.models.feature.IdentifyFeature]

2022-01-27 17:52:12 WARNING (MainThread) [aiohue.util] Value {} of type <class 'dict'> is invalid for Device.identify, expected value of type typing.Optional[aiohue.v2.models.feature.IdentifyFeature]

2022-01-27 17:52:12 WARNING (MainThread) [aiohue.util] Value {} of type <class 'dict'> is invalid for Device.identify, expected value of type typing.Optional[aiohue.v2.models.feature.IdentifyFeature]

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.bathroom_temperature has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.bedroom_temperature has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.oneplus_8_pro_battery_temperature has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.humidifier_energy_factor (Cos u03c6) does not match the unit of already compiled statistics (Cos φ). Generation of long term statistics will be suppressed unless the unit changes back to Cos φ

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.bathroom_energy_factor (Cos u03c6) does not match the unit of already compiled statistics (Cos φ). Generation of long term statistics will be suppressed unless the unit changes back to Cos φ

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.dryer_energy_factor (Cos u03c6) does not match the unit of already compiled statistics (Cos φ). Generation of long term statistics will be suppressed unless the unit changes back to Cos φ

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.hallway_energy_factor (Cos u03c6) does not match the unit of already compiled statistics (Cos φ). Generation of long term statistics will be suppressed unless the unit changes back to Cos φ

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.servers_energy_factor (Cos u03c6) does not match the unit of already compiled statistics (Cos φ). Generation of long term statistics will be suppressed unless the unit changes back to Cos φ

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.christmas_tree_energy_factor (Cos u03c6) does not match the unit of already compiled statistics (Cos φ). Generation of long term statistics will be suppressed unless the unit changes back to Cos φ

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.diskstation_temperature_1 has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.diskstation_temperature_2 has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.diskstation_temperature_3 has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.diskstation_temperature_4 has unit u00b0C which is unsupported for device_class temperature

2022-01-27 17:52:21 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.diskstation_temperature has unit u00b0C which is unsupported for device_class temperature

Anyone managed to get onto something here?
I’m soon planning to migrate since I want more long term statistic and I have mySQL already running on server that is running HASS.

I have no idea who is able to sort out the remaining issues. I still don’t feel confident enough to migrate using this guide (still be best! but not error-free unfortunately) because reliability is a must. :frowning:

What about using DBeaver for migration - could it be useful?

Heard about it here (Energy Management in Home Assistant - #1156 by ChirpyTurnip) first and saw few others like here (Home Assistant Add-on: PostgreSQL + TimescaleDB - #22 by Ecard) using this software for migrating to PostgreSQL.

Maybe it’s just another tool to achieve the same like on the CLI as shown in former posts.

Seems like Download DataGrip: Cross-Platform IDE for Databases & SQL works just fine with converting sqlite to mysql.
I’ve set it up and am copying sqlite to mysql and HASS seems to work fine with it. Will test now once my second transfer is complete.
First I tested it and it was all seemingly working but now I’m converting the most up to date DB and will continue to use mysql for the following day and see how it goes.

1 Like

Does the trial version do the trick totally or does one need a license? In other words, any limits on fucntionality, database size, amount of records?

Great to hear! I did some further research and I think those information would guide us to a successful migration:

And it also seems to be possible to migrate back/vice versa (MySQL to SQLite).

Keep us posted what your results are @zagi988. Especially if Energy Dashboard and other things (noted as malfunctioning in the former posts) are working fine.

1 Like

I’m trying to install mysql but I always get this error
sudo: apt: command not found

Results? Working?

I did an initial trial yesterday, and I find the import in mariadb to be extremely slow . My DB was 484491264 bytes, the sql dump 368643027 bytes (less!), the compressed sql for mariadb 44741761 bytes (1/10th).

FYI, I found another record of a conversion: Migrating Home Assistant from sqlite3 to MySQL 8.0 .

I do not want to have a long down time, I think we need to use features offered by sqlite3 that we see at How To Use The SQLite Dump Command and split up the process in steps to limit down time.

  1. Copying the DB to a workstation (i.e. a faster computer with easy access).
    • while the system is live is doable - I’ld prefer rsync but I do not have it on my HAOS.
  2. Prepare the import on the workstation:
    • Create SQL script to create the SCHEMA (all the tables);
    • Export the most recent data needed to continue regular functionnality
      • Use “.mode insert” and appropriate selects to create the inserts.
      • Most recent data to be inserted first.
      • use “INSERT OR UPDATE” rather then “INSERT”
    • Export all the data (without the SCHEMA) to another file.
      • Most recent data to be inserted first.
      • use “INSERT OR UPDATE” rather then “INSERT”
      • optimize by grouping the inserts/using transactions.
      • IGNORE FOREIGN KEYS.
      • set autoincrement numbers to biggest value + margin .
        The margin allows HA to continue adding some data after the import.
  3. Perform a first import without bringing HA offline.
    • Apply the schema;
    • INSERT/UPDATE the most recent data.
    • This way the target database will already have an image.
    • One could test the data base with a test HA instance on the workstation to see everything is fine.
      (In that case, it may be needed to restart the entire import after testing because HA will add data).
    • Stop HA
    • Do step 2 again, but skip step 3 and continue here.
    • Import the small file with the most recent data;
    • Update the HA configuration
    • start HA;
    • Import the big file while HA is online.
      Most of the data will already be imported, so the user already has the previously imported history.
      There may be a gap which corresponds to the delay between the first import and the final import.
      The insert/update will ensure only the changes are applied.

Automate all of the above.
I started a script here: https://gist.github.com/38854d24863c1081154cf08d75e6535a . It does not include my proposed procedure above.

2 Likes

For me it seems to be working. Energy dash was ok, everything was ok only that history was slooow.
mySQL is running on a server that holds the database on HDD’s that are not that fast and it was either unable to process that much at the time or it was the hard drives being too slow.
So I switched back to sqlite until SSD for that server arrives.

If history is still slow, the slow queries could be analysed to add indexes to speed them up.

I have been trying to migrate using the steps at the top but because its based on an old database schema and in 2022.4 things are a bit different people will have issues.

I am writing notes as I fix the issues I find but there are a few extra gotchas FYI all.

6 Likes

@sudo Any update?

1 Like

Meanwhile there have been further changes related to the database scheme. According to release notes of 2022.4 to 2022.6 e. g. attributes and events have been outsourced from states to separate tables. All that needs to be considered to get a stable, working MySQL database after conversion.

So yes, double the question here Migrating home assistant database from sqlite to mariadb - #88 by WeterPeter :slight_smile:

Hi,
I have found quite simple solution to do that if you have already running somewhere mysql database

  1. Stop home assistant and take backup

  2. Take file home-assistant_v2.db and convert SqlLite to Mysql using sqlite3-to-mysql (python sqlite3-to-mysql · PyPI) - it transfer whole database to mysql database

  3. Export data from mysql server - I used MySql Workench to export data to file (set Include Create Schema)

  4. I had to replace in all files utf8mb4_unicode_ci to utf8mb4_general_ci because I had in first some errors during import related with different database versions.

  5. Start fresh mariaDb addon in hass with exposed port to outside world

  6. Import data to MariaDb - I used MySql Workench to import data from file.

  7. Enable mariadb url in recorder in configuration.yaml

recorder:
  db_url: !secret mariadb_url

In the end I did not have to think about foreign key and my long term statistics works fine. I have not noticed any problem however I have finished my migration today few minutes ago :slight_smile:



10 Likes

Hi Mariusz, we are a few minutes further ahead in time :slight_smile: Did you notice any problems that you care to mention for those interested in migrating as well?

1 Like

Also very interested in this because… SQLite database s**ks a lot meanwhile.

1 Like