MySQL is a popular database system used by many websites and web applications. If you have forgotten the MySQL root password and cannot access your databases, use this guide to reset it. Steps are given for a server without a control panel, a cPanel server and a Plesk server.
Before you begin:
– You need root (administrator) access to the server through SSH.
– Back up your databases if possible.
– In the commands below, replace NEWPASSWORD with a strong password of your own.
– The MySQL service name can be mysql, mysqld or mariadb, depending on your server. Use the name that matches your server.
– While MySQL runs with skip-grant-tables, anyone can log in without a password. Finish the steps quickly and do not leave this mode on.
————————————————-
PART 1: Server without a control panel
————————————————-
Method A: Older servers using SysVinit (for example, CentOS 6)
- Stop the MySQL service:
/etc/init.d/mysqld stop
- Start MySQL without a password:
mysqld_safe --skip-grant-tables --skip-networking &
- Log in to MySQL:
mysql -u root
- Switch to the MySQL database:
USE mysql;
- Update the root password:
UPDATE user SET password=PASSWORD('NEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit; - Stop MySQL:
/etc/init.d/mysqld stop If this command does not work, stop the process with: killall mysqld
- Restart MySQL:
/etc/init.d/mysqld start
- Access MySQL with the new password:
mysql -u root -p
Method B: Servers using systemd (for example, CentOS 7 and newer, AlmaLinux, Rocky Linux, Ubuntu, Debian)
- Stop the MySQL service:
systemctl stop mysql
- Set environment for skipping grant tables:
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
- Start MySQL:
systemctl start mysql
- Log in to MySQL:
mysql -u root
- Update the root password:
USE mysql; UPDATE user SET password=PASSWORD('NEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit; OR For newer versions (MySQL 5.7 and 8.0, MariaDB 10.2 and newer): FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEWPASSWORD'; exit; - Unset the environment and restart MySQL:
systemctl unset-environment MYSQLD_OPTS systemctl restart mysql
- Access MySQL with the new password:
mysql -u root -p
————————————————-
PART 2: cPanel server
————————————————-
Reset MySQL Root Password on a cPanel Server
- Locate the Current Password
- Log in as the root user and view the .my.cnf file:
cat /root/.my.cnf
- The file contains the MySQL root password.
- Log in as the root user and view the .my.cnf file:
- Change the Password via WHM
- Log in to WHM.
- Navigate to SQL Services > MySQL Root Password.
- Enter the new password and confirm the change.
Note: If the above methods don’t work, reset the password using the steps for a Plain server. - If these steps do not work, reset the password using the steps in Part 1. After that, update the password saved in /root/.my.cnf so that cPanel can still connect to MySQL.
————————————————-
PART 3: Plesk server
————————————————-
Reset MySQL Root Password on a Plesk Server using Automated Method
- Download the restoration script:
curl -LO https://plesk.zendesk.com/hc/article_attachments/360042955514/213364309-restore-admin-user.php.tar.gz
- Extract and run the script:
tar xf 213364309-restore-admin-user.php.tar.gz plesk php 213364309-restore-admin-user.php
- Attempt to log in to Plesk.
Method B: Manual method
Using Manual Method
- Check for the old-passwords directive:
grep -ir old-passwords /etc/my*
If found, remove the directive.
- Add skip-grant-tables to my.cnf:
vi /etc/my.cnf Add the following lines: [mysqld] skip-grant-tables
- Restart MySQL:
service mysql restart
- Log in to MySQL:
plesk db
- Restore the admin user:
DROP USER 'admin'@'localhost'; CREATE USER 'admin'@'localhost' IDENTIFIED BY 'NEWPASSWORD'; GRANT ALL ON *.* TO 'admin'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; Important: Plesk normally uses the password stored in the file /etc/psa/.psa.shadow for this user. Use that password in place of NEWPASSWORD, or Plesk may not be able to connect to its database.
- Remove skip-grant-tables from my.cnf and restart MySQL:
service mysql restart
By following the methods outlined above, you can successfully reset the MySQL root password on your server. If you encounter any issues, feel free to contact our support team for assistance.
After resetting MySQL, ensure your server has enough space. Learn How to Check Server Disk Space Usage


