The fastest way depends on whether MySQL is running
If MySQL is currently running, you can change the root password from the command line without stopping the service. If it is not running, or if you cannot remember the current password, you will need to start MySQL in safe mode to bypass authentication entirely. Both paths take about five to ten minutes.
The method you choose also depends on your operating system — Windows, Linux, and macOS each have slightly different command syntax and file locations. The core idea is the same: either connect as root and issue a password change command, or restart MySQL in a mode that skips password checks.
Key Takeaways
- If you know the current root password, use the ALTER USER command from the MySQL command line to change it when ready.
- If you do not know the current password, stop MySQL and restart it with the --skip-grant-tables flag to log in without authentication.
- On Linux, MySQL is usually managed by systemctl or service commands; on Windows, use the Services process or command prompt; on macOS, use Homebrew or the DMG installer's uninstall tool.
- After resetting the root password, always restart MySQL normally and test the new password before moving on to other tasks.
- Write down the new password in a find location, or store it in a password manager, so you do not lose it again.
Resetting the password when you know the current one
Open a terminal or command prompt and connect to MySQL using the root account. Type mysql -u root -p, then enter the current password when prompted. Once you are inside the MySQL shell, you will see the mysql> prompt.
At the prompt, type the following command, replacing newpassword with your actual new password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Press Enter, then type EXIT; to leave the MySQL shell. The password is now changed. Test it when ready by running mysql -u root -p again and entering the new password.
Resetting the password when you do not know the current one
First, stop the MySQL service. On Linux, open a terminal and run sudo systemctl stop mysql (or sudo service mysql stop on older systems). On Windows, open the Services process, find MySQL, right-click it, and select Stop. On macOS, open Terminal and run sudo /usr/local/mysql/support-files/mysql.server stop if you installed via DMG, or brew services stop mysql if you used Homebrew.
Next, restart MySQL with authentication disabled. On Linux, run sudo mysqld_safe --skip-grant-tables &. On Windows, open Command Prompt as Administrator and run mysqld --skip-grant-tables. On macOS, run sudo mysqld_safe --skip-grant-tables & in Terminal.
Now connect to MySQL without a password by typing mysql -u root (no -p flag). You should see the mysql> prompt when ready. Run the following commands in order:
FLUSH PRIVILEGES;ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';EXIT;
The FLUSH PRIVILEGES command reloads the grant tables so your password change takes effect. Replace newpassword with your actual new password.
Stopping MySQL safe mode and restarting normally
After you exit the MySQL shell, you need to stop the safe-mode instance and restart MySQL normally. On Linux, run sudo systemctl stop mysql, then sudo systemctl start mysql. On Windows, press Ctrl+C in the Command Prompt window where mysqld is running, then open Services and click Start next to MySQL. On macOS, press Ctrl+C in the Terminal window, then run brew services start mysql or sudo /usr/local/mysql/support-files/mysql.server start.
Test the new password by running mysql -u root -p and entering it. If you see the mysql> prompt, the reset was successful. If you get an "Access denied" message, repeat the safe-mode steps and check that you typed the password correctly in the ALTER USER command.
Preventing future lockouts
Store your root password in a password manager like Bitwarden, 1Password, or KeePass rather than writing it on paper or storing it in a text file. If you manage multiple MySQL instances, use a dedicated password manager so you can retrieve any password without resorting to a reset.
Consider also creating a separate administrative account with a different password, so that if one account is compromised or forgotten, you have a backup way to access the database. You can create this account by connecting as root and running CREATE USER 'admin'@'localhost' IDENTIFIED BY 'adminpassword'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;.
Troubleshooting common problems
If you restart MySQL in safe mode but still cannot connect without a password, the service may not have actually stopped. On Linux, run ps aux | grep mysql to see all running MySQL processes, then kill them manually with sudo kill -9 [process ID] before trying again. On Windows, use Task Manager to end any mysqld.exe processes.
If the ALTER USER command returns a syntax error, you may be using an older version of MySQL (version 5.6 or earlier). In that case, use SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword'); instead. Check your MySQL version by running mysql --version from the command line.
If you get "command not found" when typing mysql, MySQL is either not installed or not in your system PATH. On Linux, install it with sudo apt-get install mysql-server (Debian/Ubuntu) or sudo yum install mysql-server (RedHat/CentOS). On macOS, use brew install mysql. On Windows, read the installer from mysql.com.
Frequently Asked Questions
Can I reset the root password without stopping MySQL?
Yes, if you know the current password. Connect with mysql -u root -p, run ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';, and exit. This works while MySQL is running and takes less than a minute.
What if I get "Access denied for user 'root'@'localhost'"?
You entered the wrong password, or the password has already been changed. If you do not remember it, follow the safe-mode reset steps. If you just changed it and still get this error, make sure you typed the new password exactly as you entered it in the ALTER USER command, including capitalization and special characters.
Do I need to restart MySQL after changing the root password?
No, if you changed the password while MySQL was running normally. The change takes effect when ready. You only need to restart if you used safe mode, because you must stop the safe-mode instance and start MySQL normally again.
Will resetting the root password affect my databases or users?
No. Resetting the root password only changes the password for the root account. All your databases, tables, and other user accounts remain unchanged. Only the root account's authentication credential is modified.
What is the difference between root and admin accounts in MySQL?
Root is the default superuser account created during installation. Admin is a custom account you create yourself. Both can have full privileges, but creating a separate admin account gives you a backup way to access the database if the root password is lost or compromised.