# 拉取mysql:8.0镜像
[root@ecs-s6-medium-2-linux-20191113090041 home]# docker pull mysql:8.0
Trying to pull repository docker.io/library/mysql ...
8.0: Pulling from docker.io/library/mysql
Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee
Status: Image is up to date for docker.io/mysql:8.0
[root@ecs-s6-medium-2-linux-20191113090041 home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.7 cd3ed0dfff7e 4 weeks ago 437 MB
docker.io/mysql 8.0 c8ee894bd2bd 4 weeks ago 456 MB
# 现在mysql:5.7已经正在运行
[root@ecs-s6-medium-2-linux-20191113090041 conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cf8b0556758 mysql:5.7 "docker-entrypoint..." 2 days ago Up 2 days 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 conf]#
参考docker之mysql5.7的安装中的第3和第4条配置,流程一致, 为了区分, 5.7创建的目录是mysql5.7
, 本次的8.0创建的目录是mysql8.0
, 本次忽略过程,结果如下:
[root@ecs-s6-medium-2-linux-20191113090041 project]# pwd
/home/project
[root@ecs-s6-medium-2-linux-20191113090041 project]# ll
total 4
drwxr-xr-x 4 root root 4096 Nov 14 15:42 mysql5.7
drwxr-xr-x 4 root root 4096 Nov 18 11:44 mysql8.0
[root@ecs-s6-medium-2-linux-20191113090041 project]# cd mysql5.7/
[root@ecs-s6-medium-2-linux-20191113090041 mysql5.7]# pwd
/home/project/mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 mysql5.7]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 15 17:58 conf
drwxr-xr-x 5 polkitd root 4096 Nov 15 15:56 data
[root@ecs-s6-medium-2-linux-20191113090041 mysql5.7]# cd ../mysql8.0/
[root@ecs-s6-medium-2-linux-20191113090041 mysql8.0]# pwd
/home/project/mysql8.0
[root@ecs-s6-medium-2-linux-20191113090041 mysql8.0]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 11:45 conf
drwxr-xr-x 2 root root 4096 Nov 18 11:44 data
# mysql8.0的配置文件与mysql5.7的配置文件稍有不同
[root@ecs-s6-medium-2-linux-20191113090041 mysql8.0]# cat conf/mysql.conf
[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
# default: sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# modeified:
sql_mode= STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-time_zone = '+8:00'
# Custom config should go here
!includedir /etc/mysql/conf.d/
[root@ecs-s6-medium-2-linux-20191113090041 mysql8.0]#
[root@ecs-s6-medium-2-linux-20191113090041 logs]# docker run --name mysql8.0 --restart=always --privileged=true -p 3307:3306 -v /home/project/mysql8.0/conf/mysql.conf:/etc/mysql/my.cnf -v /home/project/mysql8.0/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456-d mysql:8.0
2cfcbac7ee77894d945accb775acde3036413e24c2dc591d6f4e1554ef63163a
# 检查是否已经启动
[root@ecs-s6-medium-2-linux-20191113090041 logs]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cfcbac7ee77 mysql:8.0 "docker-entrypoint..." 3 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." About an hour ago Up 28 minutes 33060/tcp, 0.0.0.0:3306->3306/tcp mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 logs]#
-e MYSQL_ROOT_PASSWORD=123456
配置的密码不起作用,[root@ecs-s6-medium-2-linux-20191113090041 logs]# docker exec -it mysql8.0 /bin/bash
root@2cfcbac7ee77:/# mysql -uroot -p
Enter password: 此处键入 `123456` 报错了
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@2cfcbac7ee77:/# mysql -uroot -p
Enter password: 此处直接回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Database changed
mysql> select host, user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | caching_sha2_password | |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 因为下面创建了新的用户root2, 数据安全起见, 所以此处的root用户访问host最好还是恢复为host=localhost,我为了学习, 就没有恢复.
mysql> select host, user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
# 这些sql在8.0里不能使用了.
mysql> update user set password=password('password') where user='root';
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 '('123456') where user='root'' at line 1
mysql> update user set password='password' where user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> grant all privileges on *.* to root@'%' identified by '123456';
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 '123456'' at line 1
mysql> create user 'root'@'%' identified by 'password';
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
# 不能为自己授权, 那就新建个用户
mysql> create user `root2`@'%' identified by 'password';
Query OK, 0 rows affected (0.07 sec)
mysql> select host, user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | |
| % | root2 | caching_sha2_password | $A$005$K3\0mfP%M%5Fz#jfuG9BqAwFNfgBE0SOgOnNCw3I0A2OgJcXYUwJgKD0bNB |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
# 为新用户授权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root2'@'%';
Query OK, 0 rows affected (0.08 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
# 添加远程连接
mysql> ALTER USER 'root2'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.06 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)
mysql>
既然5.1中提到-e MYSQL_ROOT_PASSWORD=123456
不起作用,那就在docker run命令中去掉这一环境变量尝试一下.
# 现在系统中正在运行中两个mysql
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2cfcbac7ee77 mysql:8.0 "docker-entrypoint..." 2 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." 3 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3309->3306/tcp mysql5.7
# 测试去掉环境配置后
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker stop mysql8.0 && docker rm mysql8.0 && docker run -d -p 3307:3306 --restart=always --privileged=true -v /home/project/mysql8.0/conf:/etc/mysql -v /home/project/mysql8.0/data:/var/lib/mysql --name mysql8.0 mysql:8.0
mysql8.0
mysql8.0
aea5b87bdc672a795106bb3e6c54e519c514b43d44eae81608c8cd1d3d23627e
# 没有起来
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aea5b87bdc67 mysql:8.0 "docker-entrypoint..." 7 seconds ago Up Less than a second 33060/tcp, 0.0.0.0:3307->3306/tcp mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." 3 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3309->3306/tcp mysql5.7
# 再减参
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker stop mysql8.0 && docker rm mysql8.0 && docker run -d -p 3307:3306 -v /home/project/mysql8.0/conf:/etc/mysql -v /home/project/mysql8.0/data:/var/lib/mysql --name mysql8.0 mysql:8.0
mysql8.0
mysql8.0
469eb43c63e4625f6479f8d367710f7dae1b896ae20f37746992d20b6d2fd50d
# 没有起来
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
469eb43c63e4 mysql:8.0 "docker-entrypoint..." 7 seconds ago Exited (1) 5 seconds ago mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." 3 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3309->3306/tcp mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker stop mysql8.0 && docker rm mysql8.0 && docker run -d -p 3307:3306 -v /home/project/mysql8.0/conf:/etc/mysql --name mysql8.0 mysql:8.0
mysql8.0
mysql8.0
7cc575468db9992109ee1d1d273ed3422bbdd06c1129886c6923dcba98fa795e
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7cc575468db9 mysql:8.0 "docker-entrypoint..." 6 seconds ago Exited (1) 4 seconds ago mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." 3 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3309->3306/tcp mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker stop mysql8.0 && docker rm mysql8.0 && docker run -d -p 3307:3306 --name mysql8.0 mysql:8.0
mysql8.0
mysql8.0
67d9786a6632023e8755ee8e62d2c43a797eb4bb7e91aa0eb1b83108e5508c0d
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
67d9786a6632 mysql:8.0 "docker-entrypoint..." 4 seconds ago Exited (1) 2 seconds ago mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." 3 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3309->3306/tcp mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker stop mysql8.0 && docker rm mysql8.0 && docker run -d --name mysql8.0 mysql:8.0
mysql8.0
mysql8.0
1a9c381307a2b65aee284c5715971f435a55be51ecc76015dbe1871718526e7c
[root@ecs-s6-medium-2-linux-20191113090041 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a9c381307a2 mysql:8.0 "docker-entrypoint..." 5 seconds ago Exited (1) 3 seconds ago mysql8.0
251e5b8eaced mysql:5.7 "docker-entrypoint..." 3 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3309->3306/tcp mysql5.7
[root@ecs-s6-medium-2-linux-20191113090041 ~]#
粗暴结论: -e MYSQL_ROOT_PASSWORD=123456
虽然没有起到决定性作用, 但少了这个参数,容器跑不起来, 所以还是加上吧