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)
- Stop the running MySQL service.
- Open a DOS window and navigate to the
mysql\bindirectory. - Enter
mysqld --skip-grant-tablesand press Enter. The--skip-grant-tablesoption starts the MySQL service without checking the privilege table. - Open another DOS window (since the previous one is now occupied) and navigate to the
mysql\bindirectory again. - Enter
mysqland press Enter. If successful, the MySQL prompt>will appear. - Connect to the privilege database:
USE mysql;. - Change the password:
UPDATE user SET PASSWORD = PASSWORD("123") WHERE user = "root";(remember to add the semicolon). - Refresh privileges (mandatory step):
FLUSH PRIVILEGES;. - Exit:
QUIT;. - Log out of the system and log back in to use the username
rootand the newly set password123.
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.