Docker下修改 MySQL5.7 字符集配置

时间:2024-03-28 10:33:44

Docker下修改 MySQL5.7 字符集配置

保存中文乱码问题。

 

配置遇坑,首先网上找了好几篇文章,找了个觉得靠谱的。

1、于是乎查看mysql容器ID

[[email protected] bin]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d3633491bc51        mysql:5.7.17        "docker-entrypoint..."   7 months ago        Up 3 days           0.0.0.0:3308->3306/tcp   mysql5.7

 

2、根据容器ID或者名称,进入mysql容器

[[email protected] bin]# docker exec -i -t mysql5.7 /bin/bash
[email protected]:/# 

 

3、查看容器中mysql的配置,找到mysql.conf.d

[email protected]:/# cat /etc/mysql/my.cnf
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[email protected]:/# 

 

4、进入到mysql.conf.d文件夹

[email protected]:/# cd /etc/mysql/mysql.conf.d/
[email protected]:/etc/mysql/mysql.conf.d# ls
mysqld.cnf

 

5、vim修改配置文件 mysqld.cnf 主要的内容就是 default-character-set=utf8!!(就是这个配置出了问题,由于是mysql5.7版本的,所以这样配置字符集是会报错的,这是低版本的配置。)

--docker bash: vi: command not found
参考: https://blog.csdn.net/weixin_39800144/article/details/79231002

 

6、重启容器,docker restart mysql5.7

7、然后使用 docker ps,状态一直处于 restarting中,再次使用exec想进入容器mysql容器就报错。Error response from daemon: Container is restarting, wait until the containe

8、查看docker log,发现原来是配置错误了。

[[email protected] ~]# docker logs --since="2019-03-10" --tail=100 d3633491bc51
2019-03-10T06:59:02.365549Z 0 [ERROR] Aborting
ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"
2019-03-10T06:59:02.867515Z 0 [ERROR] unknown variable 'default-character-set=utf8'
2019-03-10T06:59:02.872421Z 0 [ERROR] Aborting
ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

9、运行配置帮助指令 mysqld --verbose --help

10、可是这时候,进入不了mysql5.7的容器位置,怎么修改mysqld.cnf呢?想到了物理的路径。

11、停止mysql5.7容器 docker stop mysql5.7

11、进入docker的目录

[[email protected] ~]# cd /var/lib/docker
[[email protected] docker]# 

12、搜索mysqld.cnf,找到好几个,自己进去判断是否是修改过的文件。

[[email protected] docker]# find / -name mysqld.cnf
/var/lib/docker/overlay2/29007ab67b529721cb023b06565caa9aeec072a5b26afdc7de8a47d602b1151b/diff/etc/mysql/mysql.conf.d/mysqld.cnf
/var/lib/docker/overlay2/e86ddd35a918e6b91823534047213974f1c8e2394edef6df769c381a26ea77c7/diff/etc/mysql/mysql.conf.d/mysqld.cnf
/var/lib/docker/overlay2/286d17928812548bde9d35550b5ca9041850667078f39f538ecb3096f6fd532a/diff/etc/mysql/mysql.conf.d/mysqld.cnf
/var/lib/docker/overlay2/286d17928812548bde9d35550b5ca9041850667078f39f538ecb3096f6fd532a/merged/etc/mysql/mysql.conf.d/mysqld.cnf
[[email protected] docker]# 

13、修改 mysqld.cnf文件,将default-character-set=utf8 相关配置删除,增加如下配置。

# 服务端字符集
character-set-server=utf8
collation-server=utf8_general_ci
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 表名不区分大小写
lower-case-table-names=1

14、重启 docker restart mysql5.7

 

问题解决。

Docker下修改 MySQL5.7 字符集配置