MySQL密碼正確卻沒法本地登陸,ERROR 1045 (28000): Access denied for user 'root'@'localhost' (u...

MySQL密碼正確卻沒法本地登陸mysql

報錯以下:sql

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

測試發現mysql庫中的user表缺乏一個root指向host:localhost的數據項,只有一個root指向host:主機名的數據項,故怎麼也沒法利用root帳戶登陸MySQL。
總結一點就是root帳戶缺失了訪問localhost主機的帳戶信息,致使沒法本地登陸。vim

解決方法:
首先kill掉MySQL進程而後在啓動mysql的參數中加入跳過密碼問題方式,以下:
vim /etc/my.cnf
並在[mysql]下面加上skip-grant-tables,
這個用於跳過密碼問題,可是這並不能完全解決。dom

在本地用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數據庫來說是不一樣的主機,測試

# mysql -uroot -p123456 -h 127.0.0.1
這讓我想起了mysql下的user表。
咱們要進mysql看user表,一種方法能夠經過上面的命令,若是不行,能夠用下面的命令啓動數據庫,缺省密碼進入ui

步驟:orm

[root@localhost ~]# mysql -uroot -p12345 -h 127.0.0.1
mysql> use mysql
Database changed
mysql> select user,host,password from user where user='root';
+------+-----------------------+-------------------------------------------+
| user | host | password |
+------+-----------------------+-------------------------------------------+
| root | % | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | localhost.localdomain | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | 127.0.0.1 | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | ::1 | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
+------+-----------------------+-------------------------------------------+
4 rows in set (0.02 sec)進程

mysql> update user set host='localhost' where user='root' and host='%';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0ip

mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Bye
OK,退出mysql,重啓mysql就解決問題了

先後對比一下:
[root@localhost ~]# mysql -uroot -p
mysql> use mysql;
Database changed
mysql> select user,host,password from user where user='root';
+------+-----------------------+-------------------------------------------+
| user | host | password |
+------+-----------------------+-------------------------------------------+
| root | localhost | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | localhost.localdomain | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | 127.0.0.1 | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
| root | ::1 | *00A51F3F48415C7D4E8908980D443C29C69B60C9 |
+------+-----------------------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> OK!

相關文章
相關標籤/搜索