原理:绕过数据库密码验证,将user表中存储的密码更改。
1.修改MySQL的登录设置(设为无需密码)
# vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables
保存并且退出vi。
2.重新启动mysqld
/etc$ service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
3.登录并修改MySQL的root密码
- 无密码登陆mysql命令
[root@VM_25_98_centos ~]# mysql -uroot -p
- 无需输入密码进入mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> 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
- 修改root密码为123456
mysql> UPDATE user SET Password = password ( '123456' ) WHERE User = 'root' ;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
上面的情况失败时用下面这种情况(mysql 5.7密码加*)
mysql> update user set authentication_string=password('root*') where user='root' and Host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
- 执行
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
- 退出
mysql> exit;
Bye
4.删除skip-grant-tables
# vim /etc/my.cnf
将刚才在[mysqld]的段中加上的skip-grant-tables删除
保存并且退出vim
5.重启mysql
# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
mysql5.7 经常修改后提示错误,执行下面这句将密码重置就好了。
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('liuy2988*');
mysql5.7 远程登录设置:
https://blog.youkuaiyun.com/liyf155/article/details/61420245