MySQL安全加固
资料来源于墨天轮.cnf的文件,大家自己看看文件内容就知道每个文件的作用了。
/etc/mysql/my.cnf,因为其它两个文件不存在。
root@NF:~# mysql --help |grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/my.cnf etc/mysql/my.cnf ~/.my.cnf
/etc/mysql/my.cnf文件的内容(跟
/etc/mysql/mysql.cnf内容一样)主要有下面这些信息,也说明了
/etc/mysql/my.cnf是全局配置,
~/.my.cnf(隐藏文件本环境下无此文件)是个人用户设置。
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
!includedir /etc/mysql/conf.d/
#表示包含/etc/mysql/conf.d/这个路径下面的配置文件,前提是必须以为.cnf为后缀
!includedir /etc/mysql/mysql.conf.d/
#表示包含/etc/mysql/mysql.conf.d/这个路径下面的配置文件,前提是必须以为.cnf为后缀
/usr/lib/mysql # 动态库文件(.so文件,so=shared object)
/usr/bin/mysql # mysql命令,安装的软件的命令,usr指Unix System Resource
/usr/share/mysql # mysql共享数据,主要是一些帮助文档
/etc/mysql # mysql配置文件目录
/etc/init.d/mysql # 服务管理脚本(启动,停止,关闭等)
/var/lib/mysql # 默认的数据文档存储目录
/var/log/mysql # mysql日志文件(查询语句记录,报错日志,慢查询日志等)
00 关注官方安全更新公告
01 禁止数据库用户的密码为空并设置密码有效期
select user,host from mysql.user where length(authentication_string) = 0;
或
select user,host,authentication_string,password_lifetime,account_locked from mysql.user;
set password for 'user'@'host' = password('yourpassword');
set password for 'testtest'@'192.168.56.1' = password('testtest');
禁用或限制匿名、默认账户、测试账户的访问权限;(禁用账户) 应重命名或删除默认账户,修改默认账户的默认口令; 应及时删除或停用多余的、过期的账户,避免共享账户的存在; 删除了默认数据库TEST。(旧版本会有默认的测试数据库)
ALTER USER 'user'@'host' ACCOUNT LOCK;
mysql> show global variables like 'default_password_lifetime';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| default_password_lifetime | 0 |
+---------------------------+-------+
1 row in set (0.00 sec)
default_password_lifetime = 180
ALTER USER 'root'@'localhost' PASSWORD EXPIRE INTERVAL 180 DAY;
02 检查数据库用户的密码是否为弱口令
关于MySQL密码你应该知道的那些事 - cenalulu MySQL:密码加密方式 - xuejianbest
03 密码复杂度配置
[mysqld]
plugin-load = "validate_password.so"
validate-password = FORCE_PLUS_PERMANENT
validate_password_length = 8
validate_password_policy = 1
validate_password_mixed_case_count = 1
validate_password_number_count = 1
validate_password_special_char_count = 1
validate-password = FORCE_PLUS_PERMANENT值为
FORCE_PLUS_PERMANENT表示强制启用该插件,并且不能被卸载。
validate_password_policy表示密码策略,有三个值,与其对应的策略见下表:
mysql> show variables like '%validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| 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)
安装和卸载插件 validate_password插件相关参数的介绍 MySql5.6使用validate password 插件加强密码强度的安装及使用方法 - wangmm0218
04 登录失败和连接超时设置
mysql> show variables like "%connection_control%";
+-------------------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------------------+-------+
| connection_control_failed_connections_threshold | 3 |
| connection_control_max_connection_delay | 86400 |
| connection_control_min_connection_delay | 1000 |
+-------------------------------------------------+-------+
3 rows in set (0.00 sec)
失败尝试的次数,默认为3,表示当连接失败3次后启用连接控制0表示不开启
connection_control_max_connection_delay
响应延迟的最大时间
connection_control_min_connection_delay
响应延迟的最小时间,默认1000微秒,1秒
show global variables like 'interactive_timeout';
show global variables like 'wait_timeout';
set global interactive_timeout=1800;
set global wait_timeout=1800;
interactive_timeout:交互式连接超时时间(mysql工具、mysqldump等)
wait_timeout:非交互式连接超时时间、默认的连接mysql api程序、jdbc连接数据库等
连接控制插件安装 MySQL安全插件:Connection-Control Plugins 的利与弊 - leonpenn MySQL 插件之 连接控制插件(Connection-Control) - ZhenXing_Yu MySQL连接超时相关的两个参数interactive_timeout和wait_timeout的区别和解释 - young5201314 MySQL参数max_connect_errors分析释疑 - 潇湘隐者 MySQL状态变量Aborted_connects与Aborted_clients浅析 -海东潮
05 启用SSL
validate-password = FORCE_PLUS_PERMANENT值为
FORCE_PLUS_PERMANENT表示强制启用该插件,并且不能被卸载。
validate_password_policy表示密码策略,有三个值,与其对应的策略见下表:
mysql> show variables like '%validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| 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)
安装和卸载插件 validate_password插件相关参数的介绍 MySql5.6使用validate password 插件加强密码强度的安装及使用方法 - wangmm0218
04 登录失败和连接超时设置
mysql> show variables like "%connection_control%";
+-------------------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------------------+-------+
| connection_control_failed_connections_threshold | 3 |
| connection_control_max_connection_delay | 86400 |
| connection_control_min_connection_delay | 1000 |
+-------------------------------------------------+-------+
3 rows in set (0.00 sec)
失败尝试的次数,默认为3,表示当连接失败3次后启用连接控制,0表示不开启
connection_control_max_connection_delay
响应延迟的最大时间
connection_control_min_connection_delay
响应延迟的最小时间,默认1000微秒,1秒
show global variables like 'interactive_timeout';
show global variables like 'wait_timeout';
set global interactive_timeout=1800;
set global wait_timeout=1800;
interactive_timeout:交互式连接超时时间(mysql工具、mysqldump等)
wait_timeout:非交互式连接超时时间、默认的连接mysql api程序、jdbc连接数据库等
连接控制插件安装 MySQL安全插件:Connection-Control Plugins 的利与弊 - leonpenn MySQL 插件之 连接控制插件(Connection-Control) - ZhenXing_Yu MySQL连接超时相关的两个参数interactive_timeout和wait_timeout的区别和解释 - young5201314 MySQL参数max_connect_errors分析释疑 - 潇湘隐者 MySQL状态变量Aborted_connects与Aborted_clients浅析 -海东潮
05 启用SSL
mysql> show variables like '%ssl';
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| have_openssl | DISABLED |
| have_ssl | DISABLED |
+---------------+----------+
2 rows in set (0.01 sec)
select user,host from mysql.user;),则已经符合安全要求,此情况下已无需启用SSL。
MySQL SSL配置(mysql5.7和mysql5.6) - Yuki_xiong MYSQL SSL配置与使用 - 德莱華
06 远程管理限制
GRANT ALL PRIVILEGES ON <databases-name>.* TO 'user'@'<ip>' IDENTIFIED BY '<password>' WITH GRANT OPTION;
FLUSH PRIVILEGES;
# 举例,给数据库用户teacher分配student数据库,只允许192.168.56.%网段远程连接并设置口令为Admin123。
GRANT ALL PRIVILEGES ON student.* TO 'teacher'@'192.168.56.%' IDENTIFIED BY 'Admin123' WITH GRANT OPTION;
FLUSH PRIVILEGES;
select user,host from mysql.user where account_locked='N' and host!='localhost';
drop user 'user'@'host';
drop
drop user XXX;删除已存在的用户,默认删除的是'XXX'@'%'这个用户,如果还有其他的用户(其它主机名),如'XXX'@'localhost'等,不会一起被删除。如果要删除'XXX'@'localhost',使用drop删除时需要加上host即drop user 'XXX'@'localhost'。 delete
delete from user where user='XXX' and host='localhost';其中XXX为用户名,localhost为主机名(即需指定主机名)。 drop和delete的区别
drop不仅会将user表中的数据删除,还会删除其他权限表的内容。而delete只删除user表中的内容,所以使用delete删除用户后需要执行FLUSH PRIVILEGES;刷新权限,否则下次使用create语句创建用户时会报错。
07 会话连接数配置
mysql> show variables like "%connections";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| max_connections | 100 |
| max_user_connections | 0 |
+----------------------+-------+
2 rows in set (0.01 sec)
max_user_connections是对每个用户的限制,
为0表示不限制。
root@NF:~$ grep max_connections etc/mysql/mysql.conf.d/mysqld.cnf
max_connections = 100
MySQL参数最大连接数max_connections - paul_hch
08 启用日志审计
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#通用日志,将所有到达MySQL Server的SQL语句记录下来
general_log_file = var/log/mysql/mysql.log
general_log = 1
log_timestamps = SYSTEM
#
# Error log - should be very few entries.
#错误日志,文件内容不会很多
log_error = var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#慢查询日志,记录SQL执行语句(执行时间超过2秒才会记录)
slow_query_log = 1
slow_query_log_file = var/log/mysql/mysql-slow.log
long_query_time = 2
log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#二进制日志
server-id = 1
log_bin = var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
相关日志(主要是查询日志和错误日志)应留存6个月以上。 日志记录的日期和时间应当是正确的,服务器需开启了NTP服务进行时间校对。
#Linux 检查NTP服务时间同步情况
ntpq -p -n
ntpstat
09 禁止.mysql_history文件记录信息
检查所有.mysql_history文件是否链接到dev/null,若没连接到,则以root用户执行如下命令:
find -name ".mysql_history" | xargs
rm <your_path>/.mysql_history
ln -s dev/null <your_path>/.mysql_history
show variables like 'local_infile';
[mysqld]
local_infile = 0
11 用户权限合理分配
select user,host,account_locked from mysql.user;
show grants for 'user'@'host';
select * from mysql.user where user='user' and host='host' \G;
不能存在特权用户 不存在越权访问情况(绕过访问控制策略) mysql 数据库应当只允许root用户进行访问和管理
others
最小权限原则 1.对于数据库,可以一个数据库用户分配一个数据库 2.对于mysql进程,不得以root用户运行,默认是采用了mysql用户运行。 更改默认开放端口3306 站库分离
SET GLOBAL default_password_lifetime = 180;
#设置全局变量及赋值。
INSTALL PLUGIN validate_password SONAME 'validate_password.so';
#安装插件,这里是安装配置密码复杂度策略的插件。
update user set password=password('123') where user='root' and host='localhost';
# mysql 5.7以下
update mysql.user set authentication_string=PASSWORD('newpassword') where user='username' and host='localhost';
# mysql 5.7以上
alter user 'root'@'localhost' identified by 'newpassword';
# mysql 8.0以上
侵权请私聊公众号删文
热文推荐