以下經驗經本人親自試驗過,確實好用:
1. 打開mysql的配置文件my.ini,默認路徑是C:\ProgramData\MySQL\MySQLServer 5.7下。注意:如果找不到C:\ProgramData這個文件夾,可以在工具->文件夾選項下顯示隱藏的文件夾。然后,在配置文件中添加上skip-grant-tables,可以添加到文件的末尾或者是這添加到[mysqld]的下面。這樣做可以讓你跳過root密碼登錄到數據庫中。
2. 然后在命令行中重啟MYSQL服務,windows環境中命令是: net stop MySQL
net start MySQL
如果出現不是有效的命令,注意環境變量的配置;或者在命令行中轉到mysql的bin目錄下:cd C:\ProgramFiles\MySQL\MySQL Server 5.7\bin就可以了
-
修改root密碼
在命令行輸入 mysql –uroot–p
出現讓你輸入密碼,直接enter鍵就好了
這樣你就進入數據庫了,你可以show databases;查看目前有哪些數據庫;
接下來,選擇數據庫, 命令行輸入usemysql
現在我們執行如下語句把root密碼更新為123456:
updatemysql.user set authentication_string=password('123456') where user='root' andHost = 'localhost';
quit 退出MySQL
4、還原配置文件並重啟
再把上面配置文件中的的“skip-grant-tables”刪掉,重啟服務,這樣就OK了,可以用新的密碼登錄了
如下指令:
mysql_safe --skip-grant-tables --skip-networking &
mysql -uroot -p //可以空密碼進入。
update mysql.user set authentication_string=password('123qwe') where user='root';
flush privileges;
alter user 'root'@'localhost' identified by '123';
set password for 'root'@'localhost'=password('123')
quit;
重啟mysqld。
