docker安装部署mysql并修改中文配置(mysql5.7)

docker安装部署mysql并修改中文配置(mysql5.7)

1.拉取镜像

docker pull mysql:5.7.18

2.查看镜像

docker images

3.运行docker容器

docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.18

4.查看容器是否启动成功

docker ps

5.修改容器随docker启动自启动

docker update --restart=always 容器ID

6.进入容器

docker exec -it 容器ID /bin/bash

7.查看配置

more /etc/mysql/mysql.conf.d/mysqld.cnf

**注意:**如果执行该语句提示没有该文件,先看完下面的内容 自行酌情处理

8.编辑配置

8.1方式一:将配置从容器中copy到宿主机,在宿主机编辑完成后copy回容器中

docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /root/mysqld.cnf

**注意:**如果没有该文件,那这一步省略,直接在宿主机编辑好以下写好的配置再copy放到容器中就好

mysqld.cnf如下:

# 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

将修改后的配置copy到容器

docker cp /root/mysqld.cnf mysql:/etc/mysql/mysql.conf.d/

**注意:**上述语句mysql为容器name,可用docker ps查看

8.2方式二 直接编辑 容器内的mysqld.cnf,编辑中文编码后重启容器即可

vi /etc/mysql/mysql.conf.d/mysqld.cnf
(内容参考上述配置文件)

9.最后重启容器就可以了

docker restart 容器ID
上一篇:Centos7下安装mysql8.0.15完整详细教程


下一篇:【解决】mysql密码不对,忘记密码,强制修改mysql密码!