To install MySQL and set the root password on Ubuntu, follow these steps:
Installing MySQL
Open a terminal and run the following command to install MySQL:
sudo apt update
sudo apt install mysql-server
Resetting or setting the root password
If you didn’t set a root password during the secure installation process or want to change it later, use the MySQL command-line client. Open a terminal and enter the following commands:
sudo mysql -u root
This will open the MySQL shell. Then, execute the following SQL command to change the root password (replace new_password with your desired password):
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
FLUSH PRIVILEGES;
After setting the password, exit the MySQL shell:
exit;
Optionally, Restart the MySQL service to apply the changes:
sudo service mysql restart
Your MySQL root password should now be set or updated. Remember to replace new_password with your desired password. Make sure to choose a strong and secure password to protect your MySQL installation.
Keep in mind that MySQL 8 and later versions use a different authentication plugin, so using mysql_native_password as shown in the command might be necessary for compatibility reasons. If you’re using a more recent MySQL version, consult the documentation for the appropriate password setting approach.