4 Methods to Change MySQL Root Password

Publish: 2018-01-05 | Modify: 2018-08-14

Method 1: Using 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: Using UPDATE to edit the user table directly
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: In case of forgetting the root password, you can do the following
Using Windows as an example:

  1. Stop the running MySQL service.
  2. Open a DOS window and go to the mysql\bin directory.
  3. Enter mysqld -skip-grant-tables and press Enter. -skip-grant-tables means skipping permission table authentication when starting the MySQL service.
  4. Open another DOS window (since the previous DOS window can no longer be used) and go to the mysql\bin directory.
  5. Enter mysql and press Enter. If successful, the MySQL prompt will appear.
  6. Connect to the permission database: use mysql.
  7. Change the password: update user set password=password("123") where user="root"; (don't forget the semicolon at the end).
  8. Refresh permissions (a necessary step): flush privileges.
  9. Quit: quit.
  10. Log out of the system, then log in again using the username root and the newly set password 123.

Original article from: MySQL修改root密码的4种方法
Author: QQ:337003006


Comments