Method 1: Use the Set password command
to first log in to MySQL.
Format:mysql> Set password for user name @localhost = password (' new password ');
Example:mysql> set password for root@localhost = password (' 123 ');
Method 2: Mysqladmin
Format: mysqladmin-u user name-p old password password new password
Example: mysqladmin-uroot-p123456 Password 123
Method 3: Edit the user table directly with update
Login 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 the root password, you can do this
take windows as an example:
1. Close the running MySQL service.
2. Open the DOS window and go to the Mysqlbin directory.
3. Enter mysqld--skip-grant-tables carriage return. --skip-grant-tables means to skip permission table authentication when starting the MySQL service.
4. Open a DOS window again (because the DOS window is already unable to move), go to the Mysqlbin directory.
5. Enter MySQL return, if successful, the MySQL prompt > will appear.
6. Connection rights database: use MySQL;.
6. Change Password: Update user set Password=password ("123") where user= "root" (don't forget the last semicolon).
7. Refresh permissions (required steps): Flush privileges;.
8. Exit quit.
9. Log off the system, then enter, use username root and just set the new password 123 to login.