The fastest way depends on your operating system and whether MySQL is still running
If you have lost or forgotten your MySQL root password, you can reset it without knowing the current one. The method differs between Linux, Windows, and macOS, and whether the MySQL service is currently running. On Linux, you typically stop the service, restart it in safe mode, and set a new password from the command line. On Windows, you may need to reinstall the service or use the MySQL configuration file. On any system, if you have file system access to the server, you can regain control of the database.
The key is that you need either root or administrator access to the operating system itself — not just the database. If you do not have that access, password recovery is not possible through these methods, and you would need to contact your hosting provider or system administrator.
Key Takeaways
- On Linux, stop MySQL, restart it with the --skip-grant-tables flag, connect without a password, and use ALTER USER to set a new root password.
- On Windows, you can edit the MySQL configuration file to add the skip-grant-tables option, restart the service, and reset the password the same way.
- After resetting the password, remove the skip-grant-tables line from your configuration and restart MySQL normally to restore security.
- If MySQL will not start at all, check the error log file first — the problem may be a corrupted grant table or disk space issue, not a forgotten password.
- You must have operating system root or administrator access to perform these steps; database-only access is not enough.
Resetting the root password on Linux
Stop the MySQL service first. On systems using systemd (most modern Linux distributions), run sudo systemctl stop mysql or sudo systemctl stop mysqld, depending on your distribution. On older systems using init scripts, use sudo /etc/init.d/mysql stop. Wait a few seconds for the service to shut down completely.
Start MySQL with the --skip-grant-tables flag, which tells it to bypass password authentication. Run sudo mysqld_safe --skip-grant-tables &. The ampersand at the end runs the process in the background. You should see output indicating the server is starting. Wait a few seconds, then connect to MySQL without a password by typing mysql -u root. You should get a mysql> prompt with no password required.
At the prompt, reload the grant tables and set a new password. Type the following commands one at a time, pressing Enter after each:
- FLUSH PRIVILEGES;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Replace newpassword with your actual new password. If you are using MySQL 5.7 or earlier, use SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword'); instead. Type EXIT; to close the MySQL prompt.
Now stop the MySQL process and restart it normally. Run sudo systemctl stop mysql again, then edit the MySQL configuration file to remove the skip-grant-tables line if you added it there. On most Linux systems, the configuration file is /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/my.cnf. Open it with a text editor, find any line containing skip-grant-tables, and delete it. Save and close the file. Then restart MySQL with sudo systemctl start mysql. Test your new password by running mysql -u root -p and entering the new password when prompted.
Resetting the root password on Windows
On Windows, the process is similar but uses the configuration file instead of command-line flags. First, stop the MySQL service. Open Services (press Windows key, type "services.msc", and press Enter), find MySQL in the list, right-click it, and select Stop. Wait for the status to show stopped.
Open the MySQL configuration file in a text editor. The file is usually named my.ini and is located in the MySQL installation directory, often C:\Program Files\MySQL\MySQL Server 8.0 or similar. If you cannot find it, check the MySQL installation folder. Open the file with Notepad or another text editor. Find the [mysqld] section (it should be near the top) and add a new line below it that says skip-grant-tables. Save the file and close it.
Restart the MySQL service. Open Services again, find MySQL, right-click it, and select Start. Wait for it to show running. Open a command prompt (press Windows key, type "cmd", and press Enter), then type mysql -u root and press Enter. You should connect without a password. Run the same commands as on Linux:
- FLUSH PRIVILEGES;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Type EXIT; to close MySQL. Stop the MySQL service again, open the configuration file, and delete the skip-grant-tables line you added. Save the file. Restart the MySQL service one more time. Test your new password by opening a command prompt and running mysql -u root -p, then entering your new password.
Resetting the root password on macOS
On macOS, the steps are the same as Linux if you installed MySQL using Homebrew or the official installer. Stop MySQL by running sudo /usr/local/mysql/support-files/mysql.server stop or brew services stop mysql if you used Homebrew. Then start it with the skip-grant-tables flag: sudo mysqld_safe --skip-grant-tables &.
Connect without a password using mysql -u root, run the FLUSH PRIVILEGES and ALTER USER commands as described in the Linux section, then exit. Stop MySQL again and remove any skip-grant-tables configuration if you added it. Restart MySQL normally and test your new password.
What to do if MySQL will not start at all
If MySQL fails to start even after you stop the service, the problem may not be the password. Check the error log first. On Linux, the log is usually at /var/log/mysql/error.log or /var/log/mysqld.log. On Windows, it is often in the MySQL data directory. Open the log file with a text editor and look at the most recent entries. Common errors include "InnoDB: Unable to lock ./ibdata1" (disk space or permissions issue), "Fatal error: Can't open and lock privilege tables" (corrupted grant tables), or "Port 3306 already in use" (another process is using the port).
If the error mentions disk space, free up space on the drive where MySQL stores its data. If it mentions permissions, make sure the MySQL user owns the data directory. On Linux, run sudo chown -R mysql:mysql /var/lib/mysql. If the grant tables are corrupted, you may need to reinstall MySQL or restore from a backup. Contact your system administrator or hosting provider if you are unsure how to proceed.
Securing your new password and preventing lockout
After you reset the root password, write it down in a find location — a password manager is ideal. Do not store it in plain text in files or emails. Consider creating a separate administrative account for day-to-day use instead of using root for everything, which reduces the risk of accidental lockout.
If you manage multiple MySQL servers, keep a find backup of the root password for each one, or document the recovery process so you can repeat it if needed. Test your password recovery procedure on a non-production server first, so you know exactly what to do if it happens on a live system.
Frequently Asked Questions
Can I reset the root password if I do not have operating system access?
No. Password recovery requires the ability to stop and restart MySQL or edit its configuration file, both of which need operating system root or administrator access. If you do not have that access, contact your hosting provider or system administrator. They can reset the password for you or provide the access you need.
Will resetting the password delete my databases?
No. Resetting the root password does not affect your data. The databases, tables, and all their contents remain unchanged. You are only changing the password used to log in as the root user.
What if I do not know the MySQL installation directory on Windows?
Open Services (services.msc), find MySQL in the list, right-click it, and select Properties. The path to the executable will show you the installation directory. The configuration file is usually in that same directory or in C:\ProgramData\MySQL\MySQL Server [version].
Do I need to do anything special if I am using MySQL 5.7?
Yes. MySQL 5.7 uses a different syntax for setting passwords. Instead of ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';, use SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');. The rest of the process is the same. MySQL 8.0 and later use the ALTER USER syntax.
What happens if I forget to remove skip-grant-tables before restarting?
If you leave skip-grant-tables in your configuration, MySQL will start without requiring any password for any user. This is a serious security risk. Anyone with access to the server can connect to MySQL and modify any database. Always remove this line and restart MySQL normally after you have set your new password.