Home Assistant 更换 MySQL 和 PostgreSQL 数据库

Home Assistant 默认的数据库是 sqlite,随着使用时间的增加,数据库文件越来越大,查看日志和历史的速度也变得越来越慢。曾经试着把这个 sqlite 数据库文件放到 /tmp 目录下,也就是内存中,响应速度快了很多,但是内存很快就塞满导致死机。

MySQL/MariaDB 和 PostgreSQL 则成为更好性能数据库的选择。强烈推荐使用 PostgreSQL,内存占用少,系统负载更低。

下面以安装在本机的数据库为例,进行简单的配置。

Home Assistant 使用 MySQL/MariaDB
1、安装 MySQL/MariaDB

2、新建数据库

mysql -u root -p

输入密码后,进入 mysql 命令行,

create database hass;

如果你使用 phpMyAdmin 管理 MySQL,新建数据库之类的操作就简单多了。

3、安装依赖软件

pip3 install mysqlclient
4、在 Home Assistant 的配置文件 configuration.yaml 中添加

recorder:
db_url: mysql://user:passwd@SERVER_IP/DB_NAME?charset=utf8

5、监测 MySQL 数据库大小的的 sensor,可根据需要使用

  • platform: sql
    db_url: mysql://user:passwd@SERVER_IP:3306/information_schema
    queries:
    • name: hass_db_size
      query: “select concat(round(sum(DATA_LENGTH/1024/1024),0)) as db_size from TABLES where table_schema=‘HASS’;”
      column: ‘db_size’
      unit_of_measurement: MB

Home Assistant 使用 PostgreSQL
1、安装 PostgreSQL。

2、新建用户和数据库,设置为运行 Home Assistant 的用户名,如 pi、homeassistant 或者 root。

sudo -u postgres createuser homeassistant
sudo -u postgres createdb -O homeassistant homeassistant
3、安装依赖软件

pip3 install psycopg2

4、在 Home Assistant 的配置文件 configuration.yaml 中添加

recorder:
db_url: postgres://SERVER_IP/DB_NAME
#db_url: postgres://127.0.0.1/homeassistant
下面一行是留给你参考的,照葫芦画瓢是行不通的。

5、监测 PostgreSQL 数据库大小的的 sensor,可根据需要使用

  • platform: sql
    db_url: postgres://127.0.0.1/homeassistant
    queries:
    • name: hass_db_size
      query: “SELECT (pg_database_size(‘homeassistant’)/1024/1024) as db_size;”
      column: “db_size”
      unit_of_measurement: MB

https://www.wilf.cn/post/tag/home-assistant

猜你喜欢

转载自blog.csdn.net/weixin_44968310/article/details/91947715