The fastest way to reset MySQL root password depends on your operating system

If you have lost or forgotten the MySQL root password, you can reset it without knowing the current one. The method differs between Linux, Windows, and macOS, but all involve stopping the MySQL service, restarting it in a special mode that skips authentication, and then setting a new password. You do not need to reinstall MySQL or lose any data.

The process takes 5 to 15 minutes and requires command-line access to the server where MySQL is running. You will need administrator or sudo privileges on that machine.

Key Takeaways

  • Stop the MySQL service first, then restart it with the --skip-grant-tables flag to bypass password authentication.
  • On Linux, use sudo systemctl stop mysql or sudo service mysql stop depending on your distribution.
  • Connect to MySQL without a password when it is running in skip-grant-tables mode, then use FLUSH PRIVILEGES before setting a new password.
  • After you set the new password, restart MySQL normally and verify the new password works before leaving the server.
  • If MySQL will not start in skip-grant-tables mode, check that the data directory exists and that the MySQL user owns it.

Reset MySQL root password on Linux

Stop the MySQL service using systemctl or the service command, depending on your Linux distribution. Most modern distributions use systemctl:

sudo systemctl stop mysql

If that command does not work, try the older service syntax:

sudo service mysql stop

Verify the service has stopped by checking the status. Then start MySQL with the skip-grant-tables flag, which tells it to skip the authentication system entirely:

sudo mysqld_safe --skip-grant-tables &

The ampersand at the end runs the command in the background. Wait a few seconds for MySQL to start, then connect to it without entering a password:

mysql -u root

You should see the mysql> prompt. Now tell MySQL to reload the grant tables (the authentication system) so that your password change will take effect:

FLUSH PRIVILEGES;

Then set a new root password. Replace newpassword with your actual new password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Type exit to leave the MySQL prompt. Now stop the mysqld_safe process and restart MySQL normally. Find the process ID and kill it:

ps aux | grep mysqld

Look for the mysqld_safe line and note the process ID, then kill it:

sudo kill [process-id]

Restart MySQL with the normal startup command:

sudo systemctl start mysql

Test the new password by connecting as root:

mysql -u root -p

When prompted, enter your new password. If you see the mysql> prompt, the reset worked.

Reset MySQL root password on Windows

Open Command Prompt as Administrator. Stop the MySQL service:

net stop MySQL80

Replace MySQL80 with the actual service name if yours is different. You can see the service name in Services (services.msc) or by running sc query | find "MySQL".

Create a text file in a temporary location (for example, C:\temp\mysql-init.txt) with this single line:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Replace newpassword with your actual new password. Save the file and close it. Now start MySQL with the init-file flag pointing to this file:

mysqld --init-file=C:\temp\mysql-init.txt

MySQL will read and execute the command in that file during startup, setting the new password. Wait for the command to complete (it may take 10 to 20 seconds), then stop it by pressing Ctrl+C.

Delete the init file so the password is not stored in plain text:

del C:\temp\mysql-init.txt

Restart the MySQL service normally:

net start MySQL80

Test the new password using MySQL Workbench or the command line:

mysql -u root -p

Reset MySQL root password on macOS

If you installed MySQL using Homebrew, stop the service:

brew services stop mysql

If you installed it manually or from the DMG installer, stop it using:

sudo /usr/local/mysql/support-files/mysql.server stop

Start MySQL in skip-grant-tables mode:

sudo mysqld_safe --skip-grant-tables &

Connect without a password:

mysql -u root

Reload the grant tables and set the new password:

FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Exit the MySQL prompt and stop the mysqld_safe process. Find it first:

ps aux | grep mysqld

Kill the process using its ID:

sudo kill [process-id]

Restart MySQL normally. If you use Homebrew:

brew services start mysql

If you installed it manually:

sudo /usr/local/mysql/support-files/mysql.server start

What to do if MySQL will not start in skip-grant-tables mode

The most common reason MySQL fails to start is that the data directory does not exist or the MySQL user does not own it. Check that the directory exists and has the correct permissions. On Linux, the data directory is usually /var/lib/mysql:

ls -la /var/lib/mysql

If the directory does not exist, create it and set the correct owner:

sudo mkdir -p /var/lib/mysql

sudo chown mysql:mysql /var/lib/mysql

sudo chmod 750 /var/lib/mysql

If MySQL still will not start, check the error log. On Linux, it is usually at /var/log/mysql/error.log:

sudo tail -50 /var/log/mysql/error.log

The last 50 lines will show what went wrong. Common issues include a corrupted ibdata1 file (the main data file) or insufficient disk space. If the data directory is corrupted and you have no backups, you may need to reinstall MySQL and restore from a backup.

Verify the new password and find the root account

After you reset the password and restart MySQL normally, test it when ready by connecting as root:

mysql -u root -p

Enter the new password when prompted. If you see the mysql> prompt, the reset succeeded. Run a straightforward query to confirm MySQL is working:

SELECT 1;

You should see a result of 1. Type exit to leave.

Now find the root account by removing unnecessary test databases and anonymous users. Connect as root again and run:

DROP DATABASE IF EXISTS test;

DELETE FROM mysql.user WHERE User='';

FLUSH PRIVILEGES;

These commands remove the default test database and any anonymous accounts that MySQL creates during installation. Both are security risks if left in place.

Frequently Asked Questions

Can I reset the root password without stopping MySQL?

No. You must stop MySQL and restart it in skip-grant-tables mode to bypass the authentication system. If you know the current root password, you can change it while MySQL is running using the ALTER USER command, but that is a password change, not a reset.

Will resetting the root password delete my databases?

No. The password reset process does not touch your data. All databases, tables, and records remain intact. You are only changing the password used to access them.

What if I do not know the data directory location?

You can find it by connecting to MySQL (if you still have access) and running SELECT @@datadir;. On Linux, it is usually /var/lib/mysql. On Windows, it is usually C:\ProgramData\MySQL\MySQL Server 8.0\Data. On macOS with Homebrew, it is usually /usr/local/var/mysql.

Do I need to change the password for other MySQL users?

No. Resetting the root password does not affect other user accounts. Only the root password changes. Other users keep their existing passwords.

What if mysqld_safe is not found on my system?

On some systems, mysqld_safe may not be in your PATH. Try the full path: sudo /usr/sbin/mysqld_safe --skip-grant-tables & on Linux or sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables & on macOS. If it still does not work, use mysqld directly instead: sudo mysqld --skip-grant-tables &.