Mysql Access denied for user 'root'@ '*.*.*.*' (using password: YES) exception handling

1. Abnormal error

PS C:\Users\10568> mysql -u root -p
Enter password: ****
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Access denied means denial of access, using password: NO/YES whether a password has been entered.

insert image description here

Two, the reason

One of the reasons is the mysql self-starting bug. The 3306 port number of mysql is occupied by itself. You need to close the mysql service and restart it. I can solve it by doing this. Of course, it may also be because the user who does not exist on the MySQL server tries to Access the database. You can refer to the stack overflow question to solve it.

The answer on stack overflow is that by default the root MySQL user is set to authenticate using the auth_socket plugin instead of using a password. In many cases this allows for greater security and usability, but it also complicates things when you need to allow external programs (eg, phpMyAdmin) to access users. In order to connect to MySQL as root using a password, you need to switch its authentication method from auth_socket to mysql_native_password.
Reference: Access Denied for User 'root'@'localhost' (using password: YES) -No Privileges?: https://stackoverflow.com/questions/17975120/access-denied-for-user-rootlocalhost-using-password- yes-no-privileges

3. Solutions

Check port 3306 and find that the PID process is 5296

netstat -ano|findstr 3306

insert image description here

Use the command taskkill /PID 5964 -t -fto kill the process, or end the task directly in the task manager

restart mysql afterwards

net start mysql

insert image description here

Just re-enter mysql

insert image description here

If it is an Ubuntu system, you can refer to the following answers:

MySQL Error: : 'Access denied for user ‘root’@‘localhost’:https://stackoverflow.com/questions/41645309/mysql-error-access-denied-for-user-rootlocalhost
In Ubuntu systems running MySQL 5.7 (and later versions), the root
MySQL user is set to authenticate using the auth_socket plugin by
default rather than with a password. This allows for some greater
security and usability in many cases, but it can also complicate
things when you need to allow an external program (e.g., phpMyAdmin)
to access the user.

In order to use a password to connect to MySQL as root, you will need
to switch its authentication method from auth_socket to
mysql_native_password. To do this, open up the MySQL prompt from your
terminal:

sudo mysql

Next, check which authentication method each of your MySQL user
accounts use with the following command:

SELECT user,authentication_string,plugin,host FROM mysql.user;

Output

±-----------------±------------------------------------------±----------------------±----------+ | user | authentication_string |
plugin | host |
±-----------------±------------------------------------------±----------------------±----------+ | root | |
auth_socket | localhost | | mysql.session |
*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys |
*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint |
*CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
±-----------------±------------------------------------------±----------------------±----------+ 4 rows in set (0.00 sec) In this example, you can see that the root
user does in fact authenticate using the auth_socket plugin. To
configure the root account to authenticate with a password, run the
following ALTER USER command. Be sure to change password to a strong
password of your choosing, and note that this command will change the
root password you set in Step 2:

ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY
‘password’;

Then, run FLUSH PRIVILEGES which tells the server to reload the grant
tables and put your new changes into effect:

FLUSH PRIVILEGES;

Check the authentication methods employed by each of your users again
to confirm that root no longer authenticates using the auth_socket
plugin:

SELECT user,authentication_string,plugin,host FROM mysql.user;

Output

±-----------------±------------------------------------------±----------------------±----------+ | user | authentication_string |
plugin | host |
±-----------------±------------------------------------------±----------------------±----------+ | root | *3636DACC8616D997782ADD0839F92C1571D6D78F |
mysql_native_password | localhost | | mysql.session |
*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys |
*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint |
*CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
±-----------------±------------------------------------------±----------------------±----------+ 4 rows in set (0.00 sec) You can see in this example output that the
root MySQL user now authenticates using a password. Once you confirm
this on your own server, you can exit the MySQL shell:

exit

Guess you like

Origin blog.csdn.net/qq_46207024/article/details/131177552