Changing the MySQL root password

Guide to updating or resetting the MySQL root user password.

Managing MySQL databases is a common task when administering websites and applications on VPS or dedicated servers. It's important to know how to change the root password or recover access if it's lost.

Changing the MySQL Root Password (if you know the current password)

  1. Connect to the server via SSH.
  2. Log in to MySQL as root:
mysql -u root -p

Enter the current password and press Enter.

  1. Run the following command to change the password (replace newpass with your new password):
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
FLUSH PRIVILEGES;
quit

The password has been successfully updated.

Resetting the MySQL Root Password (if the password is lost)

If the root password is unknown, start MySQL in safe mode without authentication checks.

Ubuntu / Debian

  1. Stop the MySQL service:
sudo systemctl stop mysql
  1. Create the PID directory if it doesn't exist:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
  1. Start MySQL in safe mode:
sudo mysqld_safe --skip-grant-tables --skip-networking &
  1. Connect without a password:
mysql -u root
  1. Reset the password (replace new_root_password with your new password):
FLUSH PRIVILEGES;
USE mysql;
UPDATE user SET authentication_string=PASSWORD('new_root_password') WHERE User='root';
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;
quit
  1. Stop the safe mode process and restart MySQL:
sudo pkill mysqld
sudo systemctl start mysql
  1. Test login with the new password:
mysql -u root -p

CentOS 7 / RHEL 7

  1. Stop the MySQL service:
sudo systemctl stop mysql
  1. Start MySQL in safe mode:
sudo mysqld_safe --skip-grant-tables &
  1. Connect without a password:
mysql -u root
  1. Reset the password (replace new_root_password with your new password):
USE mysql;
UPDATE user SET password=PASSWORD('new_root_password') WHERE User='root';
FLUSH PRIVILEGES;
quit
  1. Restart MySQL:
sudo systemctl stop mysqld
sudo systemctl start mysqld
  1. Test login with the new password:
mysql -u root -p

Useful notes

  • After resetting the password, it's recommended to update it in your control panel or application to something more secure.
  • For MySQL 8.0+ — use ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass'; instead of PASSWORD().
  • Always back up your databases before making changes.

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us