1、 首先檢查mysql服務是否啟動,若已啟動則先將其停止服務,可在開始菜單的運行,使用命令:
net stop mysql
打開第一個cmd窗口,切換到mysql的bin目錄,運行命令:
mysqld --defaults-file="C:\ProgramData\MySQL\MySQL Server 5.7\my.ini" --skip-grant-tables
注釋:
該命令通過跳過權限安全檢查,開啟mysql服務,這樣連接mysql時,可以不用輸入用戶密碼。
2、打開第二個cmd窗口,連接mysql:
輸入命令:
mysql -uroot -p
出現:
Enter password:
在這里直接回車,不用輸入密碼。
然后就就會出現登錄成功的信息,
使用命令切換到mysql數據庫:
use mysql;
使用命令更改root密碼:
update user set password=password("新密碼") where user="root';5.7之前版本用這個命令
update mysql.user set authentication_string=password('新密碼') where user='root'; 這是5.7版本的命令
刷新權限:
flush privileges;
然后退出,重新登錄:
quit
重新登錄:
mysql -uroot -p
出現輸入密碼提示,輸入新的密碼即可登錄:
Enter password: ***********
顯示登錄信息: 成功 就一切ok了