Linux MySQL root password forgotten solution

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not logged into MySQL for a long time due to some reasons, or if the work handover is not completed well, you may forget the database root login password. How can you solve this problem?

1. Change the my.cnf configuration file

1. Edit the /etc/my.cnf configuration file using the command: vim /etc/my.cnf or vi /etc/my.cnf or nano /etc/my.cnf

2. Add skip-grant-tables under [mysqld], then save and exit

3. Restart the mysql service: service mysqld restart

2. Change the root username

1. After restarting, execute the mysql command to enter the mysql command line

2. Change the root user password

MySQL> UPDATE mysql.user SET Password=PASSWORD('new password') where USER='root';
MySQL> flush privileges;
MySQL> exit

Note: The above is for versions before 5.7. Versions after 5.7 do not have the Password field, and the password field is changed to authentication_string

mysql> update mysql.user set authentication_string=password('root123456') where user='root'; #Password changed successfully
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges; #Effective immediately
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

n>mysql -u ******* -p #Login successfully with this user.
Enter password: ********
…………………………
mysql>

Note: For versions later than 5.7, the password cannot be too simple, such as 123456. Otherwise, the following error will be reported: ERROR 1819 (HY000): Your password does not satisfy the current policy requirements.

At this point, either set the password to be more complex or modify the configuration:

This is actually related to the value of validate_password_policy.

validate_password_policy has the following values:

The default value is 1, which means MEDIUM, so the password you set at the beginning must meet the length and must contain numbers, lowercase or uppercase letters, and special characters.
Sometimes, just for my own testing, I don’t want to set a complicated password. For example, I just want to set the root password to 123456.
Two global parameters must be modified:

First, modify the value of the validate_password_policy parameter mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
1
2
The validate_password_length parameter defaults to 8, which we change to 1

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
1
2
4. After completion, execute the password change statement again to succeed mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

3. Finally, comment out skip-grant-tables in /etc/my.cnf, and then restart mysql, that is: service mysqld restart

OK, now we can use the new root password to log in to MySQL.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL multi-instance deployment and installation guide under Linux
  • MySQL 8.0.25 installation and configuration tutorial under Linux
  • How to start multiple MySQL databases on a Linux host
  • Steps and pitfalls of upgrading linux mysql5.5 to mysql5.7
  • Solve the problem of no my.cnf file in /etc when installing mysql on Linux
  • Steps to install MySQL using Docker under Linux
  • Detailed explanation of how to manually deploy a remote MySQL database in Linux
  • Detailed explanation of the idea of ​​using mysqldump+expect+crontab to implement mysql periodic cold backup in linux
  • Aliyun Linux compile and install php7.3 tengine2.3.2 mysql8.0 redis5 process detailed explanation
  • How to implement scheduled backup of MySQL in Linux
  • How to reset the root password in Linux mysql-5.6
  • Use MySQL to open/modify port 3306 and open access permissions in Ubuntu/Linux environment
  • MySQL scheduled backup solution (using Linux crontab)
  • Detailed tutorial on installing MySQL database in Linux environment
  • How to automatically backup mysql remotely under Linux
  • Detailed tutorial on installing mysql-8.0.20 under Linux
  • How to use MyCat to implement MySQL master-slave read-write separation in Linux

<<:  Teach you how to implement Vue3 Reactivity

>>:  Example analysis of the use of GROUP_CONCAT in MySQL

Recommend

A solution to a bug in IE6 with jquery-multiselect

When using jquery-multiselect (a control that tra...

Make your website run fast

Does performance really matter? Performance is im...

Detailed explanation of special phenomena examples of sleep function in MySQL

Preface The sleep system function in MySQL has fe...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

Implementation of Nginx domain name forwarding https access

A word in advance: Suddenly I received a task to ...

Two common solutions to html text overflow display ellipsis characters

Method 1: Use CSS overflow omission to solve The ...

How to prevent website content from being included in search engines

Usually the goal of building a website is to have...

Minio lightweight object storage service installation and browser usage tutorial

Table of contents Introduction Install 1. Create ...

MySQL automatically inserts millions of simulated data operation code

I use Navicat as my database tool. Others are sim...

Summary of 28 common JavaScript string methods and usage tips

Table of contents Preface 1. Get the length of a ...