MySQL 管理运维:root 密码还可以这样改

来源:这里教程网 时间:2026-03-01 18:23:26 作者:

在 MySQL 的管理和运维过程中,修改 root 用户的密码是一个常见的操作。

通常情况下,我们使用 MySQL 自带的命令来修改 root 用户的密码,例如:

mysql -u root -p
Enter password: 旧密码
mysql> set password = '新密码';

不过,为了确保用户安全,尤其是 root 用户密码的健壮性,MySQL 服务器默认启用了密码校验组件。

validate_password 组件

是的,这里的表述是“组件”,不再是“插件”。

# ll *valid*
-rwxr-xr-x 1 root root 50136 Mar 17  2023 component_validate_password.so
-rwxr-xr-x 1 root root 33896 Mar 17  2023 validate_password.so

从 MySQL 8.0 开始,MySQL Server 引入了基于组件的基础架构,用于提高服务器的可扩展性。

在服务器的插件目录下,可以看到下列组件包:

[root@rocky9 plugin]# pwd
/usr/lib64/mysql/plugin
[root@rocky9 plugin]# ls component_*
component_audit_api_message_emit.so
component_example_component1.so
component_example_component2.so
component_example_component3.so
component_keyring_file.so
component_log_filter_dragnet.so
component_log_sink_json.so
component_log_sink_syseventlog.so
component_log_sink_test.so
component_mysqlbackup.so
component_mysqlx_global_reset.so
component_pfs_example_component_population.so
component_pfs_example.so
component_query_attributes.so
component_reference_cache.so
component_test_audit_api_message.so
component_test_backup_lock_service.so
component_test_component_deinit.so
component_test_host_application_signal.so
component_test_mysql_command_services.so
component_test_mysql_current_thread_reader.so
component_test_mysql_runtime_error.so
component_test_mysql_system_variable_set.so
component_test_mysql_thd_store_service.so
component_test_pfs_notification.so
component_test_pfs_resource_group.so
component_test_sensitive_system_variables.so
component_test_server_telemetry_traces.so
component_test_status_var_reader.so
component_test_status_var_service_int.so
component_test_status_var_service_reg_only.so
component_test_status_var_service.so
component_test_status_var_service_str.so
component_test_status_var_service_unreg_only.so
component_test_string_service_charset.so
component_test_string_service_long.so
component_test_string_service.so
component_test_system_variable_source.so
component_test_sys_var_service_int.so
component_test_sys_var_service_same.so
component_test_sys_var_service.so
component_test_sys_var_service_str.so
component_test_table_access.so
component_test_udf_registration.so
component_test_udf_services.so
component_udf_reg_3_func.so
component_udf_reg_avg_func.so
component_udf_reg_int_func.so
component_udf_reg_int_same_func.so
component_udf_reg_only_3_func.so
component_udf_reg_real_func.so
component_udf_unreg_3_func.so
component_udf_unreg_int_func.so
component_udf_unreg_real_func.so
component_validate_password.so
[root@rocky9 plugin]#

值得注意的是,在 MySQL 8.0 中,validate_password 插件被重新实现为 validate_password 组件。 validate_password 插件已弃用;预计它会在 MySQL 的未来版本中被删除。

相关参数更名为以  validate_password.* 前缀。

(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)

基于上述参数,我们在修改用户密码时需要注意密码的健壮性,可以通过下面这个函数来检查。

(root@localhost) [(none)]> SELECT VALIDATE_PASSWORD_STRENGTH('ShawnYan@CN_MYSQL8!');
+---------------------------------------------------+
| VALIDATE_PASSWORD_STRENGTH('ShawnYan@CN_MYSQL8!') |
+---------------------------------------------------+
|                                               100 |
+---------------------------------------------------+
1 row in set (0.00 sec)

修改 root 密码

只是,在开发环境中,为了便于测试,我们可以临时将 root 密码改为空。

这里,将密码校验组件包改名,并重启 MySQL,检验功能会失效,但不影响 Server 正常启动。

cd /usr/lib64/mysql/plugin/
mv component_validate_password.so component_validate_password.so.bak
systemctl restart mysqld
2023-06-11T16:23:49.575004Z 0 [ERROR] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-001126 - Can't open shared library '/usr/lib64/mysql/plugin/component_validate_password.so' (errno: 0 /usr/lib64/mysql/plugin/component_validate_password.so: cannot open shared object file: No such file or directory)
2023-06-11T16:23:49.575088Z 0 [ERROR] [MY-013129] [Server] A message intended for a client cannot be sent there as no client-session is attached. Therefore, we're sending the information to the error-log instead: MY-003529 - Cannot load component from specified URN: 'file://component_validate_password'.

如此,即可不考虑密码强度而直接修改 root 密码。

(root@localhost) [(none)]> set password = '';
Query OK, 0 rows affected (0.01 sec)

最后,这种方式仅限于开发调试,不推荐在生产环境操作。 logo.jpg

相关推荐