關於root(其他)使用者拒絕登陸mysql的處理方法

like052629發表於2015-04-28

關於root(其他)使用者拒絕登陸mysql的處理方法


1.1.   關於root(其他)使用者拒絕登陸的處理方法
問題描述:
[root@gflinux3 mysql-install]# mysql -uroot-p
Enter password:
ERROR 1045 (28000): Access denied for user'root'@'localhost' (using password: YES)

1.1.1.      跳過許可權表登陸
         (1)重啟資料庫,跳過許可權表方式登陸:
[root@gflinux3 mysql-install]# /etc/init.d/mysqldstart --skip-grant-table
Starting MySQL.........                                    [  OK  ]
[root@gflinux3 mysql-install]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.5.37-log Sourcedistribution


Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.


Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.


Type 'help;' or 'h' for help. Type 'c' toclear the current input statement.
mysql>
         (2)更該root密碼
mysql> update user set password=PASSWORD('123456') where USER='root';
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
         (3)重啟資料庫並重新登陸
[root@gflinux3 mysql-install]#service mysqlrestart
         如果問題還是不能解決,繼續向下看。


1.1.2.      遠端可以本地無法登陸
         本地無法登陸,但是遠端或者其他使用者可以登陸,root無法登陸。
[root@gflinux3 mysql-install]# mysql -urgf-p
Enter password:
ERROR 1045 (28000): Access denied for user'rgf'@'localhost' (using password: YES)
         (1)檢視user表中分佈使用者
mysql> select user,host,password from user;
+------+------+----------------------------------------------------------------------------+
| user | host          | password                                                                       |
+------+------+----------------------------------------------------------------------------+
| root | %             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | gflinux3    |
| root | 127.0.0.1 |
| root | ::1           |
| rgf  | %              | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+------+---------------------------------------------------------------------------+
2 rows in set (0.00 sec)
         (2)檢視檔案/etc/hosts
[root@gflinux3 mysql-install]# vi/etc/hosts


# Do not remove the following line, orvarious programs
# that require network functionality willfail.
127.0.0.1      gflinux3 localhost.localdomain localhost
::1            localhost6.localdomain6 localhost6
192.168.6.103   gflinux3
         (3)分析
         mysql初始化時root使用者根據hosts配置檔案中的主機及主機名在user表中產生上述記錄(127.0.0.1,gflinux3,localhost,::1,192.168.6.103),而安裝完資料庫,使用mysqladmin初始化密碼時,是對user表中localhost,即上述表中第一條記錄做的修改:
mysql> select user,host,password from user;
+------+------+----------------------------------------------------------------------------+
| user | host          | password                                                                       |
+------+------+----------------------------------------------------------------------------+
| root | localhost   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+------+---------------------------------------------------------------------------+
         而其他“主機”密碼都為空,這時再以root及其密碼登陸時當然無法登陸。
         更值得提醒的時,這些“主機”的存在,在安全上及其危險。
         (4)處理方法
         首先,修改hosts檔案為:
[root@gflinux3 mysql-install]# vi/etc/hosts


# Do not remove the following line, orvarious programs
# that require network functionality willfail.
127.0.0.1      gflinux3 localhost.localdomain localhost
#::1           localhost6.localdomain6 localhost6
192.168.6.103   gflinux3
         其次,刪除user表中所有相關主機記錄。
mysql>delete  from user where hostin('gflinux3','127.0.0.1','::1','localhost');
Query OK,5 rows affected (0.01 sec)


mysql>update privileges;


mysql>select user,host,password from user;
+------+------+-------------------------------------------+
| user |host | password                                  |
+------+------+-------------------------------------------+
| root |%    |*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
|rgf  | %    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
+------+------+-------------------------------------------+
2 rows inset (0.00 sec)
         (3)登陸驗證
[root@gflinux3mysql-install]# mysql -uroot -p
Enterpassword:
Welcometo the MySQL monitor.  Commands end with; or g.
YourMySQL connection id is 22
Serverversion: 5.5.37-log Source distribution


Copyright(c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.


Oracle isa registered trademark of Oracle Corporation and/or its
affiliates.Other names may be trademarks of their respective
owners.
Type'help;' or 'h' for help. Type 'c' to clear the current input statement.


mysql>
         當然,不建議使root使用者從遠端連線,這樣很危險,因此,作如下操作取消使用root遠端連線:
mysql>update user set host='localhost' where user='root';
Query OK,1 row affected (0.00 sec)
Rowsmatched: 1  Changed: 1  Warnings: 0


mysql>flush privileges;
Query OK,0 rows affected (0.00 sec)

1.1.3.      關於登陸
         (1)如果host欄位更新為localhost:
         如下方式可以登陸成功:
[root@gflinux3mysql-install]# mysql -uroot -p
[root@gflinux3mysql-install]# mysql -hlocalhost -uroot -p
         但是如下方式無法登陸:
[root@gflinux3mysql-install]# mysql -h127.0.0.1 -uroot -p
[root@gflinux3mysql-install]# mysql -h gflinux3 -uroot -p
[root@gflinux3mysql-install]# mysql -h192.168.6.103 -uroot -p
         原因是user表中host欄位值只有記錄localhost。其他登陸方式無法在user表中得到認證。
         (2)如果host欄位更新為%
         所有的方式都可以登陸:
[root@gflinux3mysql-install]# mysql -uroot -p
[root@gflinux3mysql-install]# mysql -hlocalhost -uroot -p
[root@gflinux3mysql-install]# mysql -h127.0.0.1      -uroot -p
[root@gflinux3mysql-install]# mysql -h gflinux3 -uroot -p
[root@gflinux3mysql-install]# mysql -h192.168.6.103 -uroot -p
         原因是user表中host欄位值匹配所有主機。(127.0.0.1,gflinux3,localhost,::1,192.168.6.103)這些都是指的一臺主機,在user表中可以透過認證,從而可以登陸。



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28258625/viewspace-1608816/,如需轉載,請註明出處,否則將追究法律責任。

相關文章