1 Stop MySQL:
$ sudo service mysql stop
or
$ sudo /usr/local/mysql/support-files/mysql.server stop
or
$ /etc/init.d/mysql stop
2 Start it in safe mode:
to do first
$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld
then
$ sudo mysqld_safe --skip-grant-tables
This will be an ongoing command until the process is finished, after the secret is changed, ctrl+z to stop it.
3 Open another shell/terminal window, log in without a password:
$ mysql -u root
mysql>use mysql;
MySQL5.6 and under:
mysql> UPDATE user SET Password=PASSWORD('password'),plugin='mysql_native_password' WHERE User='root';
MySQL 5.7 and over:
mysql> update user set authentication_string=password('password'),plugin='mysql_native_password' where user='root';
now to stop the mysql_safe process
4 Start MySQL:
sudo service mysql start
Your new password is 'password'.