MySQL 'root'@'localhost' cannot log in

This morning my colleague said that the MySQL root account could not be logged in. I tried it 

#mysql -u root -p 

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


Because a colleague left after the year, my first reaction was who changed the root password? To reset the password as forgot root password: 

#/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


I still can't log in with the new password, the prompt is the same as above. Log in with a non-root account and check the user table: 

mysql> select user,host from user; 

+———–+———+ 

| user   | host | 

+———–+———+ 

| root  | 127.0.0.1 | 

| night | % | 

+———–+———+


Suspect the default localhost is not mapped to 127.0.0.1? Try #mysql -u root -p xxxx -h 127.0.0.1, you can log in as expected. 

The students who configured the database before did not authorize 'root'@'localhost' and '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;


Query the user table again: 

write picture description here 

QQ browser screenshot 232 screen unnamed.png

Then #mysql -u root -p xxxx, the login is successful!


I checked the difference between mysql -h localhost and mysql -h 127.0.0.1. Connecting to mysql through localhost uses UNIX socket, while connecting to mysql through 127.0.0.1 uses TCP/IP. Check out the status: 

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



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304129&siteId=291194637