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

1. Problem description

MySQL exits due to misuse of shortcut keys, and an error is displayed when logging in again: ERROR 1045 (28000): Access denied for user'root'@'localhost' (using password: YES)

2. Problem solving process

1. Open the /etc/mysql/mysql.conf.d/mysqld.cnf file, the command is as follows

$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

2. Find the [mysqld] section and add a line "skip-grant-tables"

3. Restart the mysql service, enter the mysql management command line with an empty password, switch to the mysql library, and the operation commands are as follows,

$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g. 
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update mysql.user set authentication_string=password('newpass') where user='root' and Host ='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> update user set plugin="mysql_native_password"; 
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit;
Bye

4. Go back to sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf, comment or delete the line "skip-grant-tables" just added.

5. Restart the mysql service sudo service mysql restart again, log in with the new password, and the modification is successful.

$ mysql -u root -p new_pass 
Welcome to the MySQL monitor. Commands end with ; or \g. 
mysql>

Guess you like

Origin blog.csdn.net/qq_42293496/article/details/84823679