文档 · 2018年12月10日 0

Mysql忘记root密码

Mysql忘了密码怎么办?
首先你要能登陆安装Mysql的这台机器,然后找到mysql的配置文件;
编辑配置文件:

vim etc/my.cnf

添加一条参数

[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
skip-grant-tables #就这这条!!!!!!
... ...

保存退出重启Mysql

直接登陆

mysql -u root -p

成功进入Mysql后修改我们的密码

mysql>set password =password('password');
mysql>flush privileges;

使用GRANT语句

mysql>grant all on *.* to 'root'@'localhost' IDENTIFIED BY 'password'with grant option ;
mysql>flush privileges;

进入mysql库修改user表

mysql>use mysql;
mysql>update user set password=password('password') where user='root'; 
mysql>flush privileges;

Mysql5.7使用此命令,亲测可用;

mysql> update user set authentication_string=password('Mysql@123') where user='root' and host='localhost'; 
Query OK, 0 rows affected, 1 warning (0.41 sec)
Rows matched: 0 Changed: 0 Warnings: 1

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

修改成功后记得修改my.cnf配置文件,把skip-grant-tables注掉。重启mysql。切记切记!!!。

打赏