MySQL can log in without a password (in Linux)

After installing mysql, I found that you can enter MySQL without entering a password. After studying for a long time, I finally found a solution.

Step 1: input in the console (modify mysqld.cnf file)

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

Enter the mysqld.cnf configuration file, and then add the sentence skip grant tables in the [mysqld] section of the configuration file, as shown in the following figure

 [mysqld]

 # * Basic Settings

 user = mysql
 pid-file = /var/run/mysqld/mysqld.pid
 socket = /var/run/mysqld/mysqld.sock
 port = 3306
 basedir = /usr
 datadir = /var/lib/mysql
 tmpdir = /tmp
 lc-messages-dir = /usr/share/mysql
 skip-external-locking
 character-set-server=utf8
 collation-server=utf8_general_ci
 skip-grant-tables    # Add this sentence
Function: you can skip authentication and log in to MySQL without password

Use the following instructions to restart the MySQL service

service mysql restart

Step 2: enter the following commands on the console

mysql
# perhaps
mysql -u root
# perhaps
mysql -u root -p

Just press enter when prompted to enter the password. After entering MySQL, execute the following three sentences respectively

use mysql; 

update user set authentication_string=password("Your password") where user="root"; 

flush privileges; 

The effect is shown in the following figure:

Then exit MySQL.

Step 3: re-enter the mysqld.cnf file and comment out the skip grant tables statement added at the beginning

 [mysqld]

 # * Basic Settings

 user = mysql
 pid-file = /var/run/mysqld/mysqld.pid
 socket = /var/run/mysqld/mysqld.sock
 port = 3306
 basedir = /usr
 datadir = /var/lib/mysql
 tmpdir = /tmp
 lc-messages-dir = /usr/share/mysql
 skip-external-locking
 character-set-server=utf8
 collation-server=utf8_general_ci
# skip-grant-tables    # Comment out this sentence

Enter mysql -u root -p on the console again. At this time, you have to enter the password before you can log in to MySQL.

If you still don't need to enter the password at this time, please see Step 4

Step 4. If you still don't need to enter the password at this time, you need to return to step 3, release the commented statement again (that is, delete # symbols), re-enter MySQL and execute the following commands

use mysql; 

select user,plugin from user; 

The effect is shown in the following figure:

As can be seen from the figure, the reason for the error is that the plugin field of root is auth_socket, let's change it to MySQL_ native_ Just password. Execute the following command:

update user set authentication_string=password("Your password") ,plugin='mysql_native_password' where user='root';

Execute again

select user, plugin from user;

We can see that the field of root user has been changed successfully, as shown in the figure below.

Finally, exit MySQL. Go back to step 3

Then the problem will be completely solved.

Supplement: auth_ Usage scenario of socket verification plug-in:

auth_socket, a plug-in verification method, has the following characteristics:

  • First of all, this authentication method does not require the input of a password. Even if the password is entered, it will not be verified. This feature makes many people feel very unsafe. Actually, a careful study of this method shows that it is quite safe because it has two other restrictions;
  • You can only log in with socket of UNIX, which ensures that you can only log in locally. Users have passed the security verification of the operating system when using this login method;
  • The user name of the operating system must be consistent with that of the MySQL database. For example, if you want to log in as the root user of MySQL, you must log in as the root user of the operating system.

auth_ Because of these characteristics, socket plug-in is very suitable for our installation and debugging before the system is put into operation, and it also has considerable security, because the root user of the operating system and the root user of MySQL are often used at the same time before the system is put into operation. When the system is put into operation, the root user of the operating system and the root user of MySQL cannot be used casually.

Add the following code in [mysqld], or change the MySQL authentication method

default_authentication_plugin=mysql_native_password

Keywords: Linux Database MySQL

Added by jh_dempsey on Thu, 09 Sep 2021 02:29:09 +0300