MySQL 'root'@'localhost'無法登入,mysqllocalhost

來源:互聯網
上載者:User

MySQL 'root'@'localhost'無法登入,mysqllocalhost

今天早上同事說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

相關文章

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.