--但是不輸入密碼卻可以登錄
[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)