Detailed tutorial on how to modify the root password after forgetting it in MySQL 5.7

Detailed tutorial on how to modify the root password after forgetting it in MySQL 5.7

Preface

For a long time, the application and learning environment of MySQL has been MySQL 5.6 and earlier versions, and no attention has been paid to the changes and new features of the new version MySQL 5.7. Today, when I was helping someone deal with a forgotten root password, I found that the previous method didn't work.

The specific situation is as follows:

The case environment is as follows:

Operating system: Red Hat Enterprise Linux Server release 6.6 (Santiago)

Database version: 5.7.18 MySQL Community Server (GPL)

Forgot your password? When you enter an incorrect password, you will get the following error message:

[root@mytestlnx02 ~]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mytestlnx02 ~]#

Check whether the MySQL service is started. If it is started, shut down the MySQL service.

[root@mytestlnx02 ~]# ps -ef | grep -i mysql
root 22972 1 0 14:18 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 23166 22972 0 14:18 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql
[root@mytestlnx02 ~]# service mysqld stop
Stopping mysqld: [ OK ]
[root@mytestlnx02 ~]#

Find the MySQL my.cnf configuration file and add the following information to /etc/my.cnf (some versions are /etc/mysql/my.cnf):

[mysqld] 

skip-grant-tables

Then I started MySQL and entered MySQL, and changed the root password. During the operation, I encountered ERROR 1054 (42S22): Unknown column 'password' in 'field list' . I checked the table structure of the user table and found that the user table no longer had a Password field under MySQL 5.7. The encrypted user password is stored in the authentication_string field.

The specific operation process is as follows:

[root@mytestlnx02 ~]# service mysqld start
Starting mysqld: [ OK ]
[root@mytestlnx02 ~]# mysql -u root 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
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 user set password=PASSWORD('Kd8k&dfdl023')
 -> where user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> update mysql.user set authentication_string=password('Kd8k&dfdl023') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit 

In the my.cnf file, comment or delete the line "skip-grant-tables" that you just added. Then after restarting the MySQL service, you need to execute the command set password=password('newpassword'); and the problem will be solved.

[root@mytestlnx02 ~]# service mysqld start
Starting mysqld: [ OK ]
[root@mytestlnx02 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18
 
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password=password('Kd8k&dfdl023');
Query OK, 0 rows affected, 1 warning (0.00 sec)

After checking some relevant information, I found that MySQL 5.7 has some new features in terms of security.

1. The plugin field of the user table mysql.user is not allowed to be empty. The default value is mysql_native_password instead of mysql_old_password. The old password format is no longer supported.

2. Add a password expiration mechanism. After the expiration, you need to change the password, otherwise it may be disabled or enter sandbox mode. Whether to enable password expiration is controlled by the parameter default_password_lifetime.

mysql> show variables like 'default_password_lifetime';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| default_password_lifetime | 0 |
+---------------------------+-------+
1 row in set (0.00 sec)
 
mysql>

3: Added password security level and password complexity settings. The parameters are as follows:

mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)

4. When using mysql_install_db to initialize, a random password is automatically generated by default and placed in /var/log/mysqld.log. No other accounts except root@localhost and mysql.sys@localhost are created, and no test library is created.

[root@mytestlnx02 mysql]# yum localinstall mysql-community-{server,client,common,libs}-* 
[root@mytestlnx02 mysql]# rpm -qa | grep -i mysql
mysql-community-client-5.7.18-1.el6.i686
mysql-community-libs-5.7.18-1.el6.i686
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-community-server-5.7.18-1.el6.i686
mysql-community-common-5.7.18-1.el6.i686
mysql-community-libs-compat-5.7.18-1.el6.i686
[root@mytestlnx02 mysql]# service mysqld start
 
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
[root@mytestlnx02 mysql]# 
[root@mytestlnx02 mysql]# grep 'temporary password' /var/log/mysqld.log
2017-05-05T06:10:57.802143Z 1 [Note] A temporary password is generated for root@localhost: w99s(m-q_ML:
 
mysql> select user ,host from user;
+-----------+-----------+
| user | host |
+-----------+-----------+
| mysql.sys | localhost |
| root | localhost |
+-----------+-----------+
2 rows in set (0.00 sec)

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Solution for forgetting the root password of MySQL5.7 under Windows 8.1
  • How to change the root password of Mysql5.7.10 on MAC
  • What to do if you forget the root password of Mysql5.7 (simple and effective method)
  • Solution to forgetting the root password of MySQL5.7 on Mac
  • MySQL 5.7 installation process and method to reset the root password (shell script)
  • Detailed method for forgetting the root password or resetting the password in Mysql 5.7
  • Problem of retrieving root password in MYSQL 5.7 under Linux (tested and available)

<<:  Detailed graphic tutorial on silent installation of oracle12.2 on CentOS under VMware

>>:  Complete steps for using Echarts and sub-packaging in WeChat Mini Program

Recommend

Tomcat8 uses cronolog to split Catalina.Out logs

background If the catalina.out log file generated...

Implementation example of JS native double-column shuttle selection box

Table of contents When to use Structural branches...

Vue implements simple slider verification

This article example shares the implementation of...

Introduction to install method in Vue

Table of contents 1. Globally registered componen...

MySQL database optimization: index implementation principle and usage analysis

This article uses examples to illustrate the prin...

How to count down the date using bash

Need to know how many days there are before an im...

ElementUI implements the el-form form reset function button

Table of contents Business scenario: Effect demon...

Modify the default scroll bar style in the front-end project (summary)

I have written many projects that require changin...

Example of adding multi-language function to Vue background management

Table of contents 1. First, configure the main.js...

What is the base tag and what does it do?

The <base> tag specifies the default addres...

vue_drf implements SMS verification code

Table of contents 1. Demand 1. Demand 2. SDK para...

Detailed explanation of Nginx rewrite jump application scenarios

Application scenario 1: Domain name-based redirec...

Two ways to write stored procedures in Mysql with and without return values

Process 1: with return value: drop procedure if e...

JavaScript Advanced Custom Exception

Table of contents 1. Concept 1.1 What are errors ...