本机用 docker 起了一个高版本 MySQL 服务,用Navicat测试连接时,如下提示:
1
2
3
Test Failed
2007 - Protocol mismatch; server version = 11, client version = 10
我选用的 MySQL 版本是 8.0.21,以前用的是 5.7.16,没有出现过这个问题。
经查,MySQL 的 docker服务,启用了两个端口:3306 和 33060。笔者误把 33060 看成了 3306,本来的3306被忽略了,33060端口映射成了3307 端口。 当客户端尝试连接 3307 端口时,会出现上面的错误。
MySQL8 的 3306 端口,仍然是原来的 3306 端口,客户端使用 Classic MySQL protocol 来与服务端连接; 而 33060 端口是 X Protocol 端口,客户端使用 X Protocol 协议来与服务端连接。这个协议较新,很多客户端不支持。
解决方法:映射3306端口为3307,33060为33070(不映射也没关系,Navicat 也不用),Navicat 使用3307连接。
但是有可能还是连接不了,提示如下错误:
1
Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found
原因是旧版本 MySQL 身份认证采用的是 mysql_native_password
,而从 MySQL8 开始默认采用了 caching_sha2_password
,客户端版本太低,不支持 caching_sha2_password
就会出现上面的错误。
在 MySQL5.7.16 中查看验证:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.16 |
+-----------+
1 row in set (0.00 sec)
mysql> select host,user,plugin from mysql.user where user='root';
+-----------+------+-----------------------+
| host | user | plugin |
+-----------+------+-----------------------+
| localhost | root | mysql_native_password |
+-----------+------+-----------------------+
1 row in set (0.01 sec)
在 MySQL8.0.21 中查看:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.21 |
+-----------+
1 row in set (0.0047 sec)
mysql> select host,user,plugin from mysql.user where user='root';
+-----------+------+-----------------------+
| host | user | plugin |
+-----------+------+-----------------------+
| % | root | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------+-----------------------+
解决这个问题,有两个思路:
- 升级客户端到高版本,直接有效。
- 将登录身份验证插件降级,
caching_sha2_password
降级到mysql_native_password
。 以登录名 ‘root’ 为例,1 2
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; mysql> FLUSH PRIVILEGES;
其中 ‘localhost’ 可以替换为客户端 IP,对于所有来源则为 ‘%’,’password’ 为登录用户 ‘root’ 的密码。
参考资料:https://dev.mysql.com/doc/mysql-port-reference/en/mysql-ports-reference-tables.html