Database error 1045-Access denied for user 'root'@'localhost' (using password: YES) solution

Preface

Today, when I was using my computer and wanted to use Navicat to open the MySQL database, I found that I could not log in and prompted 1045 Access denied for user ‘root’@‘localhost’ (using password: YES). The answers on the Internet are all "the same, and the descriptions are not simple and clear enough." Today I will give an explanation of the reasons for this problem and two ways to solve this problem!
Insert image description here

One, cause:

1. The database password has been tampered with!

Yes, that's the reason. But many people will immediately refute, my own computer has been used well, how could it be tampered with? Hackers' methods are very powerful. Tampering with your database password is not the main purpose. The purpose is to enter your database after tampering, back up the database file and then delete the local database. The main purpose is to blackmail you for Bitcoin.

Insert image description here

2. The database permissions have changed!

This reason has a lot to do with the change of account permissions on your machine. There are many actual scenarios, so I won’t give examples.

2. Solution

1. Method: Edit the mysql configuration file my.ini

In the installation directory of mysql, different computers may be different. Reference directory:
①D:\Program Files\MySQL\MySQL Server 5.0\my.ini;
②C:\ProgramData\MySQL\MySQL Server 8.0\my.ini;

在[mysqld]这个条目下加入 skip-grant-tables 保存退出后重启mysql
PS:
8.0的版本不支持,亲测8.03 , to avoid the following errors:
mysql: [ERROR] unknown option ‘–skip-grant-tables’.

2. The steps are as follows:

1. Open cmd, click "Start" -> "Run" (shortcut key Win+R).
2. Stop: Enter net stop mysql
3. Start: Enter net start mysql
4. Skip permissions: Entermysqld --console --skip-grant-tables --shared-memory

Insert image description here

You can also find the MySQL service in Computer-Management-Services 手动进行重启服务. As shown in the picture:

Insert image description here

At this time, enter mysql -u root -p in cmd to log in without a password. When password: appears, just press Enter to enter. ERROR 1045 (28000) will not appear, but many operations will be restricted because there is no grant permission. .

Insert image description here

3. Summary: The solution to the problem that the command line mysqld -skip-grant-tables fails to log in under mysql8.0 version

1. Log in to cmd with administrator rights. If you don’t know how to log in with an administrator, please search cmd and right-click the search result.

2. Enter the command line: net stop mysql; and then prompt. The service is stopping --> The service has been stopped. If other errors occur, please refer to Baidu.

  这只是一个示例,请在服务中查看服务具体名称,比如我的就是mysql8.0.33。

  服务不会打开的请 win+R --> services.msc --> 回车,找到mysql开头的服务名。

3、由于mysqld –skip-grant-tables实测在mysql8.0中已失效,现使用mysqld --console --skip-grant-tables --shared-memory

4. Open another cmd and use mysql to log in directly without password.

After successful login, continue to follow the following process:

4. Set a new password

1. Enter the mysql database:

mysql> use mysql;

Insert image description here

2. Set a new password for the root user:

mysql> update user set authentication_string=password(“新密码”) where user=“root”;
Query OK, 1 rows affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Insert image description here

PS: If the following error occurs:

mysql> ERROR 1054 (42S22): Unknown column ‘password’ in ‘field list’

是因为mysql数据库下已经没有password这个字段了,按照我的步骤把password字段改成了authentication_string

3. Refresh the database

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Insert image description here

4. Exit mysql:

mysql> quit;
Bye

Insert image description here

After making the changes, save and exit, restart the mysql service, and then connect with Navicat again.

Guess you like

Origin blog.csdn.net/y2020520/article/details/132136255