Solve MySQL login error: 'Access denied for user 'root'@'localhost'

Solve MySQL login error: 'Access denied for user 'root'@'localhost'

First of all, I don't know why I can't log in to MySQL using the command line or workbench, and it prompts 'Access denied for user 'root'@'localhost'.
Uninstalling and reinstalling the database several times did not work. It seems that the data is not cleaned up properly. The pitfalls encountered during the solution process are recorded and shared here.

Valid operation records:

1. First, skip the permissions to log in to MySQL and view the user table.

Stop mysql service~ $ sudo service mysql stop

Start MySQL~$ sudo mysqld_safe --skip-grant-tables &

Notice:

It may prompt mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exist

Solution: (I tried and found that sudo must be added)

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

Execute the above safe mode again to start mysql ~$ sudo mysqld_safe --skip-grant-tables &

This time the prompt statement shows, mysqld_safe Staring mysqld deamon with database from /var/lib/mysql

This time you can log in without a password: mysql -u root Press Enter to log in

The sql statement to query the mysql.user table is as follows:

Add pictures (to be added later)

Someone else suggested doing it this way, but I didn't try it.

Open & Edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distro.
Add skip-grant-tables under [mysqld]

2. Found that the user's plugin is socket_plugin, changed to mysql_native_password

Modify the plugin permissions of root:

update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password' where user='root';
flush privileges;
quit;

(Note that the modification here must be correct, so as not to be like the author, carelessly modifying the content of the plugin by one letter less, and then having the following troubles)

3. There are other users in the user table. Root cannot log in, but you can log in with other users. After searching, I found the following valid operations:

In the installation directory of mysql, there is usually a debain.cnf file in /etc/mysql, which contains user and password. Use this file to log in and copy the password. Then you can modify the plugin of the user table root again. The operation is the same as above.

Restart the mysql service, sudo service mysql restart;

You can log in as the root user.

Reference blog:

MySQL ERROR 1698 (28000) error https://www.jb51.net/article/117566.htm

mysql How to view the currently used configuration file my.cnf https://www.jb51.net/article/110395.htm

linux -- Ubuntu view and modify mysql login name and password, install phpmyadmin https://www.jb51.net/article/174925.htm

Summarize

The above is the editor's introduction to solving the MySQL login error: ''Access denied for user ''root''@''localhost''. I hope it will be helpful to everyone!

You may also be interested in:
  • Solution to the problem of Access denied for user'root'@'localhost' (using password: YES) in MySQL 8.0 login under win10
  • Install MySQL database 5.6 source code under Linux and change the login user password
  • Node.js+Express+MySql to realize user login and registration functions
  • Use node and express to connect to mysql to implement login and registration code
  • How to modify the password of MySQL 5.1 and remotely log in to the MySQL database

<<:  JavaScript to implement the function of changing avatar

>>:  How to turn a jar package into a docker container

Recommend

Baota Linux panel command list

Table of contents Install Pagoda Management Pagod...

Learn javascript iterator

Table of contents Introduction What does an itera...

MySQL 8.0.18 installation and configuration graphic tutorial

Learning objectives: Learn to use Windows system ...

Do you know the common MySQL design errors?

Thanks to the development of the Internet, we can...

Solve the problem that Docker cannot ping the host machine under Mac

Solution Abandon the Linux virtual machine that c...

Detailed tutorial on deploying Springboot or Nginx using Kubernetes

1 Introduction After "Maven deploys Springbo...

Summary of special processing statements of MySQL SQL statements (must read)

1. Update the entire table. If the value of a col...

Security configuration and detection of SSL after the website enables https

It is standard for websites to enable SSL nowaday...

Vue implements automatic jump to login page when token expires

The project was tested these days, and the tester...

Detailed explanation of the use of shared memory in nginx

In the nginx process model, tasks such as traffic...

Vue realizes the palace grid rotation lottery

Vue implements the palace grid rotation lottery (...

Use Visual Studio Code to connect to the MySql database and query

Visual Studio Code is a powerful text editor prod...

Methods of adaptive web design (good access experience on mobile phones)

1. Add the viewport tag to the HTML header. At th...