简体   繁体   中英

MySQL CentOS : Access denied for user ''@'localhost' to database

I installed MySQL in CentOS using sudo yum install mysql-server and logged into MySQL using mysql -u root -p and gave blank password. It's not allowing me to use MySQL database or select users/update password/create database.

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>

Error Again:

[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'

Please let me know how to resolve this.

  1. sudo /etc/init.d/mysql stop
  2. sudo mysqld_safe --skip-grant-tables &
  3. sudo mysql -u root
  4. Setup new MySQL root user password

    use mysql; update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root'; flush privileges; quit
  5. Stop MySQL Server: sudo /etc/init.d/mysql stop (if any error use sudo )

  6. Start MySQL server and test it: sudo mysql -u root -p

If you have no Password set, you can't give the -p Parameter with the mysql command. Start it only with mysql -u root

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM