关于ubuntu18.x下在docker中安装mysql5.7字符集问题
先进入docker容器中(指的是mysql容器)
命令:
docker exec -it mysql bash
上面命令写mysql的位置 是你mysql容器的名字,我的容器名字是mysql,不知道自己容器名字的可以用 docker images查看
然后成功进入容器内,和之前终端控制台使用mysql一样,输入:
mysql -uroot -p
进入mysql界面,再输入:
show variable like 'char%';
可以看到docker中安装的mysql5.7默认字符集是latin1:

划重点:想要让mysql的字符集改变成utf8,并且重启仍然有效,就要修改mysql的配置文件
那么重点来了,网上很多方法都是让修改那个神秘的my.cnf配置文件,可是当我们进入到docker容器中,去/etc/mysql/目录下去找my.cnf这个文件的时候,会发现并不存在,还有的博客说在/usr/share/目录下,反正我是翻遍了docker的mysql容器,确实找不到my.cnf文件。
那么,没有文件怎么办? 那就自己创建一个:
首先进入到mysql文件夹:
cd /etc/mysql
然后创建一个my.cnf文件:
touch my.cnf
然后编辑my.cnf文件:
vim my.cnf
这个插一句:
docker 容器中是没有vim命令的,想要vim,按照以下步骤:
首先
apt-get update
然后
apt-get install vim
然后就可以编辑my.cnf文件了
那么具体的my.cnf文件的内容贴在下面,直接copy进去就好了:
# Copyright (c) 2014, 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
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character_set_server=utf8
init_connect='SET NAMES utf8'
max_allowed_packet = 20M
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
配置好之后,退出容器,重启docker中的mysql
再次进入docker容器,再次查看mysql的字符集:

已经修改好了
本文主要介绍了在ubuntu18.x的docker环境下,如何解决mysql5.7默认字符集为latin1的问题,强调了要使字符集修改为utf8并保持重启有效,需修改my.cnf配置文件。由于在docker容器中找不到my.cnf,因此需要手动创建并编辑该文件,确保配置正确后重启mysql服务,最终成功将字符集更改为utf8。
&spm=1001.2101.3001.5002&articleId=117174064&d=1&t=3&u=ffe1a8b73ad7400b80e79fe683fea9e8)
2246

被折叠的 条评论
为什么被折叠?



