49

In some way I have managed to get this error when I try to access into MySQL via the command line:

[root@localhost ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

I have tried resetting the password without any luck using this HowTo.

I have uninstalled mysql completley and reinstalled but I still get asked for a password. I have no idea why this is the case!

Can someone please help me get a default install of MySQL.

Environment

Fedora Core 10, Full Root Access, Apache and PHP installed

Thank you for any help!!

EDIT

To all those that would like to save themselves a few hours of "blood coughing" - when you uninstall MySQl completely delete everything that is left behind. If you don't do this, it will never be a FRESH install.

OMG Ponies
  • 300,587
  • 73
  • 490
  • 482
Abs
  • 51,038
  • 92
  • 260
  • 394

7 Answers7

90

If you actually have set a root password and you've just lost/forgotten it:

  1. Stop MySQL
  2. Restart it manually with the skip-grant-tables option: mysqld_safe --skip-grant-tables

  3. Now, open a new terminal window and run the MySQL client: mysql -u root

  4. Reset the root password manually with this MySQL command: UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root'; If you are using MySQL 5.7 (check using mysql --version in the Terminal) then the command is:

    UPDATE mysql.user SET authentication_string=PASSWORD('password')  WHERE  User='root';
    
  5. Flush the privileges with this MySQL command: FLUSH PRIVILEGES;

From http://www.tech-faq.com/reset-mysql-password.shtml

(Maybe this isn't what you need, Abs, but I figure it could be useful for people stumbling across this question in the future)

Rounak
  • 577
  • 1
  • 7
  • 21
David Z
  • 116,302
  • 26
  • 230
  • 268
  • 1
    In my case, mysql 5.5 installation on windows 7 didn't prompt for password and I had to use the solution above. Please note that you need to shutdown mysqld using 'mysqladmin -u root -p shutdown' because Ctrl-C does not stop mysqld and probably you don't want to kill it. – Champ Apr 17 '12 at 04:58
  • For MySQL 5.7, there is no password column in the user table. So replace, step number 4. with the code: UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root'; – Rounak May 04 '15 at 16:50
  • This works but only as long as you do it each time yo start mysql. If I reboot the computer the same error happens again and I have to do all this all over again. I was wondering if there is a permanent fix. – Alexander Nenartovich Mar 25 '21 at 18:59
14

Try connecting without any password:

mysql -u root

I believe the initial default is no password for the root account (which should obviously be changed as soon as possible).

Chad Birch
  • 69,230
  • 22
  • 145
  • 148
4

use this command to check the possible output

mysql> select user,host,password from mysql.user;

output

mysql> select user,host,password from mysql.user;
+-------+-----------------------+-------------------------------------------+
| user  | host                  | password                                  |
+-------+-----------------------+-------------------------------------------+
| root  | localhost             | *8232A1298A49F710DBEE0B330C42EEC825D4190A |
| root  | localhost.localdomain | *8232A1298A49F710DBEE0B330C42EEC825D4190A |
| root  | 127.0.0.1             | *8232A1298A49F710DBEE0B330C42EEC825D4190A |
| admin | localhost             | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| admin | %                     |                                           |
+-------+-----------------------+-------------------------------------------+
5 rows in set (0.00 sec)
  1. In this user admin will not be allowed to login from another host though you have granted permission. the reason is that user admin is not identified by any password.
  2. Grant the user admin with password using GRANT command once again

    mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED by 'password'
    

then check the GRANT LIST the out put will be like his

mysql> select user,host,password from mysql.user;

+-------+-----------------------+-------------------------------------------+
| user  | host                  | password                                  |
+-------+-----------------------+-------------------------------------------+
| root  | localhost             | *8232A1298A49F710DBEE0B330C42EEC825D4190A |
| root  | localhost.localdomain | *8232A1298A49F710DBEE0B330C42EEC825D4190A |
| root  | 127.0.0.1             | *8232A1298A49F710DBEE0B330C42EEC825D4190A |
| admin | localhost             | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| admin | %                     | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------------------+-------------------------------------------+
5 rows in set (0.00 sec)

if the desired user for example user 'admin' is need to be allowed login then use once GRANT command and execute the command.

Now the user should be allowed to login.

Maxime
  • 7,283
  • 4
  • 48
  • 49
  • PRIVILEGES is plural: GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED by 'password' – i_a May 03 '17 at 14:41
  • MySQL 5.7*: `mysql> select user,host,password from mysql.user; ERROR 1054 (42S22): Unknown column 'password' in 'field list'` – Dave Everitt May 25 '17 at 12:12
2

The current root password must be empty. Then under "new root password" enter your password and confirm.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
1

I had the same error, but it was because password_expired was set to Y. You can fix that issue by following the same approach as the currently accepted answer, but instead executing the MySQL query:

UPDATE mysql.user SET password_expired = 'N' WHERE User='root';
Brandon
  • 2,238
  • 22
  • 32
0
  1. Go to mysql console
  2. Enter use mysql;
  3. UPDATE mysql.user SET Password= PASSWORD ('') WHERE User='root' FLUSH PRIVILEGES; exit PASSWORD ('') is must empty
  4. Then go to wamp/apps/phpmyadmin../config.inc.php
  5. Find $cfg ['Servers']['$I']['password']='root';
  6. Replace the ['password'] with ['your old password']
  7. Save the file
  8. Restart the all services and goto localhost/phpmyadmin
0

I could not connect to MySql Administrator. I fixed it by creating another user and assigning all the permissions.

I logged in with that new user and it worked.