Mysql 5.5 原始碼安裝後建立使用者報錯"ERROR 1045 (28000): Access denied for user"

feelpurple發表於2016-04-12
安裝MySQL後,root使用者可設定密碼登入,其他新建的使用者不能透過密碼登入,只能不輸入密碼登入,而且對新建使用者的授權均無效。


--建立一個庫和使用者
mysql> create database fire;
Query OK, 1 row affected (0.00 sec)

mysql> create user neo identified by 'Mysql#2015';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select,create,update,delete on fire.* to neo;
Query OK, 0 rows affected (0.00 sec)

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

--使用新使用者登入報錯

[root@localhost data]# ./bin/mysql -u neo -p"Mysql#2015"
ERROR 1045 (28000): Access denied for user 'neo'@'localhost' (using password: YES)

--但是不輸入密碼卻可以登入

[root@localhost software]# ./bin/mysql -uneo
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.48-log production environment

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

Oracle is a 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> show grants;
+--------------------------------------+
| Grants for @localhost                |
+--------------------------------------+
| GRANT USAGE ON *.* TO ''@'localhost' |
+--------------------------------------+
1 row in set (0.00 sec)

報錯原因Mysql使用原始碼安裝後,資料庫中存在匿名使用者,可以透過任意使用者名稱不輸入密碼即可登入資料庫。

mysql> select host,user,password  from mysql.user where (user) not in ('root');
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| localhost             |      |                                           |
| localhost.localdomain |      |                                           |
| %                     | neo  | *CC4C777F1511C297751B78287D6E05345D227819 |
| %                     | test | *443EF031E558E317B19BAD70BDF4E25FA73A89A7 |
+-----------------------+------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> select host,user,password  from mysql.user where (user) not in ('root','neo','test');
+-----------------------+------+----------+
| host                  | user | password |
+-----------------------+------+----------+
| localhost             |      |          |
| localhost.localdomain |      |          |
+-----------------------+------+----------+
2 rows in set (0.00 sec)

解決方法刪除匿名使用者

mysql> delete  from mysql.user where (user) not in ('root','neo','test');
Query OK, 2 rows affected (0.00 sec)

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

之後新建立的使用者即可正常登入到資料庫

[root@localhost software]# ./bin/mysql -uneo -p'Mysql#2015'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48-log production environment

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

Oracle is a 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> show grants;
+-----------------------------------------------------------------+
| Grants for neo@%                                                |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'neo'@'%' IDENTIFIED BY PASSWORD <secret> |
| GRANT SELECT, INSERT, DELETE, CREATE ON `fire`.* TO 'neo'@'%'   |
+-----------------------------------------------------------------+
2 rows in set (0.00 sec)

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

相關文章