Skip to main content

How to Turn On the MySQL Password Policy with validate_password

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

MySQL can refuse weak passwords, with a component that's off by default in the Docker image. It takes one statement to turn on, and it's easy to see its rules, or score a password, before you rely on it.

Step 1: Install It​

As an administrator:

mysql>
INSTALL COMPONENT 'file://component_validate_password';
Output
Query OK, 0 rows affected (0.002 sec)

MySQL remembers installed components across restarts, in the mysql.component table.

Step 2: Read the Rules​

mysql>
SHOW VARIABLES LIKE 'validate_password%';
Output
+-------------------------------------------------+--------+
| Variable_name | Value |
+-------------------------------------------------+--------+
| validate_password.changed_characters_percentage | 0 |
| 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 |
+-------------------------------------------------+--------+
8 rows in set (0.003 sec)

The validate_password variables: a MEDIUM policy, a length of 8, and at least one mixed case letter, number and special character.

With the MEDIUM policy, a password needs a length of 8, with at least one number, mixed case letter and special character. To make it longer, for example:

mysql>
SET PERSIST validate_password.length = 16;

Step 3: See It Work​

mysql>
CREATE USER 'weak_demo'@'%' IDENTIFIED BY 'password';
Output
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

To score a password without creating anything, from 0 to 100:

mysql>
SELECT VALIDATE_PASSWORD_STRENGTH('password') AS weak, VALIDATE_PASSWORD_STRENGTH('8f3Kd92xQm1Zr7Tw#') AS strong;
Output
+------+--------+
| weak | strong |
+------+--------+
| 50 | 100 |
+------+--------+
1 row in set (0.000 sec)

It Can Refuse Strong Passwords​

A random hex password from openssl rand -hex 24 is long and strong, but it has no uppercase letter or special character. Against MEDIUM:

Output
+----------+
| long_hex |
+----------+
| 50 |
+----------+
1 row in set (0.000 sec)

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

So if your setup generates hex passwords, either adjust the rules, for example with SET PERSIST validate_password.special_char_count = 0, or generate them another way. How to Generate a Random Password for a MySQL Account gives you one that passes, though it holds characters a connection string needs quoted.

Step 4: Take It Out Again​

mysql>
UNINSTALL COMPONENT 'file://component_validate_password';
Output
Query OK, 0 rows affected (0.010 sec)

Existing passwords aren't checked again either way. The policy only applies when a password is set.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting it to check existing passwordsIt only checks new onesRotate them
Turning it on before a script that uses weak defaultsThe script failsFix the script's passwords
Lowering the policy for one passwordThe whole server has weaker rulesUse a better password

Troubleshooting​

SymptomLikely causeFix
ERROR 1819A password fails the policyThe fix
validate_password% shows nothingIt isn't installedRun INSTALL COMPONENT
ERROR 3529 (HY000): Cannot load component from specified URNIt's already installedSELECT * FROM mysql.component

Conclusion​

INSTALL COMPONENT 'file://component_validate_password' turns on the password policy. Read its rules, test it with VALIDATE_PASSWORD_STRENGTH, and check it against how your setup generates passwords.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.