Unable to access MySQL after it automatically generated a temporary password

最后都变了- 提交于 2019-11-27 17:56:37

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

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).

Doing these steps under OSX 10.11 El Capitan and MySQL 5.7.X, should do the trick.

Considering that you already have MySQL installed then..

Open a terminal window and type:

  1. sudo /usr/local/mysql/support-files/mysql.server stop
  2. sudo mysqld_safe --skip-grant-tables

Since the command fired in the step 2 will be under on going state, you need to open another terminal window and then type:

  1. mysql -u root -p
  2. UPDATE mysql.user SET password_expired='N', authentication_string=PASSWORD('') WHERE User='root';
  3. quit;
  4. sudo /usr/local/mysql/support-files/mysql.server restart

Important: in the step 2 you must replace for your password.

Hope it will wok for you.

MySQL password expired

Resetting the password will solve the problem temporarily, however, from MySQL 5.7.4 to 5.7.10 (I think to encourage better security) the default value for the default_password_lifetime variable is 360 (about a year). For those versions, if you make no changes to this variable (or to individual user accounts) all user passwords expire after 360 days.

Typically, from a script you might get the message: "Your password has expired. To log in you must change it using a client that supports expired passwords."

So, to prevent automatic password expiry, log in as root (mysql -u root -p), then, for clients that automatically connect to the server (e.g. scripts.) change the password expiration settings for those clients:

ALTER USER 'script'@'localhost' PASSWORD EXPIRE NEVER;

or you can disable automatic password expiration for all users:

SET GLOBAL default_password_lifetime = 0;

Links:

MySQL: Password Expiration and Sandbox Mode
MySQL: Password Expiration Policy
Password expiration policy in MySQL Server 5.7

I'm running macOS Sierra(10.12.3) and I installed mysql-5.7.17-macos10.12-x86_64.dmg.

The answer from @lesley worked for me with the exception that I needed to add ./ to ensure I was calling the mysql binary in my current working directory. Which is where the aforementioned package was installed.

If you cd to /usr/local/mysql/bin and run mysql -u root -p --connect-expired-password, you could receive the following error.

mysql: unknown option '--connect-expired-password'

I did. Because simply running mysql without providing a path, called a previously installed version of the MariaDB client.

So to ensure you are executing the correct binary, you can either

provide the absolute path

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

or the relative path after changing directories

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

Both ways should work. Once you are connected to the client, the instruction are the same as above from @lesley.

Enter your temporary password generated by the installer and set your new password.

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

I faced the same problem. I followed the installation process guide from https://www.ntu.edu.sg/home/ehchua/programming/sql/MySQL_HowTo.html and downloaded DMG archive and installed MySQL on my MAC OS X 10.12.2.

Finally executed the following commands on new Terminal.

cd /usr/local/mysql/bin

./mysql -u root -p --connect-expired-password

It worked.

james meek

Answer 7 worked for me: El capitan, MySQL installed from dmg and autogenerated password, but made sure to cd to /usr/local/bin/mysql before entering ./mysql -root -p Obvious, but I didn't the first time.

Now to find where all my databases and tables are and how to link them in.

For Mysql 5.7 I use

shell $ > sudo grep 'temporary password' /var/log/mysqld.log

This particular one did the trick for me:

As specified in this link: https://www.variphy.com/kb/mac-os-x-reset-mysql-root-password

Do all the steps except executing

    UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root';

Execute

    UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';

And then execute FLUSH PRIVILEGES;

The another way to solve this issue is to use an older version of MySQL instead.

I have uninstalled MySQL version 5.7.9 for Mac OS X 10.9 (x86, 64-bit), DMG Archive and then install the older version, MySQL version 5.6.7 for Mac OS X 10.9 (x86, 64-bit), DMG Archive. This issue is solved. The given autogenerated password before finishing installation of this older version is gone and I can ultimately access the database using root as username and a blank password. Everything is working like a charm!

I installed view brew, and I had the same error message until I noticed this caveat:

We've installed your MySQL database without a root password. To secure it run: mysql_secure_installation

To connect run: mysql -uroot

To have launchd start mysql now and restart at login: brew services start mysql

Or, if you don't want/need a background service you can just run: mysql.server start

I got around this problem by running 'mysql -u root -p --connect-expired-password' Then input the expired auto-gen password from mysql. Finally got in. Selected mysql db with 'use mysql' and then updated user 'root' pw with 'ALTER USER 'root'@'localhost' IDENTIFIED BY 'your new password'

Installing MySQL manually by downloading packages for the first time generates a default password for root. Copy and save that. If not done somehow on successive re-installations it does not show that password.

Thus you cannot login to root. Do the following :

  1. Find mysql related entries from system sudo find / -name mysql
  2. Remove all mysql related entries by doing rm -rf <mysql_entries_above>
  3. Download latest mysql-server and intall it.
  4. You will be promted with a default password which you need to copy.
  5. Run mysql_secure_installation and paste that password when asked for root.
  6. Subsequently follow the steps and change admin password when prompted for.

This may happens when you have installed mysql before. Try the password you set for the last version of mysql. This did work for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!