项目踩坑--ubuntu18.x在docker中安装mysql修改字符集问题(my.cnf配置文件)

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

关于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的字符集:
在这里插入图片描述
已经修改好了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值