MySQL 修改密码
参考1:https://blog.csdn.net/wdwangye/article/details/107442305
由于mysql密码规范,导致密码较为复杂,难免会遇到忘记密码的情况,今天一起来学习如何在忘记密码的情况下修改密码。
- 进入配置文件/etc/my.cnf,添加 skip-grant-tables。(可能跟我写的不一样,但是在mysqld下添加此行就可以了)
[root@slave ~]# vim /etc/my.cnf 1 [client] 2 port = 3306 3 socket = /tmp/mysql.sock 4 5 [mysqld] 6 server_id=2 7 relay-log=relay-log 8 relay-log-index=relay-log.index 9 skip-grant-tables #在[mysqld]下添加此行。 10 port = 3306 11 user = mysql
- 重启mysql
[root@slave ~]# systemctl stop mysql [root@slave ~]# systemctl start mysql
- 此时可以不用密码直接进入mysql
[root@slave ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.15 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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> flush privileges; #刷新mysql Query OK, 0 rows affected (0.10 sec) mysql> alter user "root"@"localhost" identified by "WWW13@me"; Query OK, 0 rows affected (0.11 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #此过程三步缺一不可
知识点补充:
flush privileges原理:
flush privileges 命令本质上的作用是将当前user和privilige表中的用户信息权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。而不必冒太大风险。
- 进入配置文件,将刚才加入的参数删除或注释,不然还是可以免密登录。
cat /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock [mysqld] server_id=2 relay-log=relay-log relay-log-index=relay-log.index #skip-grant-tables ………………
- 最后重启mysql
[root@slave ~]# systemctl stop mysql [root@slave ~]# systemctl start mysql