目录

Mysql-密码管理

3

初始化密码

刚安装好的mysql服务是没有密码的,我们需要给root用户设置一个密码

修改密码

进入mysql数据库,执行

alter user root@"localhost" identified by "root";

密码策略

查看策略

  • 以下命令可查看现有的密码策略

    show variables like "validate_password%";

      mysql> show variables like "validate_password%";
      +-------------------------------------------------+-------+
      | Variable_name                                   | Value |
      +-------------------------------------------------+-------+
      | validate_password.changed_characters_percentage | 0     |
      | validate_password.check_user_name               | ON    |
      | validate_password.dictionary_file               |       |
      | validate_password.length                        | 4     |
      | validate_password.mixed_case_count              | 1     |
      | validate_password.number_count                  | 1     |
      | validate_password.policy                        | LOW   |
      | validate_password.special_char_count            | 1     |
      +-------------------------------------------------+-------+
      8 rows in set (0.01 sec)
    
  • 如果执行命令没有相应输出,一般是模组没有启用(mysql8.0.17+版本)或插件没有启用(mysqlmysql5.7版本)

    启用模块:INSTALL COMPONENT 'file://component_validate_password';

    启用插件:INSTALL PLUGIN validate_password SONAME 'validate_password.so';

修改策略

  1. 临时修改,重启命令行重置

    以下命令可修改现有密码策略

    set global validate_password.length = 4;

  2. 永久修改

    修改配置文件 vim /etc/my.cnf.d/mysql-server.cnf

       [mysqld]
       datadir=/var/lib/mysql
       socket=/var/lib/mysql/mysql.sock
       log-error=/var/log/mysql/mysqld.log
       pid-file=/run/mysqld/mysqld.pid
       bind-address = 0.0.0.0
    
       #新增
       validate_password.length = 4
       validate_password.policy = LOW
    

破解root密码

有感破解(需重启服务)

  1. 修改配置文件 vim /etc/my.cnf.d/mysql-server.cnf

       [mysqld]
       skip-grant-tables		#跳过授权表
       #datadir=/var/lib/mysql
       #socket=/var/lib/mysql/mysql.sock
       #log-error=/var/log/mysql/mysqld.log
       #pid-file=/run/mysqld/mysqld.pid
       #bind-address = 0.0.0.0
    
  2. 重启服务

  3. 登录mysql,修改root密码为空

       mysql>FLUSH PRIVILEGES;
       mysql>USE mysql;
       # 清空 root@localhost 和 root@% 的密码(设为空)
       mysql>UPDATE user SET authentication_string = '' WHERE User = 'root' AND Host = 'localhost';
       #或者只改特定 host
       #UPDATE user SET authentication_string = '' WHERE User = 'root' AND Host = '%';
    
  4. 还原mysqld配置文件

       [mysqld]
       datadir=/var/lib/mysql
       socket=/var/lib/mysql/mysql.sock
       log-error=/var/log/mysql/mysqld.log
       pid-file=/run/mysqld/mysqld.pid
       bind-address = 0.0.0.0
    
  5. 重启服务

  6. 登录mysql,修改root密码

       mysql>alter user root@"localhost" identified by "root";
    

推荐文章

Mysql-条件查询
Mysql-基础查询