解決mysql root賬戶連接報錯:1045 - Access denied for user root@localhost(using password: YES)

 

這個報錯是由於root密碼不對(可能你記錯了),重置root 密碼就可,方法如下,親試有效(Windows環境下)!

1 管理員身份進入cmd

點擊“開始”菜單——>輸入cmd(右鍵以管理員方式運行)

2 解決1045

2.1 往my.ini文件加 skip-grant-tables 指令

在mysql安裝目錄下找到my.ini文件,默認安裝在c盤,我安裝在e盤的,直接在指定盤下搜索:

打開編輯my.ini文件:

2.2 進入剛纔打開的cmd命令窗口(記住每一步用完cmd指令窗口,不必關閉,之後所有指令都可在同一個窗口執行

Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。

C:\windows\system32>net stop mysql    //停止mysql服務
MySQL 服務正在停止.
MySQL 服務已成功停止。


C:\windows\system32>net start mysql    //再開啓mysql服務,讓剛纔修改的文件生效
MySQL 服務正在啓動 .
MySQL 服務已經啓動成功。


C:\windows\system32>mysql -u root -p    //登錄root賬戶
Enter password:   //此時不需要輸入密碼,直接Enter就可登錄,但是很多操作受限。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;    //進入數據庫
Database changed


mysql> update user set password=password("123456") where user="root"; //給root設置新密碼,新密碼爲123456   
Query OK, 3 rows affected (0.09 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;    //刷新數據庫
Query OK, 0 rows affected (0.02 sec)

mysql> quit;    //退出
Bye

 2.3 再次編輯my.ini文件,刪除剛纔輸入的 skip-grant-tables 命令,保存,退出。進入剛纔的cmd窗口,執行以下命令,成功:

C:\windows\system32>net stop mysql    //再次停止mysql服務
MySQL 服務正在停止.
MySQL 服務已成功停止。


C:\windows\system32>net start mysql    //再次開啓mysql服務,使剛纔修改的my.ini文件生效
MySQL 服務正在啓動 .
MySQL 服務已經啓動成功。


C:\windows\system32>mysql -uroot -p    //登錄root賬戶,在下面輸入密碼123456,連接成功!
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

此時進mysql的圖形用戶界面 Navicat,也是可以連接成功,並可進行自定義操作。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章