MySQL5.7设置root本地密码为空/简单密码(关闭密码强度验证)

2021-09-18 1185点热度 0人点赞 0条评论

前言

本文只针对MSQl5.7版本有效

MySQL8的免密码登录解决方案,请看:https://blog.terrynow.com/2021/09/21/linux-mysql-local-root-auto-login/

如果我们安装的MySQL(版本5.7)只是作为测试用途,在本机登录MySQL的root账号(root@localhost)其实可以不用设置密码(其实就算本地root账号不设置密码,其他ip用root登录可以设置密码,算是不同的账号的),因为只有登录到这台服务器才能执行mysql -uroot来登录MySQL服务器,这样测试登录就比较方便一些。

不过默认安装好后,可能无法为root@localhost设置空密码。

首次安装好MySQL后,如果默认root密码不为空,很可能已经在安装的时候,生成了一个默认的复杂密码,我们来查下日志:

[root@wulala opt]# grep 'temporary password' /var/log/mysqld.log
2021-09-17T12:09:21.125416Z 1 [Note] A temporary password is generated for root@localhost: )bpGqfpu:7g4

可以看到root@localhost的默认密码了

修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '';

[root@wulala opt]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.35

Copyright (c) 2000, 2021, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

提示不符合密码复杂度要求

MySQL默认有一个validate_password的plugin,我们需要把它卸载掉。

uninstall plugin validate_password;

mysql> uninstall plugin validate_password;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

我们发现,在没有修改密码之前,是不能执行相关操作的。好吧,我们先设置一个较为复杂的密码,然后卸载插件plugin

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456ABC';

用123456ABC作为密码登录后,再卸载validate_password插件,然后再修改成空密码就可以啦:

mysql> uninstall plugin validate_password;
Query OK, 0 rows affected (0.00 sec)

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

mysql> exit
Bye

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论