Docker安装mysql8.0中间会遇到很多坑,这边与大家分享下。

先安装docker,这边其他博客已经介绍,不懂的童鞋可以先看下docker入门和安装

1、远程拉取mysql镜像

[root@bogon /]#  docker pull docker.io/mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ... 
latest: Pulling from docker.io/library/mysql
f17d81b4b692: Pull complete 
c691115e6ae9: Pull complete 
41544cb19235: Pull complete 
254d04f5f66d: Pull complete 
4fe240edfdc9: Pull complete 
0cd4fcc94b67: Pull complete 
8df36ec4b34a: Pull complete 
720bf9851f6a: Pull complete 
e933e0a4fddf: Pull complete 
9ffdbf5f677f: Pull complete 
a403e1df0389: Pull complete 
4669c5f285a6: Pull complete 
Digest: sha256:811483efcd38de17d93193b4b4bc4ba290a931215c4c8512cbff624e5967a7dd
Status: Downloaded newer image for docker.io/mysql:latest

2、 本地新建两个文件夹作为docker挂载在本地的mysql配置 

[root@bogon /]# mkdir /home/mysql/data
[root@bogon /]# mkdir /home/mysql/conf.d

3、 启动docker镜像,设置root密码、映射端口和挂载路径

[root@bogon /]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /home/mysql/data:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -d mysql
43ed3d44e27d6d5519940bcc385ba703dc9d1752f57d755f9ccc6897612a8768

4、 发现3306端口并没有启动起来

[root@bogon /]# lsof -i:3306

 5、查看mysql启动日志,发现并没有权限进行挂载

[root@bogon /]#  docker logs -f mysql
ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"
mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 13 - Permission denied)
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!

6、 启动mysql镜像时新增权限命令,重启成功

[root@bogon /]# docker run --privileged=true  --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456  -v /home/mysql/data:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -d mysql
e58a1bd3ec7dc42ba3717a67de40aa13c83a9f74ebc7065da257e0088c94862a
[root@bogon /]# lsof -i:3306
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
docker-pr 19082 root    4u  IPv6 65804197      0t0  TCP *:mysql (LISTEN)

7、此时navicat连接mysql提示 1251- Client does not support authentication protocol,由于默认密码加密方式不对

      一、进入docker内部

[root@bogon ~]# docker exec -it mysql bash
root@e58a1bd3ec7d:/# 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 10
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> 

      二、修改root默认密码

mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

8、navicat重新连接,发现可以连接,sql创建一张表,发现报错:docker安装mysql8.0 this is incompatible with sql_mode=only_full_group_by

执行以下操作可以解决

mysql> select @@global.sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                                     |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.sql_mode;
+----------------------------------------------------------------------------------------------------+
| @@global.sql_mode                                                                                  |
+----------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

至此docker安装mysql已基本结束。

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐