1,问题描述
2,原因分析
3,解决方案
1,问题描述
- 问题出现的需求背景:
从数据库192.168.109.44/45 (业务数据库名testdb,业务用户名密码:testusr/Test2019)迁移数据到数据库192.168.51.43/44。 - 数据迁移的方案:
#导出,在源数据库中执行
mysqldump -uroot -p --master-data=2 --single-transaction --set-gtid-purged=OFF -A >/tmp/testdb3306_191212.sql
#导入,在目标数据库中执行
mysql -uroot -p </tmp/testdb3306_191212.sql
- 在目标库使用业务用户登录,报错
$ mysql -utestusr -pTest2019
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): ACCESS denied for user 'testusr'@'localhost' (using password:
- 猜想解决方法尝试
#有人可能猜想update密码获取就好了,那就让大家看一下实验结果
$ mysql -uroot -p
mysql> use mysql;
mysql>update user set authentication_string=PASSWORD('Test2019') where user='testers';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
猜想的方案并不能成功,反而又报了一个错误。
那产生该问题的具体原因是什么呢?请往下看。
2,原因分析
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误的字面意思是:你的密码不能满足当前的策略需求 。
看到该报错,就会立马想到目标mysql数据库肯定开启了密码策略。验证步骤:
- 在/etc/my.cnf文件中找到了启动校验密码机制的配置:
#for script
[mysqld]
plugin-load-add=validate_password.so
validate-password=FORCE_PLUS_PERMANENT
- 登录目标库mysql,查看密码校验插件的状态
mysql>select plugin_name,plugin_status from information_schema.plugins where plugin_name like 'validate%';
+-------------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+-------------------+---------------+
| validate_password | ACTIVE |
+-------------------+---------------+
通过以上两步验证,果然证实目标库启用了校验密码的插件。
查看该目标数据库的具体密码策略参数值:
root@localhost [(none)]>show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | ON |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
校验密码的策略是 validate_password_policy=MEDIUM。其实校验密码策略有3个级别:
- 0 or LOW
仅要求密码长度,可通过命令set global validate_password_length=number;进行更改。 - 1 or MEDIUM
要求密码长度;且要求数字,大小写字母,特殊字符都有。 - 2 or STRONG
要求密码长度;且要求数字,大小写字母,特殊字符都有;同时要求长度为4或更长的密码子字符串必须与字典文件中的单词不匹配(如果已经指定了一个单词)。要指定字典文件,请修改validate_password_dictionary_file。
3,解决方案
由以上原因分析,可得出两种解决方法:
方法一:把密码修改成符合密码策略要求的密码,比如Test@2019.com
$ mysql -uroot -p
mysql> use mysql;
mysql>update user set authentication_string=PASSWORD('Test@2019.com') where user='testusr';
mysql>flush privileges;
# 查看新密码的强度
mysql>select validate_password_strength('Test@2019.com');
+---------------------------------------------+
| validate_password_strength('Test@2019.com') |
+---------------------------------------------+
| 100 |
+---------------------------------------------+
1 row in set (0.00 sec)
此处提到了校验密码强度的函数:validate_password_strength(password args)。如果没有安装validate_password.os校验密码插件,validate_password_xxx系统变量没有设置,语句中的密码就没法被检查,VALIDATE_PASSWORD_STRENGTH()函数也总是返回0。例如,如果没有安装插件,帐户的密码可以少于8个字符,甚至没有密码。
方法二:降低密码策略级别
$ mysql -uroot -p
mysql> use mysql;
mysql> show variables like 'validate_password_policy';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| validate_password_policy | MEDIUM |
+--------------------------+--------+
mysql>set global validate_password_policy=0;
mysql> show variables like 'validate_password_policy';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| validate_password_policy | LOW |
+--------------------------+--------+
mysql>update user set authentication_string=PASSWORD('Test2019') where user='testers';
Query OK, 0 rows affected, 1 warning(0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
#再把密码策略改成原来的级别:
mysql>set global validate_password_policy=1;
通过以上两种解决方案,用户名密码都可以正常登录。
mysql -utestusr -p密码。