今天在鏈接mysql的時候,發現總是提示鏈接到localhost失敗,找了緣由,是前兩天密碼被非法修改了。如今不知道root登錄密碼,又想鏈接到數據庫,網站找了下方法,結合本身的實踐,總結以下:
一、在my.init文件中的[mysqld]下面加上skip-grant-tables,故名思議,就是跳過受權驗證表,直接操做數據庫
二、從新啓動mysql (net start/stop mysql)
三、在MySQL Command Line Client用root以空密碼登錄。 或者在命令行中敲命令"mysql"(path設置)
四、修改密碼
mysql> use mysql;
Database changed
mysql> UPDATE user SET Password = password ('root') WHERE User = 'root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
五、查看是否修改爲功
mysql> select password from user where user='root';
+-------------------------------------------+
| password |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
2 rows in set (0.00 sec)
六、刷新權限,mysql新設置用戶或更改密碼後需用flush privileges刷新MySQL的系統權限相關表,不然會出現拒絕訪問,還有一種方法,就是從新啓動mysql服務器,來使新設置生效。並退出
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
七、在my.int中,把skip-grant-tables刪掉,重啓mysql
八、再次登錄數據庫,成功
C:\Windows\system32>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.57-community MySQL Community Server (GPL)