Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Reset a Lost MySQL Root Password

This guide shows how to get back into MySQL if you forgot the root password. It needs root access to your server.

Databases2 min read10 steps

What is the MySQL root user?

MySQL is a database program. Its "root" user is the main admin, who can do anything inside it. This is not the same as the root user of the server, but the names are alike.

This guide is for a VPS or dedicated server where you have root access. On shared hosting you cannot do this. Use cPanel to change database user passwords instead, or ask Hostvento support.

Take a backup of your data folder before you begin, if you can. These steps stop MySQL for a short time, so your sites will be briefly down.

Steps

  1. Log in to your server over SSH as root. SSH is a way to type commands on your server from afar.
  2. Stop the MySQL service:
systemctl stop mysqld

On some systems the service is named mysql or mariadb.

  1. Start MySQL in a special mode that skips password checks:
mysqld_safe --skip-grant-tables --skip-networking &

The --skip-networking part blocks outside connections, so only you on the server can enter while it is open.

  1. Connect without a password:
mysql -u root
  1. Load the permission tables back:
FLUSH PRIVILEGES;
  1. Set a new password. Use a strong one:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewStrongPassword';

On very old MySQL 5.5 or 5.6, use this instead:

UPDATE mysql.user SET Password=PASSWORD('YourNewStrongPassword') WHERE User='root';
  1. Apply and leave:
FLUSH PRIVILEGES;
EXIT;
  1. Stop the special-mode server:
mysqladmin shutdown

If that asks for a password, find the process with ps aux | grep mysql and stop it with kill using its process number.

  1. Start MySQL normally:
systemctl start mysqld
  1. Test the new password:
mysql -u root -p

If you use WHM

On a cPanel/WHM server, root's MySQL password is also saved in /root/.my.cnf. Update the password in that file so cPanel can still connect.

Tip: Store the new password in a password manager. Do not leave the server in --skip-grant-tables mode, because anyone on the server could then use the database.

Quick recap

  • You need root access on a VPS or dedicated server.
  • Stop MySQL, then start it with --skip-grant-tables.
  • Run FLUSH PRIVILEGES; and set a new password.
  • Restart MySQL normally and test.
  • Update /root/.my.cnf if you use WHM.