docker安装mysql执行now()函数,时差为8小时

发布时间 2023-03-29 23:53:29作者: Realife

经过一番查询最最直接的方式是:在my.conf文件中的[mysqld]下添加

default-time-zone=Asia/Shanghai

由于mysql是通过 docker 安装的,当时配置文件采用的是挂载的方式。所以不需要进入mysql容器内修改配置。修改挂载文件就行。修改完重启容器会自动同步到mysql容器内。
查看mysql 容器挂载目录:

[root@VM-4-5-centos ~]# docker inspect mysql | grep Mounts -A 24
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/docker/mysql/conf",
                "Destination": "/etc/mysql/conf.d",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/docker/mysql/logs",
                "Destination": "/var/log/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/docker/mysql/data",
                "Destination": "/var/lib/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }

根据查询可以知道挂载目录在/docker/mysql/conf下,添加并重启容器,修改成功。

mysql> select now(), curdate(), curtime();
+---------------------+------------+-----------+
| now()               | curdate()  | curtime() |
+---------------------+------------+-----------+
| 2023-03-29 23:40:30 | 2023-03-29 | 23:40:30  |
+---------------------+------------+-----------+
1 row in set (0.00 sec)