Unable to access MySQL after it automatically generated a temporary password

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';


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');

Now that the password MySQL had generated is expired, the problem is reduced to getting this password to work again (1) or generate a new one (2). This can be accomplished by running MySQL with the skip-grant-tables option which would make it ignore the access rights:

  1. Stop your MySQL server.

  2. Add the below at the end of the [mysqld] section of my.cnf file and save it.

    skip-grant-tables

  3. Start MySQL server.

  4. In terminal, type

    mysql -u root -p

to get into MySQL command prompt.

  1. In the command prompt, type

    USE mysql;

to get into the mysql database where it keeps database users.

  1. Type

    UPDATE user SET password_expired = 'N' WHERE User = 'root';

to let MySQL know the password is not expired (1) or

UPDATE user SET authentication_string = PASSWORD('YourNewPassword'), password_expired = 'N' WHERE User = 'root';

to assign a new password YourNewPassword to root (2).