Correct mysql syntax error when creating a user

2019-06-02 05:42发布

So im getting the very common ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) mysql error. I have tried all of the typical fixes on the forums, logging in via the mysql_safe method and then trying to reset my root password. However when doing so it returns with;

UPDATE user SET password=PASSWORD("PASSWORD")WHERE user="root";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

So as no rows were affected I assumed there was no user to actually change. I tried to create a user:

CREATE USER 'root'@'localhost' IDENTIFIED BY 'root'
    -> GRANT ALL ON *.* TO 'root'@'localhost'
    -> flush privileges;

However this returns with ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GRANT ALL ON *.* TO 'root'@'localhost'

Seeing what the current user is:

mysql> SELECT user();
+--------+
| user() |
+--------+
| root@  | 
+--------+

I assume the syntax error is the fact that nothing comes after the @ after "root". How can i edit this information to be root@localhost and correct the issue?

UPDATE

after carefully reading the mysqld docs i got to this section which worked perfectly.

Stop mysqld and restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.

Connect to the mysqld server with this command:

shell> mysql

Issue the following statements in the mysql client. Replace the password with the password that you want to use.

mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass')
    ->                   WHERE User='root';
mysql> FLUSH PRIVILEGES;

The FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.

2条回答
Bombasti
2楼-- · 2019-06-02 06:09

i've got that same error message (Access denied for user 'root'@'localhost' (using password: YES)) when i've tried to login in my freshly new installed mysql server.

I didn't know that when installing mysql-community-server (rpm) on redhat6.5, A superuser account 'root'@'localhost' is created and a password for the superuser is set and stored in the error log file and that to reveal it, i should use the :

  • shell > grep 'temporary password' /var/log/mysqld.log

I just check the password in the "mysqld.log" file and use it to login. Once in, i've changed that default password.

here is the link to the mysql's page for reference

查看更多
霸刀☆藐视天下
3楼-- · 2019-06-02 06:18

it should be three sql statements,

CREATE USER 'root'@'localhost' IDENTIFIED BY PASSWORD 'root';
GRANT ALL ON *.* TO 'root'@'localhost';
FLUSH PRIVILEGES;
查看更多
登录 后发表回答