1. 程式人生 > >MySQL ‘root‘@‘localhost‘無法登錄

MySQL ‘root‘@‘localhost‘無法登錄

今天早上同事說MySQL root賬號登

今天早上同事說MySQL root賬號登錄不上了。我試了一下

#mysql -u root -p

提示”Access denied for user ‘root’@’localhost’ (using password: YES)”


因為年後有同事離職,我第一反應是誰修改了root密碼?按照忘記root密碼來重置一下密碼:

#/etc/init.d/mysql stop

#mysqld_safe –skip-grant-tables &

#mysql -uroot -p

mysql>update mysql.user set password=password(‘mypassword’) where user=’root’;

mysql>flush privileges;

mysql>quit


用新密碼還是無法登錄,提示跟上面一樣。換一個非root賬號登錄,查看一下user表:

mysql> select user,host from user;

+———–+———+

| user | host |

+———–+———+

| root | 127.0.0.1 |

| night | % |

+———–+———+


懷疑默認的localhost沒有映射到127.0.0.1?試試#mysql -u root -p xxxx -h 127.0.0.1,果然可以登錄。

之前配置數據庫的同學沒有給’root’@’localhost’和’root’@’ip’授權。

grant all privileges on . to ‘root’@’localhost’ identified by ‘mypassword’ with grant option;

grant all privileges on . to ‘root’@’118.192.91.xxx’ identified by ‘mypassword’ with grant option;


再查詢一下用戶表:

這裏寫圖片描述

技術分享圖片

然後#mysql -u root -p xxxx,登錄成功!


查了一下mysql -h localhost和mysql -h 127.0.0.1的區別,通過localhost連接到mysql是使用UNIX socket,而通過127.0.0.1連接到mysql是使用TCP/IP。看看狀態:

mysql -h localhost > status

Connection id: 639

Current database: mysql

Current user: root@localhost

SSL: Not in use

Current pager: stdout

Using outfile: ”

Using delimiter: ;

Server version: 5.6.15-log Source distribution

Protocol version: 10

Connection: Localhost via UNIX socket


mysql -h 127.0.0.1 > status

Connection id: 640

Current database: mysql

Current user: root@localhost

SSL: Not in use

Current pager: stdout

Using outfile: ”

Using delimiter: ;

Server version: 5.6.15-log Source distribution

Protocol version: 10

Connection: 127.0.0.1 via TCP/IP



MySQL ‘root‘@‘localhost‘無法登錄