To change the MySQL “root” login password, follow these steps:
- Access your server via SSH as the “root” user
-
Once you are on your server, the first step is to stop the MySQL service. To do this, simply run the following command:
# /etc/init.d/mysql stop -
Wait until the service has stopped completely, and then you can run the following command to start MySQL without authentication:
# mysqld_safe --skip-grant-tables & -
Once the service has started, you can access the MySQL server as root without using a password with the command:
# mysql -uroot mysql -
Once you have logged in, enter the following command to change the password:
UPDATE user SET password=PASSWORD("nuevacontraseña") WHERE user="root"; FLUSH PRIVILEGES;The PASSWORD() function was removed in MySQL 8.0, and in MySQL 5.7+ the method uses ALTER USER:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'nuevacontraseña';
-
Now you can try to log in with the new password using the command:
# mysql -uroot -pnuevacontraseña mysql -
If the last step worked, you will need to restart MySQL without the “–skip-grant-tables” option. You can do this with the commands:
# /etc/init.d/mysql stop && /etc/init.d/mysql start
Notice
Remember that changing the MySQL root password can affect the proper functioning of, among other applications, the Plesk control panel if it is installed on the server, since it uses the MySQL root password.