MySQL CentOS:用户访问被拒绝'' localhost'到数据库

时间:2015-10-26 14:21:21

标签: php mysql database centos

我使用sudo yum install mysql-server在CentOS中安装了MySQL,并使用mysql -u root -p登录到MySQL,并输入了空白密码。它不允许我使用MySQL数据库或选择用户/更新密码/创建数据库。

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> use mysql;
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

mysql> UPDATE user SET Password=PASSWORD('*****') WHERE user='root';
ERROR 1046 (3D000): No database selected

mysql> show grants;
+--------------------------------------+
| Grants for @localhost                |
+--------------------------------------+
| GRANT USAGE ON *.* TO ''@'localhost' |
+--------------------------------------+
1 row in set (0.00 sec)


[centos@****~]$ sudo /sbin/service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
[centos@****~]$ sudo mysqld_safe
151026 14:40:32 mysqld_safe Logging to '/var/log/mysqld.log'.
151026 14:40:32 mysqld_safe A mysqld process already exists
[centos@*****~]$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 152
Server version: 5.6.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> use mysql;
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
mysql>

再次出错:

[centos@~]$ sudo /sbin/service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
[centos~]$ sudo mysqld_safe --skip-grant-tables &
[1] 25678
[centos~]$ 151026 14:53:07 mysqld_safe Logging to '/var/log/mysqld.log'.
151026 14:53:07 mysqld_safe A mysqld process already exists

[1]+  Exit 1                  sudo mysqld_safe --skip-grant-tables
[centos@~]$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 160
Server version: 5.6.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> use mysql;
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

请告诉我如何解决此问题。

2 个答案:

答案 0 :(得分:0)

  1. sudo /etc/init.d/mysql stop
  2. sudo mysqld_safe --skip-grant-tables &
  3. sudo mysql -u root
  4. 设置新的MySQL root用户密码

    use mysql;
    update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
    flush privileges;
    quit
    
  5. 停止MySQL服务器:sudo /etc/init.d/mysql stop(如果有任何错误使用sudo

  6. 启动MySQL服务器并对其进行测试:sudo mysql -u root -p

答案 1 :(得分:0)

如果您没有设置密码,则无法使用mysql命令为-p参数提供。仅使用mysql -u root

启动它