MySQL密碼正確卻無法本地登入怎麼辦

來源:互聯網
上載者:User

   MySQL root密碼正確,卻怎麼也無法從本地登入MySQL,提示

  1 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

  這裡後來經高人指點才發現mysql庫中的user表缺少一個root指向host:localhost的資料項目,只有一個root指向host:主機名稱的資料項目,故怎麼也無法利用root賬戶登入MySQL。

  總結一點就是root賬戶缺失了訪問localhost主機的賬戶資訊,導致無法本地登入。

  那有什麼辦法恢複root登入呢?

  這裡記錄一下今天遇到的糾結事情:

  首先kill掉MySQL進程然後在啟動mysql的參數中加入

  --skip-grant-tables

  會發現這時無密碼就可以登入mysql了。

  當然我們還必須修複root賬戶丟失的資料項目。

  這裡有兩種解決方案:

  第一種是因為root賬戶初始的時候有3條記錄,包含root對應localhost,hostname,127.0.0.1三條賬戶資料,我們可以update host為其他兩項中一項為localhost即可。

  第二種是直接insert一條記錄,host為localhost即可

  總結一下:即使root的host包含了主機名稱,127.0.0.1那麼依然是無法正常登入的,這裡必須要有localhost的host才行。

  如果上面辦法還是無法正常登入我們可嘗試另一種辦法

  在本地用mysql命令直接斷行符號可以進入mysql,但是裡面只有test和information_schema資料庫,沒有mysql等資料庫,使用use mysql報如下錯:

  mysql> use mysql

  ERROR 1044 (42000): Access denied for user "@'localhost' to database 'mysql'

  意思是說沒有指定user,沒有許可權訪問資料庫mysql。

  那麼用root登入呢,輸入正確的密碼報如下錯:

  [root@228827 ~]# mysql -uroot -p123456

  ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

  密碼正確的情況下,mysql資料庫已經禁止了root使用者在本地的登入許可權了。

  使用root使用者通過主機127.0.0.1登入就可以正常進入mysql,127.0.0.1和localhost對mysql資料庫來講是不同的主機,

  [root@228827 ~]# mysql -uroot -p123456 -h 127.0.0.1

  這讓我想起了mysql下的user表。

  我們要進mysql看user表,一種方法可以通過上面的命令,如果不行,可以用下面的命令啟動資料庫,預設密碼進入

代碼如下  

[root@228827 ~]# /etc/init.d/mysql stop
[root@228827 ~]# /usr/local/mysql/bin/mysqld_safe –skip-grant-tables &
[root@228827 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.1.57 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> use mysql
Database changed
mysql> select user,host,password from user where user='root';
+——+——————-+——————————————-+
| user | host | password |
+——+——————-+——————————————-+
| root | % | *A50E066E106320CF4142 |
| root | centos | *A50E066E106320CF4142 |
| root | 127.0.0.1 | *A50E066E1063608320CF4142 |
+——+——————-+——————————————-+
3 rows in set (0.12 sec)

  發現user表host欄位中沒有localhost,但是我的理解是%代表所有的主機都能登入的,為什麼localhost不能呢,同樣的情況我在5.0.45版的mysql上面做實驗就不會發生localhost無法登入,我當前用的是5.1.57版的,難道是版本的問題?

  接下來的修改很明顯了:

代碼如下  
mysql> update user set host='localhost' where user='root' and host='%';
mysql> flush privileges;

  OK,退出mysql,重啟mysql就解決問題了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.