How to Change the Password for the MySQL root User
When creating a configuration file, you usually need to enter a MySQL username and password. The default user is often root, and the password may be empty. To avoid security issues, it is recommended that you set a password for the root user.
Method 1: Change It from the Command Line
If you are using XAMPP, you can first go to MySQL's bin directory, or run mysqladmin directly with its full path.
Example on Windows:
cd xamppmysqlbin
mysqladmin -u root password "新密码"
If the root user already has an old password, add -p, then enter the old password when prompted:
mysqladmin -u root -p password "新密码"
After changing it, you can check whether you can log in with the following command:
mysql -u root -p
The paths may differ between MySQL, MariaDB, and XAMPP versions, so use the actual path for your local installation.
Method 2: Change It Through phpMyAdmin
The following uses a local server as an example:
- Open
http://localhost/phpmyadmin/in your browser to enter the database management interface. - Select the
mysqldatabase from the database list on the left. - Find and browse the
usertable on the right. - Find the record where the user is
root, then click Edit or Modify. - When changing the password field, select
PASSWORDin theFunctioncolumn, then enter the new password. - Save the changes.
After the password has been changed, you will need to enter the new root password when creating a configuration file or connecting to the MySQL database.
If you cannot log in after making the change, check the following:
- Whether the username and password in the configuration file have been updated accordingly.
- Whether phpMyAdmin or the application is still using the old password.
- Whether the MySQL service has been restarted, especially in older local integrated environments.
