愿月常圆,休要暂时缺。

What’s your problems

MySQL8.0登录提示caching_sha2_password问题解决方法

背景

docker构建mysql容器后连接遇到以下问题

问题

Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found
1
mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
1
mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
1

MySQL官方说明

解决方法1(docker)

适用场景

  1. 第一次构建容器/安装
  2. 已安装完成后新增用户

配置

配置 mysql.cnf 配置默认身份验证插件

[mysqld]
default_authentication_plugin = mysql_native_password
12

验证是否生效

使用CLI进入MySQL

$ mysql -u root -p
1
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> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | mysql_native_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
12345678910111213141516

root用户的身份验证器插件已经变为:mysql_native_password

解决方法2

适用场景

  1. MySQL 已成功安装完成后

查看身份验证类型

mysql> use mysql;
Database changed

mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
1234567891011121314

root 用户的验证器插件为 caching_sha2_password

修改身份验证类型(修改密码)

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

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

使生效

mysql> FLUSH PRIVILEGES;
1

验证是否生效

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

表名忽略大小写

在 my.cnf 文件中添加:

[mysqld]
# 表明忽略大小写
lower_case_table_names = 1

在 MySQL 8 中如果启动时出现:

different lower_case_table_names settings for server (0) and data dictionary (1)

解决:

MySQL8.0 要求 lower-case-table-names 在数据库初始化和启动时的值必须一致

导出数据库(会导出到命令行当前所在目录下):

mysqldump -uroot -proot --all-databases > alldb.sql

如果出现The user specified as a definer('xxx') does not exist when using LOCK TABLES 使用下面的语句

mysqldump --single-transaction -u root -p --all-databases> alldb.sql

重新初始化:

mysqld --defaults-file=my.cnf --user=mysql --initialize  --lower-case-table-names=1

导入数据:

mysql -uroot -proot < alldb.sql

版权声明:如无特别声明,本站收集的文章归  HuaJi66/Others  所有。 如有侵权,请联系删除。

联系邮箱: [email protected]

本文标题:《 MySQL8.0登录提示caching_sha2_password问题解决方法 》

本文链接:/%E6%95%B0%E6%8D%AE%E5%BA%93/%E5%85%B3%E7%B3%BB%E5%9E%8B/mysql/%E9%94%A6%E5%9B%8A/problem.html