MySQL forgot the root password solution, applicable to the latest version

When using MySQL, if you forget the password of other users, you can use the root user to reset it, but if you forget the root password, you must use a special method to operate.
You can modify the root password by directly modifying the authorization table. The steps are described in detail below, taking mysql5.7 as an example.

  1. Stop the mysqld service process
    [root@localhost ~]# systemctl stop mysqld.service 
    [root@localhost ~]# netstat -ntpln | grep 3306  //检测mysql是否已经启动
  2. Use mysqld combined with skip-grant-tables to start the database. Its function is that the user does not use the authorization table when logging in, so the user can log in directly without using a password.
    [root@localhost ~]# mysqld --skip-grant-tables&
    [root@localhost ~]# netstat -ntpul | grep 3306  //检测mysql是否已经启动
    tcp6       0      0 :::3306                 :::*                    LISTEN      1561/mysqld 
  3. You can log in to mysql directly without using a password, and use update to modify the root password.
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 Source distribution

mysql> update mysql.user set authentication_string=password('123abc') where user='root'; //修改root密码为123abc
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

4. After refreshing the database and using update to change the password, it is only modified in the database, but the data in the memory is not modified. The function of flush privileges is to extract the user information and permission settings in the current user and privilege tables from the mysql library into the memory. After the mysql user data and permissions are modified, if you want to take effect directly without restarting the mysql service, you need to execute the following commands.

mysql> flush privileges;      //刷新数据库
Query OK, 0 rows affected (0.01 sec)

5. Use the new password to do a login test. A successful login indicates that the modification is successful.

[root@localhost ~]# mysql -uroot -p123abc   //登录mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 Source distribution

Note here that when logging in, -p is directly followed by the login password, and there can be no spaces, otherwise there will be errors.

Guess you like

Origin blog.csdn.net/hailangnet/article/details/90483725