MySQL 4 ways to change an root password of takes windows as an example

  • 2020-05-27 07:20:22
  • OfStack

Method 1: use the SET PASSWORD command
Log on to MySQL first.
Format: mysql > set password for username @localhost = password(' new password ');
Example: mysql > set password for root@localhost = password('123');

Method 2: use mysqladmin
Format: mysqladmin-u username -p old password password new password
Example: mysqladmin-uroot-p 123456 password 123

Method 3: edit the user table directly with UPDATE
Log on to MySQL first.
mysql > use mysql;
mysql > update user set password=password('123') where user='root' and host='localhost';
mysql > flush privileges;

Method 4: when you forget your root password, do this
Take windows for example:
1. Shut down the running MySQL service.
2. Open the DOS window and go to the mysql\bin directory.
3. Enter mysqld -- skip-grant-tables enter. -- skip-grant-tables means to skip permission table authentication when starting the MySQL service.
4. Open another DOS window (because the DOS window cannot be moved anymore) and go to mysql\bin.
5. Enter mysql enter, and if successful, an MySQL prompt will appear > .
6. Connect to permission database: use mysql; .
6. Change password: update user set password=password("123") where user="root"; Don't forget the semicolon at the end.
7. Refresh permissions (required step) : flush privileges; .
8. Exit quit.
Log out of the system and enter again. Log in using the user name root and the new password 123 you just set.

Related articles: