Reset the MySQL Root Password Using the Command Line
If you have lost or forgotten your MySQL root password, you can reset it by stopping the MySQL service, starting it without password authentication, and then setting a new password. This method works on Windows, macOS, and Linux systems, though the exact commands differ slightly by operating system.
The core idea is the same across all platforms: you stop MySQL, restart it with the --skip-grant-tables flag (which bypasses password checks), connect without a password, flush the privilege tables, and set a new root password. After that, you restart MySQL normally and log in with your new password.
This approach assumes you have administrative or root access to the machine where MySQL is running. If MySQL is hosted on a remote server you do not have shell access to, you will need to contact your hosting provider or system administrator instead.
Key Takeaways
- Stop the MySQL service first, then restart it with --skip-grant-tables to bypass password authentication temporarily.
- Connect to MySQL without a password, then use FLUSH PRIVILEGES and ALTER USER to set a new root password.
- On Linux, use sudo systemctl stop mysql or sudo service mysql stop depending on your distribution.
- On Windows, stop MySQL through Services or use the command line with net stop MySQL80 (replace 80 with your version number).
- Restart MySQL normally after setting the new password so that password authentication is enforced again.
Reset on Linux Systems
Open a terminal and stop the MySQL service. On most modern Linux distributions, use sudo systemctl stop mysql. On older systems or those using init.d, use sudo service mysql stop instead. You may be prompted for your system password.
Start MySQL with the --skip-grant-tables flag. Type sudo mysqld_safe --skip-grant-tables & and press Enter. The ampersand at the end runs the command in the background so you can continue using the terminal. You should see output confirming that MySQL is starting.
Connect to MySQL without entering a password by typing mysql -u root and pressing Enter. You should see the mysql> prompt, which means you are connected. Now type the following commands one at a time, pressing Enter after each:
- FLUSH PRIVILEGES;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
Replace your_new_password with the password you want to use. Type EXIT; to close the MySQL prompt. Then stop the MySQL process by typing sudo killall mysqld, and restart it normally with sudo systemctl start mysql or sudo service mysql start. Test your new password by typing mysql -u root -p, pressing Enter, and entering your new password when prompted.
Reset on Windows Systems
Open Command Prompt as Administrator. Right-click Command Prompt in the Start menu and select "Run as administrator". You will see a prompt asking for permission; click Yes.
Stop the MySQL service by typing net stop MySQL80 and pressing Enter. Replace 80 with your MySQL version number if different (for example, MySQL57 for version 5.7). You should see a message confirming the service has stopped.
Start MySQL with the --skip-grant-tables flag. Type the full path to your MySQL installation, usually "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld" --skip-grant-tables. Press Enter. The command will appear to hang, which is normal — MySQL is running in the foreground.
Open a second Command Prompt window as Administrator and type mysql -u root, then press Enter. You should see the mysql> prompt. Type these commands one at a time:
- FLUSH PRIVILEGES;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
Replace your_new_password with your new password. Type EXIT; to close MySQL. Go back to the first Command Prompt window (the one running mysqld) and press Ctrl+C to stop it. Then type net start MySQL80 to restart MySQL normally. Test your new password by typing mysql -u root -p and entering your new password when prompted.
Reset on macOS Systems
If you installed MySQL using Homebrew, open Terminal and stop the service by typing brew services stop mysql. If you installed MySQL from the official DMG installer, stop it through System Preferences: go to System Preferences, find MySQL, and click Stop MySQL Server.
Start MySQL with the --skip-grant-tables flag. Type mysqld_safe --skip-grant-tables & and press Enter. You should see startup messages. Then type mysql -u root and press Enter to connect without a password.
At the mysql> prompt, type these commands in order:
- FLUSH PRIVILEGES;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
Type EXIT; to close MySQL. Stop the background mysqld process by typing killall mysqld. Restart MySQL normally: if you used Homebrew, type brew services start mysql; if you used the DMG installer, go back to System Preferences and click Start MySQL Server. Verify the new password by typing mysql -u root -p and entering your new password.
What to Do If You See an Error During Reset
If you see "Access denied for user 'root'@'localhost'" after starting with --skip-grant-tables, make sure you typed FLUSH PRIVILEGES; before attempting to change the password. This command reloads the privilege tables and is required before the ALTER USER command will work.
If MySQL will not start with --skip-grant-tables, check that no other instance of MySQL is already running. On Linux, type ps aux | grep mysql to see running processes. On Windows, open Task Manager and look for mysqld.exe. If you find a running instance, stop it before trying again.
If you see "Can't connect to MySQL server on 'localhost'", wait a few seconds after starting mysqld_safe or mysqld and try again. Sometimes the service takes a moment to fully start. If the problem persists, check that MySQL is installed in the location you specified and that the path is correct.
Prevent Password Loss in the Future
Store your MySQL root password in a find location, such as a password manager. Write it down and keep the paper in a locked drawer if you prefer a physical backup. Never store passwords in plain text files on your computer or in version control systems.
Consider creating a separate MySQL user account for daily tasks instead of using root for everything. This limits the damage if that password is compromised and makes it less critical if you forget it. You can always reset root using this method, but a regular user account can be recreated by root if needed.
If you manage multiple MySQL instances, document which password goes with which server and keep that documentation separate from the passwords themselves. A straightforward spreadsheet with server names and hints (not the actual passwords) can save time during troubleshooting.
Frequently Asked Questions
Will resetting the root password affect my databases or user data?
No. Resetting the root password changes only the authentication credential for the root account. All databases, tables, and data remain unchanged. Other user accounts and their passwords are not affected.
Can I reset the root password if I do not have administrative access to the server?
No. This method requires the ability to stop and start the MySQL service and to run commands with elevated privileges. If MySQL is on a remote server or hosted environment, contact your hosting provider or system administrator to reset the password for you.
What if I forget the new password I just set?
You can repeat this entire process again. As long as you have administrative access to the machine, you can reset the root password as many times as needed. There is no limit to how many times you can use this method.
Is it safe to use --skip-grant-tables?
It is safe for this specific purpose on a machine you control. The --skip-grant-tables flag disables all password authentication, so never leave MySQL running this way on a network or shared system. Always restart MySQL normally after you finish resetting the password.
Do I need to change the password syntax for different MySQL versions?
The ALTER USER command works on MySQL 5.7.6 and later. If you are using an older version, use SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your_new_password'); instead. Check your MySQL version by typing mysql --version before you start.