Mysql password expired. Can't connect

I just wiped my Mac and did a fresh install of El Capitan. I'm struggling to connect to Mysql now. Having gone through a web server setup process, I've created a simple PHP test file:

<?php
  $conn = new mysqli("127.0.0.1", "root", "xxxxxxxx");
  if ($conn->connect_error) echo "Connection failed: " . $conn->connect_error; 
  else echo "Connected successfully";
  phpinfo();
?>

When I run it, I get this error:

Warning: mysqli::mysqli(): (HY000/1862): Your password has expired. To log in you must change it using a client that supports expired passwords. in /Users/rich/Documents/DESIGN/test/index.php on line 3
Connection failed: Your password has expired. To log in you must change it using a client that supports expired passwords.

I've never seen that response from a connection before. How do I fix it if I can't connect?

EDIT

In terminal I entered the command:

mysql -u root -p

This asked me for my password (current one) which I put in. I now have access to mysql commands but anything I try results in this error:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

How do I reset the password using ALTER USER?


So I finally found the solution myself.

Firstly I went into terminal and typed:

mysql -u root -p

This asked for my current password which I typed in and it gave me access to provide more mysql commands. Anything I tried from here gave this error:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

This is confusing because I couldn't actually see a way of resetting the password using ALTER USER statement, but I did find another simple solution:

SET PASSWORD = PASSWORD('xxxxxxxx');


First, I use:

 mysql -u root -p

Giving my current password for the 'root'. Next:

mysql> ALTER USER `root`@`localhost` IDENTIFIED BY 'new_password',
       `root`@`localhost` PASSWORD EXPIRE NEVER;

Change 'new_password' to a new password for the user 'root'.
It solved my problem.


mysqladmin -u [username] -p password worked for me on OS X El Capitan and MySQL 5.7.12 Community Server. Example:

$ /usr/local/mysql/bin/mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

This is similar to pavan sachi's answer, but with password prompts.

My error was "#1862 - Your password has expired. To log in you must change it using a client that supports expired passwords." at phpMyAdmin login screen first time.


MySQL password expiry

Resetting the password will only solve the problem temporarily. From MySQL 5.7.4 to 5.7.10 (to encourage better security - see MySQL: Password Expiration Policy) the default default_password_lifetime variable value is 360 (1 year-ish). For those versions, if you make no changes to this variable (or to individual user accounts) all passwords expire after 360 days.

So 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."

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

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

OR you can disable automatic password expiration for all users:

SET GLOBAL default_password_lifetime = 0;

As pointed out by Mertaydin in the comments, to make this permanent add the following line to a my.cnf file MySQL reads on startup, under the [mysqld] group of settings. The location of my.cnf depends on your setup (e.g. Windows, or Homebrew on OS X, or an installer), and whether you want this per-user on Unix or global:

[mysqld] default_password_lifetime = 0 (There may be other settings here too...)

See the MySQL docs on configuration files.


I went through the same issue recently while installing mysql on mac os x capitan. I did not find the correct answer here, so adding this answer.

MySql in current versions, generates a temporary password when you install mysql. Use this password to set a new password using the mysqladmin utility as below;

/usr/local/mysql/bin/mysqladmin -u root -p'<your temp password>' password '<your new password>'

Hope it helps you and others.