Docker - 部署 MySQL 5.7 数据库容器
环境信息
操作系统:
$ cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
$ uname --kernel-release
3.10.0-1062.el7.x86_64
Docker 版本:
$ docker version
Client: Docker Engine - Community
Version: 19.03.11
API version: 1.40
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:13:48 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.11
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:12:26 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
部署 MySQL 5.7 数据库容器
1、访问 Docker Hub,搜索 mysql,过滤出 5.7 的版本。
2、拉取 MySQL 5.7.30 镜像。
$ docker pull mysql:5.7.30
5.7.30: Pulling from library/mysql
afb6ec6fdc1c: Pull complete
0bdc5971ba40: Pull complete
97ae94a2c729: Pull complete
f777521d340e: Pull complete
1393ff7fc871: Pull complete
a499b89994d9: Pull complete
7ebe8eefbafe: Pull complete
4eec965ae405: Pull complete
a531a782d709: Pull complete
270aeddb45e3: Pull complete
b25569b61008: Pull complete
Digest: sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
Status: Downloaded newer image for mysql:5.7.30
docker.io/library/mysql:5.7.30
3、查看拉取的镜像。
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.30 a4fdfd462add 12 days ago 448MB
4、运行镜像,并设置 MySQL root 用户的密码。
$ docker run --interactive --detach --name mysql --publish 3306:3306 --env MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30
--publish 3306:3306
宿主机的 3306 端口映射到容器的 3306 端口。
--env MYSQL_ROOT_PASSWORD=123456
设置 MySQL root 用户的密码。
5、列出正在运行的容器。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21fb901d85e6 mysql:5.7.30 "docker-entrypoint.s…" 10 seconds ago Up 9 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
修改 MySQL 的字符集
1、进入容器。
$ docker exec --interactive --tty mysql /bin/bash
2、在容器中使用 MySQL 客户端连接服务端。
root@21fb901d85e6:/# mysql --user root --password
Enter password: 123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
3、连接到 MySQL 服务端之后,通过 status
命令获取服务端的状态信息。
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.30 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 11 min 23 sec
Threads: 2 Questions: 9 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.013
--------------
从第 15 ~ 18 行可以看出,当前服务端、数据库和客户端等的字符集都是 latin1
,接下来我们需要修改这些字符集。
4、因为 MySQL 容器中没有 vi 编辑器,所以我们需要先回到宿主机。接着将 MySQL 容器中 MySQL 配置文件复制到宿主机的根目录下。
docker cp mysql:/etc/mysql/mysql.cnf /
注意:复制之前请确保没有同名的文件。
5、修改 mysql.cnf
文件,添加以下内容。
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
[client]
default-character-set=utf8mb4
6、修改完成之后,将该文件复制到 MySQL 容器的 /etc/mysql/
目录下。
$ docker cp /mysql.cnf mysql:/etc/mysql/
7、重启 MySQL 容器。
$ docker restart mysql
8、进入容器。
$ docker exec --interactive --tty mysql /bin/bash
9、重新连接 MySQL 服务端。
root@21fb901d85e6:/# mysql --user root --password
Enter password: 123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
10、执行 status
命令,可以看到服务端、数据库和客户端等的字符集都是 utf8mb4
。
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.30 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 1 min 44 sec
Threads: 1 Questions: 5 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.048
--------------