解决 Mac OS 下 MySQL 客户端连接 caching_sha2_password 插件加载失败问题

20/Jun/2021 · 1 minute read

背景

在开发我自己的 gofixtures 项目时,项目单测需要用到 MySQL,于是模仿 go-txdb 的方式,使用 docker 在本地起了 MySQL 容器。执行测试时,出现如下错误:

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found

原因分析

结论:本地客户端版本过低,不支持服务器端版本的鉴权方式。

环境

客户端:

服务器端:

原因

MySQL 从 8.0 版本开始缺省使用 caching_sha2_password 作为验证方式,而 5.7 并不支持这种验证方式(5.7 默认使用 mysql_native_password)。

解决方案

方案一:升级客户端版本

这个比较简单,不用展开。

方案二:设置使用旧的 mysql_native_password 方式

  1. 首先通过命令行登陆容器 shell:

    docker exec -it mysql /bin/bash # mysql 是你的容器名字,我的就叫 mysql
    
  2. 使用容器内的 mysql 命令连接服务器:

    mysql -uroot # 你可能需要别的连接参数,我的没有密码
    
  3. 修改 root 账号的验证方式:

    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '';
    

完成!

参考资料