解決MySQL忘記root密碼的問題

1、修改mysql配置文件,實現免密碼登陸

在mysql配置文件中添加以下參數:
[mysqld]
skip-grant-tables

2、重啓MySQL服務

service mysqld restart

3、免密登陸

mysql -uroot -p (直接點擊回車,密碼爲空)

4、修改root密碼

1.切換數據庫
use mysql;
2.執行命令修改密碼
update user set password=password('root') where user='root';
//5.7 以後版本執行上面命令報錯可以嘗試以下語句:
update user set authentication_string=password('123456') where user='root';
3.刷新權限
flush privileges;
//可能會出現的異常
ERROR 1820 (HY000): You must reset your password using ALTER USER statement;
//解決辦法(執行以下命令):
alter user user() identified by "root";

5、還原配置文件,重啓Mysql服務

[mysqld]
#skip-grant-tables
//重啓服務
service mysqld restart

6、使用新密碼登陸

mysql -uroot -p (回車輸入密碼)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章