修改密码报错

  1. mysql> set -u root password=password('mysql');
  2. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-u root password=password('mysql')' at line 1

原因

原来MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格。使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置。影响的语句和函数有:create user,grant,set password,password(),old password。

解决方案

解决办法调整MySQL密码验证规则,修改 policy 和 length 的值。

1. 查看MySQL版本

  1. [root@localhost ~]# mysql -V
  2. mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
  3. 如图所示我这里是8.0版本的MySQL

2. MySQL 5.7 进行如下设置,即可解决问题:

  1. mysql> set global validate_password_policy=0;
  2. mysql> set global validate_password_length=1;
  3. 但是8.0会报错,因为8.0的变量不存在
  4. mysql> set global validate_password_policy=0;
  5. ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'
  6. mysql> set global validate_password_length=5;
  7. ERROR 1193 (HY000): Unknown system variable 'validate_password_length'

第三第四步是参考讲解原因,也可以直接从第五步开始

3. 修改一个满足的密码 (如:Root_12root)

4. 修改密码后进MySQL,用命令查看 validate_password 密码验证插件是否安装。

  1. mysql> SHOW VARIABLES LIKE 'validate_password%';
  2. +--------------------------------------+-------+
  3. | Variable_name | Value |
  4. +--------------------------------------+-------+
  5. | validate_password.check_user_name | ON |
  6. | validate_password.dictionary_file | |
  7. | validate_password.length | 5 |
  8. | validate_password.mixed_case_count | 1 |
  9. | validate_password.number_count | 1 |
  10. | validate_password.policy | LOW |
  11. | validate_password.special_char_count | 1 |
  12. +--------------------------------------+-------+
  13. 7 rows in set (0.17 sec)

5. MySQL 8.0 调整密码验证规则:

  1. mysql> set global validate_password.policy=0;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> set global validate_password.length=5;
  4. Query OK, 0 rows affected (0.00 sec)

6. 重新设置密码

  1. mysql> alter user 'root'@'localhost' identified by 'mysql';
  2. Query OK, 0 rows affected (0.01 sec)

7. 刷新权限

flush privileges;