如果您安装了 MySQL 8.0 或更高版本并尝试连接其数据库,可能会遇到此错误:
Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found
什么是 caching_sha2_password 认证插件?
caching_sha2_password是 MySQL 的最新认证插件,它在连接加密方面带来了显着的进步,相比于其他的认证机制。首先,它具备一个内存缓存,以实现更快的认证速度。其次,它采用基于 RSA 的密码交换,与 MySQL 链接的 SSL 库独立。最后,它还支持 Unix 域文档和共享内存协议。
您可以在这里了解更多关于它的特点 这里。
我无法连接到 MySQL 8.0,为什么?
之所以是这样,因为 MySQL 和 8.0 使用 caching_sha2_password 作为 默认认证插件,但您的 MySQL 客户端目前与此不兼容。它使用了较旧版本的 libmysqlclient,这些版本不支持该 caching_sha2_password 插件。因此,连接失败。
例如,Sequel Pro 用户由于 Sequel Pro 无法支持 MySQL 8 (问题 #2699) 以及 caching_sha2_password 选项,正在经历这个问题。 插件缺失 (问题 3037)
致读者: Sequel Pro 团队 六月 20日:
Side note: Support for caching_sha2_password will probably not happen anytime soon.
Sequel Pro currently uses the 5.5 MySQL client library and this plugin is only included with the most recent 8.0 library.
Since there may be internal changes between those versions and we made some customizations ourselves, we have to go 5.5 -> 5.6 -> 5.7 -> 8.0 and check compatibility at each step.
But not even the switch to 5.6 is scheduled for the next release.
唉,真糟糕!

目前,有几种解决方案:
1. 修改 MySQL 服务器的旧密码。
- 前往“系统偏好设置” -> “MySQL”
- 从“实例”选项卡,选择“初始化数据库”
- 选择“Use legacy Password Encryption”。
- 重启服务器

现在,您可能已经可以重新连接到数据库。
2. 使用 mysql_native_password
您可以使用 ALTER 命令来将密码的加密方式更改为 mysql_native_password,而不是最新的认证插件 caching_sha2_password。
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
(注意:mysql_native_password 已经过时。在 MySQL 8.4 中默认禁用,并在 MySQL 9.0 中移除,这仅适用于较早的版本。)
3. 回退到之前的 MySQL 版本。
例如,返回 MySQL 5.7
brew uninstall mysql
brew install [email protected]
brew link [email protected] --force
MySQL 5.7 已经停止维护,并且在 Homebrew 中,[email protected] 已经不可用。
但这些只是临时性的解决方案。如果您想使用最新的加密方法,则不建议采用。
4. 使用另一个支持 MySQL 8 的 GUI 客户端。
- 下载 TablePlus 这里 或者更新 TablePlus 应用到最新版本。
- 连接任何版本的 MySQL,即使需要两步验证。
- 享受编程!
TablePlus 是一款现代、原生客户端,配备直观的 GUI 工具,用于创建、访问、查询和编辑多个关系型数据库:MySQL、PostgreSQL、SQLite、Microsoft SQL Server、Amazon Redshift、MariaDB、CockroachDB、Vertica、Cassandra、Oracle、Redis、MongoDB、Snowflake、ClickHouse、DuckDB 等。
它是天然的,美丽,而且是免费的。
