Mysql快速找回root密碼妙招

語言: CN / TW / HK
 目前B站正在直播Mysql、Oracle實戰,詳情關注公眾號:IT邦德 QQ群:955286955、168797397

一、Mysql root使用者密碼忘記 (8.0以下)

1.用命令編輯/etc/my.cnf配置檔案,即:vim /etc/my.cnf 或者 vi /etc/my.cnf
2.在[mysqld]下新增skip-grant-tables,然後儲存並退出
3.重啟mysql服務:service mysqld restart
4.更改root使用者名稱
重啟以後,執行mysql命令進入mysql命令列
5.修改root使用者密碼
update mysql.user set authentication_string=password('root') where user='root';   --5.7版本
flush privileges;
6.把/etc/my.cnf中的skip-grant-tables註釋掉,然後重啟mysql,即:service mysqld restart
好了,下面就可以用root新的密碼登入了!
二、Mysql root使用者密碼忘記 (8.0及以上)
在mysql8系統下,適用如下方法(全路徑)
 
G:\mysql-8.0.23-winx64\bin\mysqld --datadir=G:\mysql-8.0.23-winx64\data80323308 --console --skip-grant-tables --shared-memory

然後再開一個視窗,執行下面命令
cd G:\mysql-8.0.23-winx64\bin
mysql> select user,host,authentication_string from mysql.user;
mysql> delete from mysql.user where user='root' ;
mysql> flush privileges;   --記得重新整理哈,如果提示無法建立
mysql> create user [email protected]'%' identified with mysql_native_password by 'root';
mysql> grant all on *.* to [email protected]'%' with grant option;
mysql> flush privileges;
mysql> drop user 'root'@'localhost';
mysql> flush privileges;
mysql> create user [email protected]'localhost' identified with mysql_native_password by 'root';
mysql> grant all on *.* to [email protected]'localhost' with grant option;
mysql> flush privileges;

QQ:2243967774,更多資料請關注公眾號:IT 邦德,專注於資料庫及程式開發,掃描加微信