Skip to main content

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_safe command 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

![Navigation bar](../../images/banner/nav-hint-banner.png)
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 -p with your new password. This operation requires physical or console access to the server.
AI Translation Notice

This page was translated into English by AI and is provided for reference only. The original document is written in Chinese. If there is any discrepancy, the Chinese version prevails.

本页英文内容由 AI 自动翻译生成,仅供参考;原文为中文,如有差异请以中文原文为准。

查看中文原文 / View the Chinese original →