docker 文件方式修改mysql配置(时区+字符集)

永久修改Docker容器中mysql的时区

对mysql docker image 5.7 ~ latest 都有效

进入已运行成功的docker 镜像

docker exec -it mysql bash

查看数据库时区配置:

show variables like'%time_zone';

查看数据库时区结果:

mysql> show variables like "%time_zone";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | SYSTEM |
+------------------+--------+

查看数据库字符集配置:

SHOW VARIABLES LIKE 'character%';

查看数据库字符集结果:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | latin1                         |
| character_set_connection | latin1                         |
| character_set_database   | latin1                         |
| character_set_filesystem | binary                         |
| character_set_results    | latin1                         |
| character_set_server     | latin1                         |
| character_set_system     | utf8                           |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+

升级apt包管理工具(理由:不升级不能下载vim)

apt-get update

apt升级完成后下载 vim

apt-get install vim

vim修改docker.cnf 或者mysql.cnf镜像文件都会生效,任意修改一个即可

vim /etc/mysql/conf.d/docker.cnf

或者:

vim /etc/mysql/conf.d/mysql.cnf

文件中默认显示(下文中皆是以docker.cnf为例):

[mysqld]
skip-host-cache
skip-name-resolve

增加时区配置:东八区(中国)

[mysqld]
default-time_zone = '+8:00'

增加数据库服务端utf8字符集配置

[mysqld]
character_set_server=utf8

增加数据库客户端utf8字符集配置

[client]
default-character-set=utf8

增加完成后显示为:

[mysqld]
skip-host-cache
skip-name-resolve
default-time_zone = '+8:00'
character_set_server=utf8

[client]
default-character-set=utf8

配置完成保存,重启数据库(5.7有效)

service mysql restart

**建议退出容器直接重启容器(因为使用重启mysql后还是需要启动镜像的,倒不如直接退出容器直接重启镜像)

docker restart mysql

再次进入docker image mysql查看是否修改成功

时区查看:

show variables like "%time_zone";

时区查看结果:

mysql> show variables like "%time_zone";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | +08:00 |
+------------------+--------+

字符集查看:

SHOW VARIABLES LIKE 'character%';

字符集查看结果:

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8mb3                        |
| character_set_connection | utf8mb3                        |
| character_set_database   | utf8mb3                        |
| character_set_filesystem | binary                         |
| character_set_results    | utf8mb3                        |
| character_set_server     | utf8mb3                        |
| character_set_system     | utf8mb3                        |
| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值