If you have recently installed MySQL 8 and want to change the root password, follow these simple steps:
1) Open the MySQL prompt: Run the following command to access the MySQL prompt:
sudo mysql
2) Change the root user’s authentication method: Run the following ALTER USER command to switch the root user’s authentication method to mysql_native_password, which uses a password. Replace ‘password’ with your desired new password, and make sure to keep the single quotes around the password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
It’s important to choose a strong, unique password for your MySQL root user. A strong password should be at least 12 characters long, include a mix of uppercase and lowercase letters, numbers, and special characters.
3) Exit the MySQL prompt: Type exit
and press Enter
to exit the MySQL prompt.
4) Reopen MySQL with the new password: To authenticate as the root MySQL user using the new password, run this command:
mysql -u root -p
5) Revert to the default authentication method (optional): If you want to switch back to using the default auth_socket
authentication method, run this command while logged in to MySQL:
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
We hope this comprehensive guide helps you change the root password after installing MySQL 8.