İçeriğe geç
← Blog

Changing or Resetting the MySQL Root Password on Linux

26 Eylül 2009, 23:08 Databases 3 dk 9.5k 0

Managing the MySQL root password is one of the first administrative tasks after installing a MySQL server. It is important to remember that the MySQL root user and the Linux/UNIX root user are completely different accounts. Changing one does not affect the other.

In this article, we’ll look at two common scenarios:

  • Changing the MySQL root password when you already know the current password.
  • Resetting the MySQL root password when it has been forgotten.

Changing the MySQL Root Password

Setting the Root Password for the First Time

If your MySQL installation does not yet have a password configured for the root user, you can set one using the mysqladmin utility.

SQL
mysqladmin -u root password NEWPASSWORD

Replace NEWPASSWORD with the password you would like to use.


Changing an Existing Root Password

If a password is already configured, use the following command.

SQL
mysqladmin -u root -p password NEWPASSWORD

After pressing Enter, you will be prompted for the current password.


Changing the Password for Another MySQL User

The same command can be used for any MySQL account.

For example, to change the password for a user named vivek:

SQL
mysqladmin -u vivek -p password NEWPASSWORD

Changing the Password Using SQL

Another option is to update the MySQL system tables directly.

1. Log in to MySQL

SQL
mysql -u root -p

2. Select the MySQL system database

SQL
USE mysql;

3. Update the user’s password

SQL
UPDATE user
SET password = PASSWORD('NEWPASSWORD')
WHERE User = 'vivek';

4. Reload the privilege tables

SQL
FLUSH PRIVILEGES;

5. Exit MySQL

SQL
QUIT;

This approach is also useful when automating administrative tasks through PHP, Perl, or other scripting languages.


Resetting a Forgotten MySQL Root Password

If you have forgotten the MySQL root password, you can temporarily disable authentication and assign a new password.

Step 1. Stop the MySQL Server

SQL
/etc/init.d/mysql stop

Step 2. Start MySQL Without Authentication

Start MySQL with privilege checking disabled.

SQL
mysqld --skip-grant-tables --skip-networking &

This allows you to connect without providing a password.


Step 3. Connect to MySQL

SQL
mysql mysql -u root

Step 4. Set a New Password

SQL
UPDATE user
SET password = PASSWORD('newpassword')
WHERE User = 'root';

Step 5. Reload the Privilege Tables

SQL
FLUSH PRIVILEGES;

Step 6. Restart MySQL

Restart the MySQL server normally.

SQL
/etc/init.d/mysql restart

After restarting, authentication will be enabled again and the new password will take effect.


Conclusion

Changing or resetting the MySQL root password is a straightforward process once you understand the available methods.

If you know the current password, mysqladmin provides the quickest solution. If the password has been lost, MySQL can be started with authentication temporarily disabled, allowing you to assign a new password before restarting the server normally.


📝 Archive Note

This article was originally written in 2009. The examples reflect the MySQL versions commonly used at that time. Modern versions of MySQL (5.7, 8.x and later) have changed the authentication system, and commands such as PASSWORD() and direct updates to the user table are no longer recommended. For historical reference, however, the examples above accurately represent the administration process used in older MySQL releases.