Unable to access MySQL after it automatically generated a temporary password

This is what worked for me on OS X Yosemite running MySql v5.7 (installed from the .dmg).

cd /usr/local/mysql/bin
./mysql -u root -p --connect-expired-password

(Enter the temporary password generated by the installer.)

This gets you into sandbox mode and mysql> prompt. Then set desired root password with SET PASSWORD:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mySuperSecretPassword');

Try this:

mysql -u root -h 127.0.0.1 -p
Enter password: (enter the random password here)

Ref:https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization-mysqld.html

Following this, you may reset your password using ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';