Log into the linux server and then elevate to root for the rest of the commands:
sudo -i
OK, now on to the real work:
Step 1: Check the version of MySQL on Ubuntu 22.04, make sure it's 8 or higher.
mysql --version
Step 2: Stop the MySQL server.
systemctl stop mysql.service
Step 3: Skip Grant Tables & Networking.
systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"
Step 4: Start the MySQL service.
systemctl start mysql.service
Step 5: Sign In to the MySQL shell.
mysql -u root
Step 6: Alter the root password.
flush privileges;
USE mysql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';
quit;
Step 7: Revert Database Server to Normal settings.
systemctl unset-environment MYSQLD_OPTS
systemctl revert mysql
Step 8: Kill all MySQL processes & Restart the MySQL service.
killall -u mysql
systemctl restart mysql.service
Step 9: Log in with the newly set password.
mysql -u root -p