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密碼的解決方案
--------------------------------------分割線 --------------------------------------
本文永久更新連結地址: