MySQL用户密码过期登陆报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

时间:2022-01-22 23:49:42

今天接到主从复制失败告警,查看MySQL,发现MySQL能够登陆但是执行命令报错,

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

MySQL用户密码过期登陆报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

因为是用户密码过期了

解决办法:

登陆MySQL命令行,执行

set password=password("password");

flush privileges;

#设置用户密码永不过期
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
查看用户密码默认过期时间设置,默认360天
mysql> show global variables like "default_password_lifetime";
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| default_password_lifetime | |
+---------------------------+-------+
row in set (0.00 sec) 设置用户密码永不过期(临时生效)
mysql> SET GLOBAL default_password_lifetime = ;
Query OK, rows affected (0.00 sec) mysql> show global variables like "default_password_lifetime";
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| default_password_lifetime | |
+---------------------------+-------+
row in set (0.00 sec)
如果要设置密码永不过期,设置my.cnf
vi /etc/my.cnf
添加
#设置用户密码永不过期
default_password_lifetime=0 这样重启后也是没问题的
查看用户密码设置
mysql> select user,host,password_expired,password_last_changed,password_lifetime from mysql.user;
+----------------+-----------+------------------+-----------------------+-------------------+
| user | host | password_expired | password_last_changed | password_lifetime |
+----------------+-----------+------------------+-----------------------+-------------------+
| root | localhost | N | -- :: | |
| mysql.sys | localhost | N | -- :: | NULL |
| root | % | N | -- :: | NULL |
| zabbix_monitor | localhost | N | -- :: | NULL |
+----------------+-----------+------------------+-----------------------+-------------------+
rows in set (0.00 sec)
创建一个有效期30天的用户
create user tt@'%' identified by '' password expire interval day; mysql> select user,host,password_expired,password_last_changed,password_lifetime from mysql.user;
+-----------+------------+------------------+-----------------------+-------------------+
| user | host | password_expired | password_last_changed | password_lifetime |
+-----------+------------+------------------+-----------------------+-------------------+
| root | localhost | N | -- :: | NULL |
| mysql.sys | localhost | N | -- :: | NULL |
| mysync | 10.73..% | N | -- :: | NULL |
| tt | % | N | -- :: | |
+-----------+------------+------------------+-----------------------+-------------------+

参考

mysql-5.7 密码过期详解 http://www.mamicode.com/info-detail-2042849.html

Mysql 5.7 账户过期设置 - CSDN博客 http://blog.csdn.net/jc_benben/article/details/77934469