前言

  • 原来的Home-Assistant用的是SQLite,想查看里面的数据不太方便,尝试换成MySQL。

情况

  • MySQL是宿主机宝塔装的。
  • HASS是装在docker上的。

更换

查看HASS容器IP和虚拟交换机网关IP

  • 使用命令
docker inspect [容器名NAMES]
[
    {
        /* 上面的省略 */
        
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "12c7d6828f99e40b7fb1e4be3df7ec6fb5c7258beb6ab0ecee5b6b76e49560f2",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8000"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "8000"
                    }
                ],
                "8123/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8123"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "8123"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/12c7d6828f99",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "b024f731f96487cf47aca8bfb3b51836b259cc060d09700a1e35576fb1ece078",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "a86690dd05fd794734e1b5794c10ddc180d14ed10d32ba90b7de4117b8f4e8c2",
                    "EndpointID": "b024f731f96487cf47aca8bfb3b51836b259cc060d09700a1e35576fb1ece078",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

新建数据库

  • 用宝塔或命令新建数据库。
  • 数据库编码用utf8mb4。
  • 访问权限填写:172.17.0.2和127.0.0.1,宝塔新建的话用逗号隔开。

修改HASS配置文件configuration.yaml

  • 添加如下内容:
  • 数据库IP填写刚才找到的网关IP,例如上面是:172.17.0.1
  • purge_keep_days是记录保存的天数。
recorder:
  purge_keep_days: 5
  db_url: mysql://[数据库用户名]:[数据库密码]@[数据库IP]/[数据库名]?charset=utf8mb4

重启HASS

  • 完成
最后修改:2022 年 04 月 19 日
如果觉得我的文章对你有用,请随意赞赏