在Docker中快速使用各个版本的MySQL数据库(5.5、5.6、5.7、8.0、8.1)

2023-09-08 16:22:08 浏览数 (2)

介绍

https://hub.docker.com/_/mysql

http://hub.daocloud.io

代码语言:javascript复制
docker search mysql


nohup docker pull mysql:5.5.62 & 
nohup docker pull mysql:5.6.51 & 
nohup docker pull mysql:5.7.43 & 
nohup docker pull mysql:8.0.34 &
nohup docker pull mysql:8.1.0 &


docker rm mysql55 -f
docker rm mysql56 -f
docker rm mysql57 -f
docker rm mysql80 -f
docker rm mysql81 -f


mkdir -p /etc/mysql/mysql55/
mkdir -p /etc/mysql/mysql56/
mkdir -p /etc/mysql/mysql57/
mkdir -p /etc/mysql/mysql80/
mkdir -p /etc/mysql/mysql81/


cat >  /etc/mysql/mysql55/conf/my.cnf <<"EOF"
[mysqld]
skip-name-resolve
log-bin
server_id=553415
character_set_server=utf8mb4
EOF

cat >  /etc/mysql/mysql56/conf/my.cnf <<"EOF"
[mysqld]
skip-name-resolve
log-bin
server_id=563416
character_set_server=utf8mb4
EOF

cat >  /etc/mysql/mysql57/conf/my.cnf <<"EOF"
[mysqld]
default-time-zone = ' 8:00'
log_timestamps = SYSTEM
skip-name-resolve
log-bin
server_id=573417
character_set_server=utf8mb4
EOF

cat >  /etc/mysql/mysql80/conf/my.cnf <<"EOF"
[mysqld]
default-time-zone = ' 8:00'
log_timestamps = SYSTEM
skip-name-resolve
log-bin
server_id=803418
character_set_server=utf8mb4
default_authentication_plugin=mysql_native_password
EOF


cat >  /etc/mysql/mysql81/conf/my.cnf <<"EOF"
[mysqld]
default-time-zone = ' 8:00'
log_timestamps = SYSTEM
skip-name-resolve
log-bin
server_id=813419
character_set_server=utf8mb4
default_authentication_plugin=mysql_native_password
EOF



docker run -d --name mysql55 -h mysql55 -p 3415:3306 
  -v /etc/mysql/mysql55/conf:/etc/mysql/conf.d 
  -e MYSQL_ROOT_PASSWORD=lhr -e TZ=Asia/Shanghai 
  mysql:5.5.62

docker run -d --name mysql56 -h mysql56 -p 3416:3306 
  -v /etc/mysql/mysql56/conf:/etc/mysql/conf.d 
  -e MYSQL_ROOT_PASSWORD=lhr -e TZ=Asia/Shanghai 
  mysql:5.6.51

docker run -d --name mysql57 -h mysql57 -p 3417:3306 
  -v /etc/mysql/mysql57/conf:/etc/mysql/conf.d 
  -e MYSQL_ROOT_PASSWORD=lhr -e TZ=Asia/Shanghai 
  mysql:5.7.43

docker run -d --name mysql80 -h mysql80 -p 3418:3306 
  -v /etc/mysql/mysql80/conf:/etc/mysql/conf.d 
  -e MYSQL_ROOT_PASSWORD=lhr -e TZ=Asia/Shanghai 
  mysql:8.0.34

docker run -d --name mysql81 -h mysql81 -p 3419:3306 
  -v /etc/mysql/mysql81/conf:/etc/mysql/conf.d 
  -e MYSQL_ROOT_PASSWORD=lhr -e TZ=Asia/Shanghai 
  mysql:8.1.0


docker logs -f mysql55
docker logs -f mysql56
docker logs -f mysql57
docker logs -f mysql80
docker logs -f mysql81



mysql -uroot -plhr -h127.0.0.1 -P3415 -e "select now(),@@hostname,@@version;"
mysql -uroot -plhr -h127.0.0.1 -P3416 -e "select now(),@@hostname,@@version;"
mysql -uroot -plhr -h127.0.0.1 -P3417 -e "select now(),@@hostname,@@version;"
mysql -uroot -plhr -h127.0.0.1 -P3418 -e "select now(),@@hostname,@@version;"
mysql -uroot -plhr -h127.0.0.1 -P3419 -e "select now(),@@hostname,@@version;"


docker restart mysql55 mysql56 mysql57 mysql80  mysql81

检查版本

代码语言:javascript复制
[root@lhrdb ~]# mysql -uroot -plhr -h127.0.0.1 -P3415 -e "select now(),@@hostname,@@version;"
 --------------------- ------------ ------------ 
| now()               | @@hostname | @@version  |
 --------------------- ------------ ------------ 
| 2023-08-21 12:35:29 | mysql55    | 5.5.62-log |
 --------------------- ------------ ------------ 
[root@lhrdb ~]# mysql -uroot -plhr -h127.0.0.1 -P3416 -e "select now(),@@hostname,@@version;"
 --------------------- ------------ ------------ 
| now()               | @@hostname | @@version  |
 --------------------- ------------ ------------ 
| 2023-08-21 12:35:29 | mysql56    | 5.6.51-log |
 --------------------- ------------ ------------ 
[root@lhrdb ~]# mysql -uroot -plhr -h127.0.0.1 -P3417 -e "select now(),@@hostname,@@version;"
 --------------------- ------------ ------------ 
| now()               | @@hostname | @@version  |
 --------------------- ------------ ------------ 
| 2023-08-21 12:35:29 | mysql57    | 5.7.43-log |
 --------------------- ------------ ------------ 
[root@lhrdb ~]# mysql -uroot -plhr -h127.0.0.1 -P3418 -e "select now(),@@hostname,@@version;"
 --------------------- ------------ ----------- 
| now()               | @@hostname | @@version |
 --------------------- ------------ ----------- 
| 2023-08-21 12:35:29 | mysql80    | 8.0.34    |
 --------------------- ------------ ----------- 
[root@lhrdb ~]# mysql -uroot -plhr -h127.0.0.1 -P3419 -e "select now(),@@hostname,@@version;"
 --------------------- ------------ ----------- 
| now()               | @@hostname | @@version |
 --------------------- ------------ ----------- 
| 2023-08-21 12:35:29 | mysql81    | 8.1.0     |
 --------------------- ------------ ----------- 
[root@lhrdb ~]# 

示例

代码语言:javascript复制
root@lhrxxt:~# docker run -d --name mysql8030 -h mysql8030 -p 3418:3306 
>   -v /etc/mysql/mysql8030/conf:/etc/mysql/conf.d 
>   -e MYSQL_ROOT_PASSWORD=lhr -e TZ=Asia/Shanghai 
>   mysql:8.0.30
f6e94a91fd69e26d0e9e7bd5b4ef11ecde0f4b8502299c537342141ea01bf80a
root@lhrxxt:~# 
root@lhrxxt:~# cat >  /etc/mysql/mysql8030/conf/my.cnf <<"EOF"
> [mysqld]
> default-time-zone = ' 8:00'
> log_timestamps = SYSTEM
> skip-name-resolve
> log-bin
> server_id=80303418
> character_set_server=utf8mb4
> EOF
root@lhrxxt:~# docker restart mysql8030
mysql8030
root@lhrxxt:~# docker exec -it mysql8030 bash
bash-4.4# mysql      
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
bash-4.4# mysql -uroot -plhr
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.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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                |
 -------------------- 
4 rows in set (0.05 sec)

mysql> create database lhrdb;
Query OK, 1 row affected (0.04 sec)

mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| lhrdb              |
| mysql              |
| performance_schema |
| sys                |
 -------------------- 
5 rows in set (0.00 sec)

0 人点赞