1. 程式人生 > 其它 >linux下忘記mysql root密碼解決辦法

linux下忘記mysql root密碼解決辦法

前言:最近碰到一個客戶設定錯了mysql的密碼導致登不上去mysql,但是重灌又比較麻煩,剛好碰到了經過測試是可以實現的,首先講一個,在 /etc/my.cnf 裡面增加 skip-grant-tables,並重啟mysql是可以不用密碼直接登陸進去的,也就是說,理論上是不需要密碼直接可以登上去的,下面開始:****1、編輯MySQL配置檔案my.cnf

vi /etc/my.cnf #編輯檔案,找到[mysqld],在下面新增一行skip-grant-tables
skip-grant-tables

:wq! #儲存退出

service mysqld restart #重啟MySQL服務
**2、進入MySQL控制檯**mysql -uroot -p #直接按回車,這時不需要輸入root密碼。
**3、修改root密碼**update mysql.user set authentication_string=password('123456') where User="root" and Host="localhost";

flush privileges; #刷新系統授權表

grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;
**4、取消/etc/my.cnf中的skip-grant-tables**vi /etc/my.cnf 編輯檔案,找到[mysqld],刪除skip-grant-tables這一行

:wq! #儲存退出
**5、重啟mysql**service mysqld restart#重啟mysql,這個時候mysql的root密碼已經修改為123456
**6、進入mysql控制檯** mysql -uroot -p #進入mysql控制檯
 123456 #輸入密碼