-
-
Save seidler2547/93012edf3c7a2414ec1d9a8ebbc9c1a6 to your computer and use it in GitHub Desktop.
# prerequisites: | |
## install software | |
apt install mariadb-server libmariadbclient-dev sqlite3 | |
## install mysqlclient in virtualenv | |
su -c 'homeassistant/bin/pip3 install mysqlclient --upgrade' -l homeassistant | |
## create database | |
mysql -e 'CREATE SCHEMA IF NOT EXISTS `hass_db` DEFAULT CHARACTER SET utf8' | |
## create user (use a safe password please) | |
mysql -e "CREATE USER 'hass_user'@'localhost' IDENTIFIED BY 'hass_pw'" | |
mysql -e "GRANT ALL PRIVILEGES ON hass_db.* TO 'hass_user'@'localhost'" | |
mysql -e "GRANT usage ON *.* TO 'hass_user'@'localhost'" | |
# stop HA now | |
systemctl stop home-assistant # or whatever it is for you | |
# now edit the configuration to point hass to mysql | |
nano .... | |
# now start HA once and stop it right away, we only want it to create the tables: | |
systemctl start home-assistant # or whatever it is for you | |
sleep 20 | |
systemctl stop home-assistant # or whatever it is for you | |
# now empty the tables | |
mysql hass_db -e 'delete from events;delete from recorder_runs; delete from schema_changes; delete from states;' | |
# this is the actual conversion: | |
sqlite3 home-assistant_v2.db .dump \ | |
| sed -re 's/^PRAGMA .+OFF/SET FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS=0/' \ | |
-e 's/^CREATE INDEX .+//' \ | |
-e 's/^BEGIN TRANSACTION;$/SET autocommit=0;BEGIN;/' \ | |
-e '/^CREATE TABLE .+ \($/,/^\);/ d' \ | |
-e 's/^INSERT INTO "([^"]+)"/INSERT INTO \1/' \ | |
-e 's/\\n/\n/g' \ | |
| perl -pe 'binmode STDOUT, ":utf8";s/\\u([0-9A-Fa-f]{4})/pack"U*",hex($1)/ge' \ | |
| mysql hass_db --default-character-set=utf8 -u hass_user -p |
I used this perl command and had no issues with the utf8 encoding of any symbols, degrees, cosine, ...
perl -pe 'use utf8; use open qw(:std :utf8); binmode STDOUT, ":utf8mb4";s/\\u([0-9A-Fa-f]{4})/pack"U*",hex($1)/ge'
Im very novice here...
I'm using HAOS and have MariaDB addon installed.
It looks like the instructions install mariaDB at HAOS root? Im not sure how to SSH into the addon.
Is this the correct way to migrate for HAOS users?
Is it possible for detailed steps for a real novice HAOS users, like myself?
Aside from that, I had problems converting unicode characters - a lot of entries in
states
had unicode\u00b0C
that was copied as?C
for example, but anupdate
/replace
query sorted that out..
@kaitlinsm Could you give an example of what this query looked like?
I ended up using the sqlite3-to-mysql python script
So far it seems to work well.
Also utf8mb4 seems to be default on my ubuntu server setup
# prerequisites:
## install software
apt install mariadb-server
## create database
mysql -e 'CREATE SCHEMA IF NOT EXISTS `hass_db` DEFAULT CHARACTER SET utf8mb4'
## create user (use a safe password please)
mysql -e "CREATE USER 'hass_user'@'localhost' IDENTIFIED BY 'hass_pw'"
mysql -e "GRANT ALL PRIVILEGES ON hass_db.* TO 'hass_user'@'localhost'"
mysql -e "GRANT usage ON *.* TO 'hass_user'@'localhost'"
# stop HA now
docker stop homeassistant # or whatever it is for you
# now edit the configuration to point hass to mysql
nano ....
# convert the sqlite db to mysql
sqlite3mysql -f ./home-assistant_v2.db -d hass_db -u hass_user -p
# start HA
docker start homeassistant # or whatever it is for you
I ended up using the sqlite3-to-mysql python script So far it seems to work well. Also utf8mb4 seems to be default on my ubuntu server setup
# prerequisites: ## install software apt install mariadb-server ## create database mysql -e 'CREATE SCHEMA IF NOT EXISTS `hass_db` DEFAULT CHARACTER SET utf8mb4' ## create user (use a safe password please) mysql -e "CREATE USER 'hass_user'@'localhost' IDENTIFIED BY 'hass_pw'" mysql -e "GRANT ALL PRIVILEGES ON hass_db.* TO 'hass_user'@'localhost'" mysql -e "GRANT usage ON *.* TO 'hass_user'@'localhost'" # stop HA now docker stop homeassistant # or whatever it is for you # now edit the configuration to point hass to mysql nano .... # convert the sqlite db to mysql sqlite3mysql -f ./home-assistant_v2.db -d hass_db -u hass_user -p # start HA docker start homeassistant # or whatever it is for you
This method worked for me. Awesome. You're a star.
If you are getting error:
2022-07-12 09:32:29 ERROR 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
Let´s try this way:
sqlite3mysql -f ./config/home-assistant_v2.db -d hass_db -u hass_user -h core-mariadb -p
I ended up using the sqlite3-to-mysql python script So far it seems to work well. Also utf8mb4 seems to be default on my ubuntu server setup
# prerequisites: ## install software apt install mariadb-server ## create database mysql -e 'CREATE SCHEMA IF NOT EXISTS `hass_db` DEFAULT CHARACTER SET utf8mb4' ## create user (use a safe password please) mysql -e "CREATE USER 'hass_user'@'localhost' IDENTIFIED BY 'hass_pw'" mysql -e "GRANT ALL PRIVILEGES ON hass_db.* TO 'hass_user'@'localhost'" mysql -e "GRANT usage ON *.* TO 'hass_user'@'localhost'" # stop HA now docker stop homeassistant # or whatever it is for you # now edit the configuration to point hass to mysql nano .... # convert the sqlite db to mysql sqlite3mysql -f ./home-assistant_v2.db -d hass_db -u hass_user -p # start HA docker start homeassistant # or whatever it is for you
This is exactly what i need, work without any problem (debian 11) (HA 2024.10.3)
sqlite3mysql version 2.3.1
sqlite3mysql --sqlite-file home-assistant_v2.db --mysql-user user --mysql-password pass --mysql-database dbname
ha config:
recorder:
db_url: mysql://user:pass@ip/dbname?charset=utf8mb4
auto_purge: false
Thanks!
@kaitlinsm I've just done this approach for to migrate my recorder DB to MariaDB. I've got an current hass.io docker install. My
states
table doesn't include the fieldscontext_id
orcontext_user_id
, so I guess that these have been added by some customisation.UTF-8 encoding uses multiple bytes per UTF character with core ASCII taking one and emoticons 4. The MySQL / MariaDB implementation of the "utf-8" is actually an invalid implementation in that it can't handle the 4 byte UTF codes correctly; however the name has been retained for backwards compatibility. "utf8mb4" really means utf-8 correctly implemented. AFAIK, perl utf8 output loads correctly into
utf8mb4
collated fields. I certainly couldn't find any probs with my field names (including ° C).A couple of other points for HassOS users:
sqllite3
isn't available in the mariadb container so you need to do the unload by SSHing to the kernel. The/config
folder is pathed at/mnt/data/supervisor/homeassistant
in the kernel. So after stopping the supervisor, I did:/ha.sql
. This container does have a propersed
and perl 5.30.secrets.yaml
, installed the mariaDB container, configured thehomeassistant
password and started mariaDB to create thehomeassistant
DB. I did aha core stop
added thedb_url: !secret mariadb_url
to myrecorder
entry, and did aha core start
<wait>ha core stop
to allow HA to create the reporter tables in mariaDB.mysql -u root homeassistant
to truncate all 4 tables. (You will need toSET FOREIGN_KEY_CHECKS=0
first otherwise truncatingstates
will fail.)mysql
, then cleanup crapsql
files and exit the container.