本地环境

OS:阿里云轻量级应用服务器(学生机)-Ubuntu16.04

Docker Version:


Client:
 Version:           18.09.4
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        d14af54
 Built:             Wed Mar 27 18:34:51 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.4
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       d14af54
  Built:            Wed Mar 27 18:01:48 2019
  OS/Arch:          linux/amd64
  Experimental:     false


拉取镜像

PS:由于一直使用的MySQL5.7版本,所以这里以5.7版本为例


docker pull mysql:5.7.26 

创建挂载资料卷

在合适的地方创建两个主要的文件夹:data和config,然后在config文件夹中创建一个cnf配置文件。步骤如下:


root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config# mkdir mysql5.7.26
root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config# ls
mysql5.7.26
root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config# cd mysql5.7.26/
root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config/mysql5.7.26# mkdir data
root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config/mysql5.7.26# mkdir config
root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config/mysql5.7.26# cd config/
root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config/mysql5.7.26/config# touch mysqld.cnf


创建好mysqld.cnf之后,使用vim编辑文件:


root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config/mysql5.7.26/config# vim mysqld.cnf


添加如下内容:


[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
lower_case_table_names=1
#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


保存。

创建容器

使用如下命令创建容器:


docker run -d -p 6001:3306 --restart always -e MYSQL_ROOT_PASSWORD="mypassword" --name dimpleTestContainer -v /home/dimple/docker-config/mysql5.7.26/config/mysqld.cnf:/etc/my.cnf -v /home/dimple/docker-config/mysql5.7.26/data:/var/lib/mysql mysql:5.7.26


参数解释如下:


序号参数解释
1-d表示该容器启动后后台运行
2-p 6001:3306将主机的6001和docker容器的3306端口进行映射
3--restart always
表示开机自启动
4-e MYSQL_ROOT_PASSWORD="mypassword"
设置MySQL参数:root账号密码
5--name dimpleTestContainer
设置该容器的名称为 dimpleTestContainer
6-v /home/dimple/docker-config/mysql5.7.26/config/mysqld.cnf:/etc/my.cnf
设置参数:将/home/dimple/docker-config/mysql5.7.26/config/mysqld.cnf和/etc/my.cnf进行映射
7-v /home/dimple/docker-config/mysql5.7.26/data:/var/lib/mysql
设置参数:将/home/dimple/docker-config/mysql5.7.26/data和/var/lib/mysql进行映射

验证外挂配置是否生效


使用以下命令查看当前正在运行的容器:


docker ps


结果如下:


root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/docker-config/mysql5.7.26/config# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
b3719cbd0e83        mysql:5.7.26        "docker-entrypoint.s…"   26 minutes ago      Up 20 minutes       33060/tcp, 0.0.0.0:6001->3306/tcp   dimpleTestContainer 
af902d0e9945        redis:latest        "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:7001->6379/tcp              DimpleBlogRedisContainer


进入当前容器内部:


docker exec -it dimpleTestContainer bash


进入MySQL:


root@b3719cbd0e83:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@b3719cbd0e83:/# mysql -u root -p
Enter password: 


输入命令查看当前编码:


mysql> show variables like "%char%";
+--------------------------+----------------------------+
| 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/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)


输入exit退出MySQL,再输入exit退出容器。


mysql> exit
Bye
root@b3719cbd0e83:/# exit
exit


然后进入到之前的mysqld.cnf文件的路径,vim修改编码格式,其内容如下:


[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
lower_case_table_names=1
#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
user=mysql
#set characher
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8


保存。

然后输入以下命令重启MySQL容器


docker restart dimpleTestContainer 


然后按照之前的步骤进入到dimpleTestContainer 容器,再进入到MySQL里面,查看编码,结果如下:



mysql> show variables like "%char%" ;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)


修改成功。