1 前言
Docker容器原则上是短暂的,如果容器被删除或损毁,数据或配置将丢失,所以上个章节部署的MySQL只适合于测试环境,由于生产的需求,本章将使用Docker卷机制持久保存Docker容器中创建的数据。
2 最佳实践
2.1 环境配置
2.1.1 系统环境配置
IP Address = 10.168.0.71
Host Name = any
OS = CentOS 7.6 x86_64
2.1.2 软件环境配置
2.2 非持久化方式启动MySQL
2.2.1 下载MySQL服务器Docker镜像
docker pull mysql/mysql-server:5.6
其他版本的下载,请使用以下命令,
docker pull mysql/mysql-server:5.5
docker pull mysql/mysql-server:5.6
docker pull mysql/mysql-server:5.7
docker pull mysql/mysql-server:8.0
docker pull mysql/mysql-server:latest
下载完毕后,你可以使用以下命令查看当前下载的镜像,
docker images
可见如下显示,
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql/mysql-server 5.6 8fe1c25ccc38 6 weeks ago 218MB
2.2.2 初始化一个非持久化实例
docker run --name mysql1 -d mysql/mysql-server:5.6 --character-set-server=utf8 --collation-server=utf8_general_ci
启动实力后,可见如下显示,
87cb7f2615ff3f762332eb826566a566888e7ade0ecf5fe55cc4c462d7f2e9da
可使用如下命令查看正在运行的容器,
docker ps
可见如下显示,
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18682cd5edea mysql/mysql-server:5.6 "/entrypoint.sh mysq…" About a minute ago Up About a minute (healthy) 3306/tcp mysql1
2.2.3 检查MySQL启动的log
docker logs mysql1
2.2.4 获取初始化密码
docker logs mysql1 2>&1 | grep -i "GENERATED ROOT PASSWORD:"
可见如下输出,
Entrypoint] GENERATED ROOT PASSWORD: 63JyseNLAsYf4vnydUS0D3x4NasF
注:以上密码为“[im4DdeRId3gOM0bAKIBzumJYBr”
2.2.5 从容器内部连接MySQL服务器
docker exec -it mysql1 mysql -uroot -p
然后,我们需要使用如下命令修改root密码才能继续,
alter user 'root'@'localhost' identified by 'rootpassword';
如果有以下错误提示,
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'rootpassword'' at line 1
请使用如下命令配置,
set password for 'root'@'localhost'=PASSWORD('rootpassword');
2.2.6 创建授权账号
grant all privileges on *.* to 'root'@'%' identified by "rootpassword";
flush privileges;
2.2.7 查询实例IP
docker inspect mysql1
可见如下显示,
[...]
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "e3134a755397c4841912004d8ecff4bf918f6484e9873e3da815946062e87940",
"EndpointID": "8ae10d4677c284e21523fbf4457aa8b0c2a297999a717d111f6b05c12a56fa0a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
[...]
注:参数”IPAddress”的声明即实例当前地址
2.2.8 测试登录
yum install -y mariadb
安装完客户端后,可使用如下命令测试,
mysql -uroot -p -h 172.17.0.2
2.3 持久化方式启动MySQL
2.3.1 持久化的说明
docker inspect mysql1
可见如下输出,
[...]
"Mounts": [
{
"Type": "volume",
"Name": "6757102195b9452306e48650023d366d2c3d72b51764878f08279bb58dca1cb2",
"Source": "/data/docker-runningdata/volumes/6757102195b9452306e48650023d366d2c3d72b51764878f08279bb58dca1cb2/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
[...]
如上所示,“docker inspect”命令所输出的JSON文件,
– “Mount”关键字声明Docker卷相关定义
– “Destination”关键字声明存于容器内部路径为“/var/lib/mysql”
– “Source”关键字声明存于物理主机的卷路径为“/data/docker-runningdata/volumes/6757102195b9452306e48650023d366d2c3d72b51764878f08279bb58dca1cb2/_data”
基于以上,如果需要持久保存数据,官方提供以下命令范式用于启动,
docker run --name=mysql1 \
--mount type=bind,src=/path-on-host-machine/my.cnf,dst=/etc/my.cnf \
--mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \
-d mysql/mysql-server:tag
2.3.2 创建数据目录和配置文件目录
mkdir -p /data/docker/etc/mysql
mkdir -p /data/docker/data/
2.3.3 复制实例数据到本地
docker cp mysql1:/etc/my.cnf /data/docker/etc/mysql
docker cp mysql1:/etc/my.cnf.d /data/docker/etc/mysql
docker cp mysql1:/var/lib/mysql /data/docker/data/
2.3.4 停止并删除之前的实例
docker stop mysql1
docker rm mysql1
2.3.5 启动MySQL数据库实例
docker run --name=mysql1 \
--mount type=bind,src=/data/docker/etc/mysql/my.cnf,dst=/etc/my.cnf \
--mount type=bind,src=/data/docker/etc/mysql/my.cnf.d,dst=/etc/my.cnf.d \
--mount type=bind,src=/data/docker/data/mysql,dst=/var/lib/mysql \
-d mysql/mysql-server:5.6 \
--character-set-server=utf8 \
--collation-server=utf8_general_ci
2.3.6 测试登录
mysql -uroot -p -h 172.17.0.2
2.4 管理MySQL实例
2.4.1 使用shell管理MySQL服务器
docker exec -it mysql1 bash
可见如下命令提示符,
bash-4.2#
可使用如下命令查看MySQL的数据目录,
bash-4.2# ls /var/lib/mysql
可见如下提示,
auto.cnf ib_logfile0 ib_logfile1 ibdata1 mysql mysql.sock performance_schema test
2.4.2 管理MySQL容器的相关命令
docker start mysql1
docker restart mysql1
docker stop mysql1
docker rm mysql1
2.5 将容器加入一个特定的网络
2.5.1 创建容器间的网络
docker network create cmdschool-net
可以使用如下命令确认网络的创建,
docker network ls
可见如下显示,
NETWORK ID NAME DRIVER SCOPE
42120d142861 bridge bridge local
a44be59ebfd9 cmdschool-net bridge local
5bf4d2d3d1a6 host host local
e9ee5d7f4acc none null local
2.5.2 停止并删除之前配置的实例
docker stop mysql1
docker rm mysql1
2.5.3 使用新的参数连接Docker网络
docker run --name=mysql1 \
--mount type=bind,src=/data/docker/etc/mysql/my.cnf,dst=/etc/my.cnf \
--mount type=bind,src=/data/docker/etc/mysql/my.cnf.d,dst=/etc/my.cnf.d \
--mount type=bind,src=/data/docker/data/mysql,dst=/var/lib/mysql \
--network=cmdschool-net \
-d mysql/mysql-server:5.6 \
--character-set-server=utf8 \
--collation-server=utf8_general_ci
注:留意“–network”参数
2.5.4 查询当前实例的IP
docker inspect mysql1
可见如下显示,
"Networks": {
"cmdschool-net": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"4e0f2027134c"
],
"NetworkID": "a44be59ebfd9319f7c0ae0dd26f4d41f0d576141481388346a0ff870ee2ed809",
"EndpointID": "76513bee3bddd2c80f611e93f003dbb6c9abd53d184ade5e35faf6bb3d19f3cf",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:02",
"DriverOpts": null
}
}
注:参数”IPAddress”的声明即实例当前地址
2.5.5 测试登录
mysql -uroot -p -h 172.19.0.2
注:请自行安装mysql客户端
参阅文档
=================