MySQL 是世界上最受欢迎的开源数据库。凭借其可靠性、易用性和性能,MySQL 已成为 Web 应用程序的数据库优先选择。本文演示在 Docker 上部署 MySQL 8.1。
MySQL: https://www.mysql.com/
1. 部署环境
IP 地址(本地测试环境):192.168.0.10
操作系统:Linux CentOS 7.9
Docker 版本: 20.10.7
Docker Compose 版本: 2.6.1
MySQL 目录:/home/docker/mysql
Linux 用户: docker (非 root 用户,有 sudo 权限)
2. MySQL 配置文件
在 /home/docker/mysql/conf 目录下,创建 my.cnf 文件,内容如下:
[mysql] default-character-set=utf8 socket = /var/lib/mysql/mysql.sock [mysqld] pid-file = /var/lib/mysql/mysql.pid socket = /var/lib/mysql/mysql.sock datadir = /var/lib/mysql secure-file-priv= NULL log-bin-trust-function-creators=1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/ # 错误日志,指定到 /var/log/mysql 目录 log_error=/var/log/mysql/mysql_err.log
3. 创建 docker-compose.yml
在 /home/docker/mysql 目录下,创建 .env 文件,内容如下:
MYSQL_ROOT_PASSWORD=123456
MYSQL_DATABASE=testdb
在 /home/docker/mysql 目录下,创建 docker-compose.yml 文件,内容如下:
version: "3.7" networks: service-network: name: service-network services: service_mysql: image: mysql:8.1 restart: always container_name: mysql_service ports: - 3306:3306 environment: - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE=${MYSQL_DATABASE} networks: - service-network volumes: - ./conf/my.cnf:/etc/mysql/my.cnf:rw - /home/docker/mysql/data:/var/lib/mysql/:rw - /home/docker/mysql/logs:/var/log/mysql/:rw
4. 运行
$ cd /home/docker/mysql # 进入 docker-compose.yml 所在目录
# 在后台运行
$ docker-compose up -d
[+] Running 2/2 ⠿ Network service-network Created 0.1s ⠿ Container mysql_service Started 0.4s
注:本地 docker 没有所需要的镜像时,会自动下载所需的镜像。遇到无法自动下载的情况,可以使用 docker pull 命令下载所需的镜像,在运行 docker-compose up 命令。
# 查看容器运行情况
$ docker ps
CONTAINER ID IMAGE ... CREATED STATUS PORTS NAMES 1c53bb5b4cf9 mysql:8.1 8 seconds ago Restarting (1) Less than a second ago mysql_service
注:从 STATUS 的状态,可以发现 mysql_service 容器一直处于 Restarting 状态,运行不正常。
原因分析:容器内的程序要在挂载的 log 目录下创建 log 文件,需要确保 root/root 以外的用户也有 log 目录的写权限,修改 log 目录的权限,命令如下:
$ suod chmod a+w /home/docker/mysql/logs
修改后需要重启容器。
# 重启容器
$ docker restart mysql_service
$ docker ps
CONTAINER ID IMAGE ... CREATED STATUS PORTS NAMES 1c53bb5b4cf9 mysql:8.1 8 minutes ago Up 56 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql_service
# 进入容器,运行 MySQL 客户端
$ docker exec -it mysql_service /bin/bash
bash-4.4# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) bash-4.4# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.1.0 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | testdb | +--------------------+ 5 rows in set (0.01 sec) mysql> exit # 退出容器 bash-4.4# exit
5. MySQL 客户端
1) 安装 MySQL 客户端
宿主主机环境是 CentOS 7.9,默认没有安装 MySQL 客户端,需要运行 yum 命令来安装 MySQL 客户端,命令如下:
$ sudo yum install mysql
...
注:可能过程中需运行 yum update 更新 yum,这里就不介绍 MySQL 客户端的具体安装过程。
2) 运行 MySQL 客户端
连接容器内的 MySQL Server,命令如下:
$ mysql -uroot -p123456
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
注: 运行 ls /var/lib/mysql/mysql.sock 命令,返回结果如下:
ls: cannot access /var/lib/mysql/mysql.sock: No such file or directory
表示宿主机上文件 /var/lib/mysql/mysql.sock 不存在,因为 /var/lib/mysql 目录是 Docker 容器内的路径,映射到宿主机上(容器外)的路径是 /home/docker/mysql/data 目录。
运行如下命令:
$ ls /home/docker/mysql/data/mysql.sock
/home/docker/mysql/data/mysql.sock
表示 mysql.sock 在宿主机的 /home/docker/mysql/data 目录里。
修改 MySQL 客户端 (宿主主机上) 的配置文件 /etc/my.cnf,添加如下内容:
[mysql]
socket=/home/docker/mysql/data/mysql.sock
修改完 /etc/my.cnf 后,继续运行 mysql 客户端,命令如下:
$ mysql -uroot -p123456
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
注:部分 MySQL 客户端连接软件不支持 MySQL 8 新增加的加密方式 caching_sha2_password,所以无法连接。
3) 修改 MySQL 加密方式
由于部分 MySQL 客户端连接软件不支持 MySQL 8 新增加的加密方式 caching_sha2_password,需要将其改为旧的加密验证方式 mysql_native_password。具体步骤如下:
# 进入容器,运行 MySQL 客户端
$ docker exec -it mysql_service /bin/bash
bash-4.4# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) bash-4.4# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.1.0 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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. mysql> use mysql; mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; mysql> FLUSH PRIVILEGES; myql> exit # 退出容器 bash-4.4# exit
$ mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.1.0 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | testdb | +--------------------+ 5 rows in set (0.00 sec) MySQL [(none)]>
以上信息表示宿主主机上的 MySQL 客户端连接容器内的 MySQL Server 成功。