使用docker安装mysql5.7
1.安装docker
2.安装mysql5.7
1.1安装最新版本docker,如果存在旧版本的Docker,可以卸载
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
1.2安装 Docker依赖包:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
1.3添加 Docker YUM 源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
1.4.安装docker
sudo yum install docker-ce docker-ce-cli containerd.io
Dependency Installed:
docker-buildx-plugin.x86_64 0:0.12.1-1.el7 docker-ce-rootless-extras.x86_64 0:25.0.2-1.el7 docker-compose-plugin.x86_64 0:2.24.5-1.el7
Complete!
1.5.配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json << ‘EOF’
{
“registry-mirrors”: [“https://on1ekydj.mirror.aliyuncs.com”]
}
EOF
1.6.启动docker服务:
sudo systemctl enable docker
sudo systemctl start docker
sudo docker --version
Docker version 25.0.2, build 29cf629
2.安装mysql5.7
2.1.创建目录
mkdir -p /mydata/mysql/log
mkdir -p /mydata/mysql/data
mkdir -p /mydata/mysql/conf
2.2.拉取MySQL5.7镜像
docker pull mysql:5.7
[root@node2 ]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2.3.创建容器:使用自定义的 custom.cnf 配置文件。
(1)在 /mydata/mysql/conf/ 目录下创建自定义的custom.cnf配置文件
vim /mydata/mysql/conf/custom.cnf
(2)添加容器运行的配置参数。使用的是 utf8mb4 编码而不是 utf8 编码。
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init_connect=“SET collation_connection = utf8mb4_unicode_ci”
init_connect=“SET NAMES utf8mb4”
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
3)创建容器并运行。
docker run --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/mysql/data:/var/lib/mysql -v /mydata/mysql/conf:/etc/mysql/conf.d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
[root@node2 ]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84c6627f5440 mysql:5.7 “docker-entrypoint.s…” 17 seconds ago Up 16 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@node2 ]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 years ago 448MB
2.4.进入容器
docker exec -it mysql bash
mysql -u root -p123456
切换到mysql数据库,并查看user表。
use mysql;
select user,host from user;
[root@node2 ]# docker exec -it mysql bash
root@84c6627f5440:/# mysql -u root -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 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright © 2000, 2021, 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;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host from user;
±--------------±----------+
| user | host |
±--------------±----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
±--------------±----------+
4 rows in set (0.00 sec)
到此,使用docker安装mysql5.7完成,本文为实验环境学习笔记。