4

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.

dodgerogers747
  • 3,325
  • 9
  • 34
  • 55
  • 1
    Why don't you use `SET PASSWORD` instead? That does the `flush privileges` automatically: http://dev.mysql.com/doc/refman/5.5/en/set-password.html –  Jan 24 '13 at 08:29
  • You can't use SET PASSWORD because anonymous users cannot change passwords. – Rudy Hinojosa Apr 03 '18 at 15:10
  • did this sollution under UPDATE actualy give you a different output then: Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 ? Can you add that line to make it compleete? Mine is still the same what makes me currious if thats standard behavior... – novski Jul 23 '18 at 18:55

2 Answers2

9

it should be three sql statements,

CREATE USER 'root'@'localhost' IDENTIFIED BY PASSWORD 'root';
GRANT ALL ON *.* TO 'root'@'localhost';
FLUSH PRIVILEGES;
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 6
    hmm the first line returns - Password hash should be a 41-digit hexadecimal number ? – dodgerogers747 Jan 18 '13 at 09:22
  • 1
    I got the same error using this @dodgerogers747 ERROR 1827 (HY000): The password hash doesn't have the expected format. Check if the correct password algorithm is being used with the PASSWORD() function. – dlite922 Dec 21 '16 at 17:43
  • 3
    CREATE USER 'root'@'localhost' IDENTIFIED BY 'root'; --only without "PASSWORD" it worked for me – heringer Mar 10 '21 at 19:27
0

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

yanstp
  • 1