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

The whole process of developing a Google plug-in with vue+element

Simple function: Click the plug-in icon in the up...

Detailed explanation of the installation process of Jenkins on CentOS 7

Install Jenkins via Yum 1. Installation # yum sou...

Detailed explanation of MySQL binlog usage

binlog is a binary log file that records all DML ...

How to clear default styles and set common styles in CSS

CSS Clear Default Styles The usual clear default ...

CSS Back to Top Code Example

Most websites nowadays have long pages, some are ...

Detailed explanation of the use of Linux time command

1. Command Introduction time is used to count the...

Data storage implementation method in WeChat applet

Table of contents Global variable globalData Page...

Question about custom attributes of html tags

In previous development, we used the default attr...

Implementation method of Nginx+tomcat load balancing cluster

The experimental environment is as follows Here y...

Solve the problem of blocking positioning DDL in MySQL 5.7

In the previous article "MySQL table structu...