How to convert Home Assistant database to MariaDB

How to convert Home Assistant database to MariaDB


This video is a tutorial on how to convert the default Home Assistant database to MariaDB. Here are a few links to get you started..... As

Chiêm Ngưỡng "SIÊU BIỆT THỰ" 400m2 trị giá 62 Tỷ Đồng tại Vinhomes The Harmony – NhaF [4K]
Thuỷ Tiên – Công Vinh trao tiền công khai gần 1 tỷ cho hơn 600 công nhân

This video is a tutorial on how to convert the default Home Assistant database to MariaDB.

Here are a few links to get you started…..

As always, make sure your system is up to date…

sudo apt-get update
sudo apt-get upgrade

Lets start installing everything…..

sudo apt-get install software-properties-common

sudo apt-key adv –recv-keys –keyserver hkp:// 0xF1656F24C74CD1D8

sudo add-apt-repository ‘deb [arch=amd64,i386,ppc64el] xenial main’

Now lets run update again and then install MariaDB…

sudo apt-get update
sudo apt-get install mariadb-server

It will prompt you for a default admin password to use with MariaDB

Once thats done, lets install a few more pieces needed for this to work with HA….

sudo apt-get install libmariadb-client-lgpl-dev libssl-dev
pip3 install mysqlclient

In order to convert the existing db over we need to install sqlite3

sudo apt-get install sqlite3

Now we are ready to stop Home Assistant to start working with the db…
sudo systemctl stop home-assistant.service

First we need to do a dump of the db……

sqlite3 home-assistant_v2.db .dump (ANGLE BRACKET) sqlite3_dbfile_.dump.sql

Now we need to download the sqlite to mysql converter….

git clone
cd sqlite3-to-mysql/
sudo ./sqlite3-to-mysql ../sqlite3_dbfile_.dump.sql (ANGLE BRACKET) ../mysql_importme.sql

Now we are ready to create the db in Mariadb…

mysql -u -root -p
create database homeassistant;

grant all privileges on homeassistant.* to ‘hass’ identified by ‘PASSWORD’

Now we are ready to import the converted db into our newly created db…..

mysql -u hass -p -h localhost homeassistant (ANGLE BRACKET) mysql_importme.sql

Once that is done importing, we need to make a few tweaks….

mysql -u hass -p
use homeassistant
select max(run_id) from recorder_runs;


alter table states drop foreign key states_ibfk_1;

select max(event_id) from events;

alter table events modify column event_id int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=183522; (NUMBER RETURNED PREVIOUSLY **INCREASED BY 1)

Now we can update configuration.yaml file…..

db_url: mysql://hass:PASSWORD@localhost/homeassistant

Now that all the changes have been made, start HA back up….

sudo systemctl start home-assistant.service

That should be it, you should now be up and running on the mariadb database.

If you like the video, please subscribe to my channel. If you have any questions, hit me up in the comments below. Also, if there are any videos you would like me to put out here, let me know as well.



  • comment-avatar

    Can I delete a home-assistant_v2.db file after that?

  • comment-avatar

    greetings, It looks mostly good after performing the update, and home assistant does indeed connect. I did find a problem though with JSON Decoder errors indicating that for example, "Expecting property name enclosed in double quotes" . I noticed that the shell script sqlite3-to-mysql converts all double quotes to backquotes(`) for the conversion and the data in the tables events and states reflect this. I am thinking that to fix this, there needs to be something like the following to fix it:
    update events set event_data = REPLACE(event_data, '`', '"');
    update states set attributes = REPLACE(attributes, '`', '"');


    I found that the index current value needs to also be set for the states table. I needed to add the following to my instance:

    elect max(state_id) from states; (IT SHOULD RETURN A NUMBER)
    alter table states modify column state_id int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=183522; (NUMBER RETURNED PREVIOUSLY **INCREASED BY 1)

     would you want to add this to the instructions above?


  • comment-avatar

    How can maintain the ON and OFF switch status in Hassbian? Once I restart my raspberry pi 3 then I lost all the lights / switch status. Is there any way to keep in as it was before restarting?

  • comment-avatar

    You didn't add the foreign key back. I see there is a typo in the original article, should be:

    ALTER TABLE states ADD CONSTRAINT states_ibfk_1 FOREIGN KEY (event_id) REFERENCES events (event_id);

  • comment-avatar

    Im having this error after the database is implemented "Error converting row to state: <homeassistant.components.recorder" any idea why?

  • comment-avatar

    Nice video pip3 install mysqlclient give me 1 error i cant make it work! Im using virtual env in hassbian!

  • comment-avatar

    Great video
    On hassbian (Strech) got following error:
    gpg: failed to start the dirmngr '/usr/bin/dirmngr': No such file or directory
    gpg: connecting dirmngr at '/tmp/apt-key-gpghome.wfF9asVRTy/S.dirmngr' failed: No such file or directory
    gpg: keyserver receive failed: No dirmngr

    Fixed it with:
    sudo apt remove gnupg
    sudo apt install –reinstall gnupg2
    sudo apt install dirmngr

  • comment-avatar

    How can you hide the password from the DB URL in the recorder: part? I guess !secret won't work right?

  • comment-avatar

    Please make the video How to convert Home Assistant database to MariaDB for Hassbian as well.

  • comment-avatar

    Why change databases any advantages?

  • comment-avatar

    Adrian, as always great tutorial. What are the advantages of using Maiadb (Mysql or Percona) , vs sqllite. Is there a significant advantage to doing the migration.

    As far as Im concerned, you can say , "Cause I say SO" and that is good enough for me.

  • comment-avatar

    I looked over the documentation for MariaDB after watching your video. Would this allow HA to be configured for multiple logins for different users and have the UI customized per user? Or at least hide some things from a guest user? – and maybe a follow up tutorial to show how?

  • comment-avatar

    hey man thanks for the video, at the start of your videos can you give a brief explanation of what are the advantages of doing this and why somebody would do it? thanks again

  • DISQUS: 0