How to Reset MySQL Root Password
In this tutorial, you'll learn about How to Reset MySQL Root Password. We cover key concepts, practical examples, and best practices.
The Problem
You forgot the MySQL root password and cannot log in. Running mysql -u root -p fails with Access denied for user 'root'@'localhost'. Without root access, you cannot manage databases, users, or configuration.
Quick Fix
Step 1: Stop the MySQL service
Stop MySQL before starting it in safe mode:
sudo systemctl stop mysql
Step 2: Start MySQL skipping privilege tables
Launch MySQL without loading the grant tables:
sudo mysqld_safe --skip-grant-tables &
Or on systems using systemd:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo mysqld --skip-grant-tables --skip-networking &
Step 3: Log in without a password
Connect as root without authentication:
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>
Step 4: Reset the root password
Run SQL commands to update the password:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword123!';
Or on older MySQL versions:
UPDATE mysql.user SET authentication_string=PASSWORD('NewStrongPassword123!') WHERE User='root';
FLUSH PRIVILEGES;
Step 5: Stop safe mode and restart normally
Shut down the MySQL instance running in safe mode:
mysqladmin -u root shutdown
Start MySQL normally:
sudo systemctl start mysql
Step 6: Test the new password
Verify the new root password works:
mysql -u root -p
Enter the new password when prompted. You should now have full root access.
Alternative Solutions
Use mysql_secure_installation after reset
Run the security script after password recovery:
sudo mysql_secure_installation
Use a temporary init file for password reset
Create an SQL file and run it at MySQL startup:
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass';" > /tmp/reset.sql
sudo mysqld --init-file=/tmp/reset.sql &
Common Mistakes to Avoid
Starting MySQL with --skip-grant-tables and leaving it exposed. Always add --skip-networking to prevent remote connections while the password is unset.
Forgetting FLUSH PRIVILEGES after manual UPDATE. Direct updates to mysql.user table require FLUSH PRIVILEGES to take effect. ALTER USER handles this automatically.
Setting a weak password during reset. Use a mix of uppercase, lowercase, numbers, and symbols. Store the new password in a password manager.
Pro Tips
Use mysql_secure_installation after every installation. Run the security script to set a root password, remove anonymous users, and disable remote root login.
Create a superuser with a different name for daily use. Instead of using root, create a named admin user with full privileges for auditing: CREATE USER 'admin'@'localhost' IDENTIFIED BY 'pass'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';.
Store passwords in a vault. Use a command-line vault like pass or a GUI tool like KeePassXC to manage database credentials securely.
Prevention
- Store root passwords in a password manager immediately after creation.
- Create a separate admin user with reduced privileges for daily work.
- Set up passwordless
sudoaccess to MySQL for emergency recovery without credentials.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro