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

來源:互聯網
上載者:User

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

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

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

Windows下忘記MySQL root密碼的解決方案 

--------------------------------------分割線 --------------------------------------

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.