All solutions I found were much more complex than necessary and none worked for me. Here is the solution that solved my problem. No need to restart mysqld or start it with special privileges.
sudo mysql
— for MySQL
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’;
— for MariaDB
ALTER USER ‘root’@’localhost’ IDENTIFIED VIA mysql_native_password USING PASSWORD(‘root’);
With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root (feel free to change it in the query)
Now you should be able to login with root. More information can be found in MySQL documentation or MariaDB documentation
(exit mysql console with Ctrl + D or by typing exit)
Open & Edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distro.
Add skip-grant-tables under [mysqld]
Restart Mysql
You should be able to login to mysql now using the below command mysql -u root -p
Run mysql> flush privileges;
Set new password by ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘NewPassword’;
Go back to /etc/my.cnf and remove/comment skip-grant-tables
Restart Mysql
Now you will be able to login with the new password mysql -u root -p