MySQL8修改密码

MySQL8修改密码步骤

  1. 使用vi /etc/my.cnf,在配置文件里加入skip-grant-tables,然后systemctl restart mysqld.service,免密登录进入MySQL

  2. 使用select host, user, authentication_string, plugin from user;查看当前认证方式以及用户,默认是caching_sha2_password认证;

  3. 如果2是默认认证方式,则先修改为密码认证为mysql_native_password。在配置文件里加上default_authentication_plugin=mysql_native_password,后再重启MySQL,免密登录;

  4. 先清空root密码:

    1. update user set authentication_string='' where user='root';
    2. flush privileges;
  5. 退出MySQL,删除/etc/my.cnf文件里的skip-grant-tables,再重启一次MySQL,再次登录的时候使用空密码登录;

  6. 登录后即可修改密码了:

    1
    2
    alter user 'root'@'localhost' identified by 'root';
    set password for root@localhost = '123456';

修改密码

如果在修改密码时遇到:Your password does not satisfy the current policy requirements,说明你的密码强度不够,解决方案如下:

设置符合规则的密码

  1. 设置一个符合规则的密码:Abc123456!

设置简单密码

  1. 先设置符合规范的密码:Abc123456!

  2. 然后查看当前默认规则:show variables like 'validate_password%';

    密码策略

    1. validate_password_policy

      代表的密码策略,可配置的值有以下:默认是MEDIUM

      0 or LOW:仅需需符合密码长度(由参数validate_password_length指定)

      1 or MEDIUM:满足LOW策略,同时还需满足至少有1个数字,小写字母,大写字母和特殊字符

      2 or STRONG:满足MEDIUM策略,同时密码不能存在字典文件(dictionary file)中

    2. validate_password_dictionary_file

      用于配置密码的字典文件,当validate_password_policy设置为STRONG时可以配置密码字典文件,字典文件中存在的密码不得使用

    3. validate_password_length

      用来设置密码的最小长度,默认值是8最小是0

    4. validate_password_mixed_case_count

      当validate_password_policy设置为MEDIUM或者STRONG时,密码中至少同时拥有的小写和大写字母的数量,默认是1最小是0;默认是至少拥有一个小写和一个大写字母

    5. validate_password_number_count

      当validate_password_policy设置为MEDIUM或者STRONG时,密码中至少拥有的数字的个数,默认1最小是0

    6. validate_password_special_char_count

      当validate_password_policy设置为MEDIUM或者STRONG时,密码中至少拥有的特殊字符的个数,默认1最小是0

  3. 修改配置:

    1. 修改校验密码策略等级:set global validate_password.policy='LOW';
    2. 例如:设置密码长度:set global validate_password.length=6;
  4. 最后设置密码即可