Change MySQL Root Password

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.

Step 1: Restart MySQL in Safe Mode

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.

Stop MySQL Service
systemctl stop mysql
Start MySQL in Safe Mode
mysqld_safe --skip-grant-tables &

The & at the end runs the process in the background so you can continue using the terminal.

Step 2: Connect to MySQL

Once the server is running in safe mode, connect as the root user — no password is required at this point.

Connect as Root
mysql -u root

Step 3: Set the New Password

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.

Change Password & Apply
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; FLUSH PRIVILEGES; exit;

Step 4: Restart MySQL Normally

Stop the manually started safe-mode process and bring MySQL back up normally so that all security checks are re-enabled.

Stop Safe Mode Process
killall mysqld
Start MySQL Service
systemctl start mysql

Your MySQL root password has now been changed and the server is running normally with all security checks active.

Further Documentation

For advanced MySQL configuration and administration, refer to the official documentation.