简体   繁体   中英

ERROR 1045 (28000): Access denied for user 'root'@'localhost'

so i installed the MySQL application for the first time. firstly i saw the command line client is not opening so i searched for solutions. they said i must go to the bin directory and run it manually. and after i run the cmd mysql -uroot -p and run it and enter password, it gives me the error: ERROR 1045 (28000): Access denied for user 'root'@'localhost' i tried every single solution on stackoverflow including disabling permissions, running manually which i mentioned above, starting the service from service.msc, running it with password and without.... it just doesnt want to work. appreciate any help in advance.

GENERIC MYSQL INFO

To start with, read the mysql manual: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

The steps will show you how to shut down the service and start it with an overriding command that doesn't require passwords, then you reset the password. From the manual:

Stop the MySQL server, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges and disables account-management statements such as ALTER USER and SET PASSWORD . 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 MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables :

shell> mysql

In the mysql client, tell the server to reload the grant tables so that account-management statements work:

mysql> FLUSH PRIVILEGES;

Then change the 'root'@'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different hostname part, modify the instructions to use that hostname.

MySQL 5.7.6 and later:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5 and earlier:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

Or directly on the user table:

UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';

XAMPP SPECIFIC

Stop the MySQL service. Open a command window . Change to the XAMPP MySQL directory:

> cd \xampp\mysql\bin\

Run the service without security (note you are running mysqld, not mysql):

> mysqld.exe --skip-grant-tables

The MySQL service will be running in this window, so open another command window and switch to the XAMPP MySQL directory:

> cd \xampp\mysql\bin\

Run the MySQL client:

> mysql

Update the password:

mysql> UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';

Exit MySQL:

mysql> \q

Use task manager to cancel the mysqld.exe that is still running. Restart the mysql service.

The error looks something like this

mysql -uroot -proot

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

At the initial start-up of the server the following happens, given that the data directory of the server is empty:

  • The server is initialized.
  • SSL certificate and key files are generated in the data directory.
  • The validate_password plugin is installed and enabled.
  • The superuser account 'root'@'localhost' is created. The password for the superuser is set and stored in the error log file.

To reveal it, use the following command:

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

Change the root password as soon as possible by logging in with the generated temporary password and set a custom password for the superuser account:

shell> mysql -u root -p #Login to root user with some password

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Your New Password';

I got the answer myself. Seemingly, if you get this error, it means that you need to reset your password. You can learn how to do that in MySQL from this link .

And don't forget to change the 5.7 version with your currently installed version in using commands (mine was 8.0). After that, everything was working fine for me.

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