Accidents happen. Losing or forgetting a password is not uncommon. In this brief guide, we’ll teach you how to change the MySQL root password on Linux servers.
First of all, make sure you are logged in as root.
We will edit the file “ /etc/my.cnf ” file and add the following directive at the end of it:
skip-grant-tables
Restart the MySQL service:
systemctl restart mysqld
Now we can log in to MySQL as root:
mysql -u root
Clear MySQL privilege settings:
flush privileges;
Now the most important step is to change the root password with the command below:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'SUA NOVA SENHA AQUI';
We can now exit the MySQL CLI:
exit
Go back to the file “ /etc/my.cnf ” and REMOVE the line we added earlier:
# skip-grant-tables
Restart MySQL:
systemctl restart mysqld
That’s it! The MySQL root password has been changed.