CentOS安裝mysql之後,之前設定了密碼,但輸入mysql -u -p後,會直接進入mysql,而輸入mysql -uroot -p,則需要輸入密碼,這是為什麼呢?
[[email protected] ~]# mysql -u -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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> quit
Bye
[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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輸入:
select * from mysql.user where user='';
mysql> select * from mysql.user where user='';
+-----------+------+----------+-------------+-------------+-------------+-------------+----------------+-----------+------------+-----------------+------------+------------+--------------+----------------+-----------------+------------------+------------------+----------------+--------------------+--------------+----------+------------+-------------+--------------+---------------+-----
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_pr_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Supercute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routit_priv | Trigger_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_
+-----------+------+----------+-------------+-------------+-------------+-------------+----------------+-----------+------------+-----------------+------------+------------+--------------+----------------+-----------------+------------------+------------------+----------------+--------------------+--------------+----------+------------+-------------+--------------+---------------+-----
| localhost | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 |
| hadoop01 | | | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 |
+-----------+------+----------+-------------+-------------+-------------+-------------+----------------+-----------+------------+-----------------+------------+------------+--------------+----------------+-----------------+------------------+------------------+----------------+--------------------+--------------+----------+------------+-------------+--------------+---------------+-----
2 rows in set (0.00 sec)
查詢有結果,然後進行下一步。
use mysql;
delete from user where user = '';
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> delete from user where user = '';
Query OK, 2 rows affected (0.00 sec)
删除了多餘的空白賬戶, 然後進行下一步。
flush privileges;
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
重載一次權限表,最後用
service mysqld restart
[[email protected] ~]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
重新開機mysql服務,問題得到解決,
[[email protected] ~]# mysql -u -p
ERROR 1045 (28000): Access denied for user '-p'@'localhost' (using password: NO)
[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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> quit;
Bye
注意:
1、一定要記住重新開機mysql服務,否則不會生效。
2、msyql的使用者表在mysql資料庫中的user表中,主要字段有host,user,password等,作為mysql用的管理的主要表。