MySQL安全

MySQL安全

文章目录

  • MySQL安全
    • 一、以非root用户登录Mysql
    • 二、普通用户授予有限权限
    • 三、 设置强密码
    • 四、 设置登录错误限制
      • 1 环境
      • 2 插件介绍
      • 3 设置步骤
    • 五、Mysql配置文件
    • 六、设置Nginx代理转发MySQL

一、以非root用户登录Mysql

# 以普通帐户安全运行mysqld,禁止mysql以root帐号权限运行,攻击者可能通过mysql获得系统root超级用户权限,完全控制系统。
# 配置/etc/my.cnf:
[mysql.server]
user=mysql

二、普通用户授予有限权限

# 授予指定库(例如onepay)的基本权限:
# alter, create ,delete, index, insert, lock, references, select, update。

grant   alter, create ,delete, index, insert, lock, references, select, update  on  'testdb'.*  to 'user1';

三、 设置强密码

# 1 修改配置文件,设置密码要求
Linux的/etc/my.cnf:
[mysqld]
plugin-load=validate_password.so
validate-password=FORCE_PLUS_PERMANENT
# 2 重启mysql , 查看密码策略:
SHOW VARIABLES LIKE "%password%";

MySQL安全_第1张图片

validate_password_length	# 密码最小长度,参数默认为8。
validate_password_mixed_case_count	#密码至少要包含的小写字母个数和大写字母个数。
validate_password_number_count 	#密码至少要包含的数字个数。	
validate_password_policy	#密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG。默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
validate_password_special_char_count	# 密码至少要包含的特殊字符数。
# 3 如果需要修改,其他变量类似:
SET GLOBAL validate_password_length=12;	
# 4 查看validate-password 是否生效
show  plugins;

在这里插入图片描述

四、 设置登录错误限制

参考:https://blog.csdn.net/sumengnan/article/details/114111579

1 环境

MySQL 版本5.7.17以上

Centos 7.9

2 插件介绍

Connection-Control插件用来控制客户端在登录操作连续失败一定次数后的响应的延迟。可防止客户端暴力破解。

3 设置步骤

# 第一步:在配置文件中指明 插件目录位置  
/etc/my.cnf
[mysqld]
plugin-dir=/usr/lib64/mysql/plugin  #按照自己的mysql插件位置来写
#通过配置文件安装插件  或   动态的install 安装   二选一
plugin-load-add = connection_control.so
connection-control = FORCE
connection-control-failed-login-attempts = FORCE
connection_control_min_connection_delay = 60000  # 最少延迟60s
connection_control_max_connection_delay = 180000  # 最多延迟180s
connection_control_failed_connections_threshold	= 3   # 登录错误3次触发延迟
# 重启后再登录mysql,show   variables like 'plugin%' 查看该配置是否生效
# 这个插件目录里就是一堆.so文件
# 可以使用以下命令查找mysql插件路径
[root@vm-0-12-centos plugin]# find /usr/ * -name connection_control.so
/usr/lib64/mysql/plugin/connection_control.so
/usr/lib64/mysql/plugin/debug/connection_control.so
connection_control.so
debug/connection_control.so

MySQL安全_第2张图片
MySQL安全_第3张图片

# 第二步:查看MySQL是否已安装该插件
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'connection_control%';
show variables like 'connection_control%';
# 如果都显示为空,则没有安装
# 第三步: 安装插件 INSTALL 为动态安装,重启后不会失效,但延迟时间会失效
INSTALL PLUGIN connection_control SONAME 'connection_control.so';
INSTALL PLUGIN connection_control_failed_login_attempts SONAME 'connection_control.so';
# 第四步:验证插件是否安装成功
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'connection_control%';
show variables like 'connection_control%';
show plugins;

MySQL安全_第4张图片

# 第五步:设置登录限制
set global connection_control_failed_connections_threshold=3;  # 输错密码三次
set global connection_control_min_connection_delay=60000 ; # 限制登录最少60s,即最少60s后才可以登录
set global connection_control_max_connection_delay=180000;  

# 第六步:故意错误登录3次,查看错误记录
use information_schema;
select * from connection_control_failed_login_attempts;
# 连接成功后,之前的登录错误次数会归0

MySQL安全_第5张图片

五、Mysql配置文件

# 安全
# 强密码
[mysqld]
user=mysql
plugin-load=validate_password.so
validate-password=FORCE_PLUS_PERMANENT
# 登录错误限制
plugin-dir=/usr/lib64/mysql/plugin
plugin-load-add = connection_control.so
connection-control = FORCE
connection-control-failed-login-attempts = FORCE
connection_control_min_connection_delay = 60000  # 最少延迟60s
connection_control_max_connection_delay = 180000  # 最多延迟180s
connection_control_failed_connections_threshold = 3   # 登录错误3次触发延迟

审计 ~~~~

六、设置Nginx代理转发MySQL

/app/nginx/conf/nginx.conf
http{
~~~
}
# 配置MySQL代理13306
stream {
   log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

    access_log /app/nginx/logs/13306-access.log proxy ;
    open_log_file_cache off;
    server {
       listen 13306; 
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass 192.168.10.2:3306;    
    }


你可能感兴趣的:(数据库,mysql,安全,数据库)