流易

Docker 安装 MySQL 8.0

2020-01-05
poazy

基于 docker 19.03.5 版本创建并运行 mysql 8.0.18 容器。

安装环境

OS 版本

[root@centos7-qscft ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@centos7-qscft ~]#

Docker 版本

[root@centos7-qscft ~]# docker -v
Docker version 19.03.5, build 633a0ea
[root@centos7-qscft ~]#

mysql 安装

查找 mysql 镜像

https://hub.docker.com/_/mysql?tab=tags

拉取 mysql 镜像

### 拉取镜像,选择 8.0.18 版本
docker pull mysql:8.0.18

运行 mysql 镜像

docker run -d --restart=always --name mysql \
	-p 3306:3306 \
	-v /boazy/data/dockerdata/mysql/data:/var/lib/mysql \
	-v /boazy/data/dockerdata/mysql/log:/var/log/mysql \
    -v /etc/localtime:/etc/localtime:ro \
	-e "TZ=Asia/Shanghai" \
	-e MYSQL_ROOT_PASSWORD=mysql123 \
	mysql:8.0.18 \
	--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

配置 mysql

  • 8.0.18 版本没有进行配置也没有出来错误
  • 避免后面使用遇到一堆错误
### 连接数据库
docker exec -it mysql mysql -u root -p
### 输入密码
### 切换为 mysql 库
use mysql;
### 设置远程访问
### 1130-host '172.0.0.1' is not allowed to connect to this MySql server
### https://blog.csdn.net/h996666/article/details/80921913
grant all privileges on *.* to 'root'@'%';

### ERROR 1410 (42000): You are not allowed to create a user with GRANT
update user set host='%' where user='root';
### 再执行两次命令(第一次还是会报错,第二次就 OK 了):
grant all privileges on *.* to 'root'@'%';

### 查询加密方式
select Host,User,plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
### 修改为 mysql_native_password 
### 2059 - Authentication plugin 'caching_sha2_password' cannot be loaded:...'
### java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
alter user 'root'@'%' identified with mysql_native_password by 'duanbo';
### 查询加密方式
select Host,User,plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)

### mysqladmin -u root -p flush-hosts
### 1129-host '172.0.0.1' is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’
flush hosts;
### 查询 max_connect_errors 参数值
show variables like '%max_connect_errors%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 100   |
+--------------------+-------+
1 row in set (0.03 sec)
### 设置 max_connect_errors 参数值
set global max_connect_errors = 1000;
### 查询 max_connect_errors 参数值
show variables like '%max_connect_errors%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
+--------------------+-------+
1 row in set (0.01 sec)

Similar Posts

Comments