mysql-forgot-password
How to Reset the MySQL Root Password (Linux Systems)
If you have forgotten the MySQL root password, please follow these steps:
Step 1: Stop the MySQL Service
sudo systemctl stop mysql
(Note: The service name might be mysqld or mariadb on some systems.)
Step 2: Start MySQL in Safe Mode with Authentication Bypassed
sudo mysqld_safe --skip-grant-tables --skip-networking &
- This mode allows login without a password but disables network connections for security. If the
mysqld_safecommand is not found, try using its full path:sudo /usr/sbin/mysqld_safe --skip-grant-tables &.
Step 3: Log in to MySQL without a Password
mysql -u root
Step 4: Flush Privileges and Reset the Password
FLUSH PRIVILEGES; -- This is a critical step!
-- Choose the reset command based on your MySQL/MariaDB version:
-- For MySQL 5.7.6+ or MariaDB 10.1+:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
-- For MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your_new_password');
Step 5: Exit and Restart MySQL Normally
EXIT;
# Stop the MySQL process that was started in safe mode

sudo kill `sudo cat /var/run/mysqld/mysqld.pid`
# If the PID file path differs, find the process ID manually: sudo ps aux | grep mysqld
# Start the MySQL service normally
sudo systemctl start mysql
Verify the New Password
mysql -u root -p
# Enter your new password when prompted
Troubleshooting Common Issues
mysqld_safe Command Not Found
Try using the full path: sudo /usr/sbin/mysqld_safe --skip-grant-tables &.
PID File Path is Different
Check the actual PID file location or process ID: sudo ps aux | grep mysqld.
Specific Steps for MySQL 8.0+
If you encounter password policy issues after resetting, you might need to specify the authentication plugin:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
Different Service Names
On some systems, the service might be named mariadb or mysqld:
sudo systemctl stop mariadb # Example for stopping the service
sudo systemctl start mysqld # Example for starting the service
- After completing these steps, test the login using
mysql -u root -pwith your new password. This operation requires physical or console access to the server.