在日常使用MySQL数据库时如果出现忘记密码无法登录的情况,总不能选择重新去安装MySQL的方式设置密码吧,
重置密码的步骤
第一步更改MySQL的配置文件,写入一条配置
编辑/etc/my.cnf
[mysqld]
skip-grant-tables
第二步重启mysqld
systemctl restart mysqld
第三步直接登录MySQL数据库
使用 msyql -uroot
直接登录
[root@server1 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-log Source distribution
Copyright (c) 2000, 2020, 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后,使用msql库,再进行密码更改
使用库use mysql
更新密码:update mysql.user set authentication_string=password('root_password') where user='root';
root_password就是你自定义的密码
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> update mysql.user set authentication_string=password('root_password') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
第五步设置完密码退出,在配置文件删除之前写入的参数,重启mysqld
vim /etc/my.cnf
systemctl restart mysqld
第六步使用新密码登录
mysql -uroot -p******