This guide explains how to reset the root password for your MySQL server if you have lost or forgotten it.
Warning: This procedure involves restarting your database server and temporarily bypassing its normal security checks. Perform these steps with caution, preferably during a maintenance window with no active database connections.
Stop the running MySQL service and restart it with the --skip-grant-tables option. This disables permission checks and allows you to log in without a password.
The & at the end runs the process in the background so you can continue using the terminal.
Once the server is running in safe mode, connect as the root user — no password is required at this point.
Run the following SQL commands to update the root password. Replace MyNewPass with your own strong password.
The commands below apply to MySQL 5.7.6+ and MariaDB 10.1.20+. Older versions used UPDATE mysql.user SET Password=..., which is deprecated and should no longer be used.
Stop the manually started safe-mode process and bring MySQL back up normally so that all security checks are re-enabled.
Your MySQL root password has now been changed and the server is running normally with all security checks active.
For advanced MySQL configuration and administration, refer to the official documentation.