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
- Log in to your server over SSH as root. SSH is a way to type commands on your server from afar.
- Stop the MySQL service:
systemctl stop mysqld
On some systems the service is named mysql or mariadb.
- 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.
- Connect without a password:
mysql -u root
- Load the permission tables back:
FLUSH PRIVILEGES;
- 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';
- Apply and leave:
FLUSH PRIVILEGES;
EXIT;
- 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.
- Start MySQL normally:
systemctl start mysqld
- 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.
--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.cnfif you use WHM.