4 Methods to Change the MySQL Root Password

mysql root passwordchange mysql passwordmysqladminskip-grant-tablesreset forgotten password
Published·Modified·

Method 1: Using the SET PASSWORD Command

First, log in to MySQL.

Format:

mysql> SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');

Example:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');

Method 2: Using mysqladmin

Format:

mysqladmin -u username -p old_password password new_password

Example:

mysqladmin -uroot -p123456 password 123

Method 3: Directly Editing the User Table with UPDATE

First, log in to MySQL.

mysql> USE mysql;
mysql> UPDATE user SET PASSWORD = PASSWORD('123') WHERE user = 'root' AND host = 'localhost';
mysql> FLUSH PRIVILEGES;

Method 4: Resetting Password When Forgotten (Windows Example)

  1. Stop the running MySQL service.
  2. Open a DOS window and navigate to the mysql\bin directory.
  3. Enter mysqld --skip-grant-tables and press Enter. The --skip-grant-tables option starts the MySQL service without checking the privilege table.
  4. Open another DOS window (since the previous one is now occupied) and navigate to the mysql\bin directory again.
  5. Enter mysql and press Enter. If successful, the MySQL prompt > will appear.
  6. Connect to the privilege database: USE mysql;.
  7. Change the password: UPDATE user SET PASSWORD = PASSWORD("123") WHERE user = "root"; (remember to add the semicolon).
  8. Refresh privileges (mandatory step): FLUSH PRIVILEGES;.
  9. Exit: QUIT;.
  10. Log out of the system and log back in to use the username root and the newly set password 123.

Original source: 4 Methods to Change the MySQL Root Password. All rights reserved by the original author. If there is any infringement, please contact QQ: 337003006 for deletion.