MySQL 取消免密登陆失败

MySQL8.0设置密码后,仍然可以免密登陆
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[root@bogon mysql]# 
[root@bogon mysql]# 
[root@bogon mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.31-commercial MySQL  Server - Commercial

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 
mysql> select user,host,authentication_string from mysql.user ;
+------------------+-----------+------------------------------------------------------------------------+
| user             | host      | authentication_string                                                  |
+------------------+-----------+------------------------------------------------------------------------+
| mysql.infoschema | localhost | $A$005$THISISATANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | localhost | $A$005$THISISACOLTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | localhost | $A$005$THISISACOMBINTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             | localhost | $A$005$YS&{@VTMi8|mAXhxfxDk/seIDuFPtc66RqTN/ |
+------------------+-----------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> exit


操作系统:centos7 、MySQL版本:8.0.31
为什么设置了MySQL密码,还能免密登陆呢?

我擦,居然是我的密码有bug。。。

1.设置密码为“Newpassword”,一切正常,没有密码不能登陆。

mysql> 
mysql> alter user user() identified by 'Newpassword';
Query OK, 0 rows affected (0.03 sec)

mysql> 
mysql> exit
Bye
[root@bogon mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@bogon mysql]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 8.0.31-commercial MySQL Server - Commercial

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

2.修改密码为我想设置的密码,问题就又出现了,空密码或者我设置的密码都可以正常登陆。

mysql> 
mysql> alter user user() identified by 'xxxxx';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[root@bogon mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 8.0.31-commercial MySQL Server - Commercial

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> exit
Bye
[root@bogon mysql]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 8.0.31-commercial MySQL Server - Commercial

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

目前只试出来我自己的密码会出现这个问题。。。。

flush privileges 执行之后再试试呢,如果密码跟确认密码两次输入不一致,也会默认为没有设置密码

在MySQL 8.0中,除了密码之外,还需要启用密码验证插件才能实现密码验证功能。如果您设置了密码但仍可以免密登录,则可能是因为您的MySQL实例仍在使用旧的密码验证插件,而不是您新设置的密码验证插件。

您可以通过检查MySQL的密码验证插件来解决此问题。可以通过运行以下命令来查看MySQL的密码验证插件:

mysql> SELECT plugin FROM mysql.user WHERE User='root';

如果插件为caching_sha2_password,则您的MySQL实例正在使用新的密码验证插件。如果插件为mysql_native_password,则您需要更新插件以使用新的密码验证插件。您可以通过以下步骤更新插件:

打开MySQL配置文件my.cnf或my.ini文件
在[mysqld]部分下添加以下内容:

default_authentication_plugin=caching_sha2_password

保存并关闭文件。
重启MySQL服务。
然后再次检查MySQL的密码验证插件是否为caching_sha2_password。如果是,则您应该需要使用密码才能登录MySQL。
最好如果还是不行,就得考虑其他问题了