Six ways to force mysql's root password to change (mysql forgot password)

  • 2020-05-12 06:18:41
  • OfStack

Method 1
Using phpmyadmin, which is the easiest, modify the user table of the mysql library,
But don't forget to use the PASSWORD function.
Method 2
Use mysqladmin, which is a special case declared earlier.
mysqladmin -u root -p password mypasswd
After entering this command, you need to enter the original password for root, and then the password for root will be changed to mypasswd.
Change the root command to your username and you can change your own password.
Of course, if your mysqladmin can't connect to mysql server, or you can't execute mysqladmin,
Then this method is invalid.
And mysqladmin can't clear out the password.
The following methods are all used at the mysql prompt and must have the root permission of mysql:
Methods 3
mysql > INSERT INTO mysql.user (Host,User,Password)
VALUES('%','jeffrey',PASSWORD('biscuit'));
mysql > FLUSH PRIVILEGES
To be exact, this is adding a user with the username jeffrey and password biscuit.
This example is in the mysql Chinese reference manual, so I wrote it out.
Note that you use the PASSWORD function, and then you also use FLUSH PRIVILEGES.
Methods 4
And method 31, just using the REPLACE statement
mysql > REPLACE INTO mysql.user (Host,User,Password)
VALUES('%','jeffrey',PASSWORD('biscuit'));
mysql > FLUSH PRIVILEGES
Methods 5
Using the SET PASSWORD statement,
mysql > SET PASSWORD FOR jeffrey@"%" = PASSWORD('biscuit');
Quasi must also use the PASSWORD() function,
But you don't need to use FLUSH PRIVILEGES.
Methods 6
Using GRANT... IDENTIFIED BY statement
mysql > GRANT USAGE ON *.* TO jeffrey@"%" IDENTIFIED BY 'biscuit';
Here the PASSWORD() function is unnecessary and FLUSH PRIVILEGES is not required.
Note: PASSWORD() [not] enforces password encryption in the same way as Unix password encryption.
MySQL's solution for forgetting passwords
If MySQL is running, kill it first: killall-TERM mysqld.
Launch MySQL: bin/safe_mysqld -- skip-grant-tables &
You can access MySQL without a password.
And then there is
> use mysql
> update user set password=password("new_pass") where user="root";
> flush privileges;
Re-kill MySQL and start MySQL in the normal way.

Related articles: