1、以管理員身份打開cmd窗口,定位到MySQL安裝目錄下的bin目錄,輸入net stop mysql 回車,關閉MySQL數據庫。
2、輸入mysqld --console --skip-grant-tables --shared-memory(mysqld –skip-grant-tables在MySQL8.0中已失效)回車。
(--skip-grant-tables 的意思是啓動 MySQL 服務的時候跳過權限表認證)
3、重新打開一個命令行窗口,定位到MySQL安裝目錄下的bin目錄,輸入mysql回車。
4、輸入use mysql 回車。
5、輸入以下命令,將authentication_string字段置爲空(注:在MySQL8.0以上版本,無法使用 update user set password=password('newpassword') where user='root';)。
update user set authentication_string='' where user='root';
6、輸入以下命令修改密碼
alter user 'root'@'localhost' identified by '123456';
我這裏報錯了:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement。
解決方法,先輸入 flush privileges; 命令,刷新一下權限表。
再重新輸入修改密碼的命令,成功了。
7、再刷新一下權限表:
8、輸入 net start mysql 重新啓動MySQL,輸入mysql -u root -p 輸入新密碼登錄MySQL,成功。