How to Change the Password for the MySQL root User

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:

  1. Open http://localhost/phpmyadmin/ in your browser to enter the database management interface.
  2. Select the mysql database from the database list on the left.
  3. Find and browse the user table on the right.
  4. Find the record where the user is root, then click Edit or Modify.
  5. When changing the password field, select PASSWORD in the Function column, then enter the new password.
  6. 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.

Leave a Reply