Docer MySQL Authentication plugin 'caching_sha2_password' cannot be loaded:

本文解决Docker环境下MySQL 8.0以上版本在终端(如SequelPro、Navcat)无法连接的问题,详细分析错误日志并提供两种解决方案:创建容器时指定认证方式或创建后修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Docker MySQL 终端无法连接

问题描述

在使用Docer安装MySQL容器后,在终端(Sequel ProNavcat)无法连接。

错误日志

Connection failed!
Unable to connect to host 47.98.150.85, or the request timed out.
Be sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently 10 seconds).
MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image not found

问题分析

出现该问题的原因是因为我使用的MySQL镜像版本是8.0以上的。而MySQL 8.0默认的认证方式是:caching_sha2_password,之前版本的认证方式是:mysql_native_password
这种情况下一些连接工具的jdbc连接尚未升级,所以会出现上述问题。
其次,在Java程序中,也将连接失败。

解决方案

创建容器时,加载默认认证方式

在创建MySQL容器时,传入默认的认证方式--default-authentication-plugin=mysql_native_password,完整命令如下:

docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -p 3306:3306 mysql   --default-authentication-plugin=mysql_native_password

创建容器后,修改默认认证方式

这种方式在创建容器时,不需要传入默认认证试--default-authentication-plugin=mysql_native_password。使用如下方式创建MySQL容器:

docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -p 3306:3306 mysql

通过docker exec -it mysql bash进入容器,并登录MySQL数据库:

[root@izbp13xko46hud9vfr5s94z ~]# docker exec -it mysql bash 
root@551ffc60c933:/# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 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>

之后,切换至mysql数据库,对原有数据库信息进行修改。
使用use mysql切换数据库, 如下:

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>

查看当前数据库认证方式:

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

mysql>

可以看出当前数据库root用户认证方式是caching_sha2_password
修改认证方式:

mysql> alter user 'root'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.06 sec)

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

mysql> 

至此,便可解决此问题。个人更推荐使用第一种方式。

参考链接:
https://www.cnblogs.com/atuotuo/p/9402132.html
https://dev.mysql.com/doc/refman/5.7/en/caching-sha2-pluggable-authentication.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值