mysql审计

  1. 云栖社区>
  2. 博客>
  3. 正文

mysql审计

JAAT9999 2019-03-26 17:28:23 浏览372
展开阅读全文

环境:mysql5.7.19+centos 7

一,审计安装:

1、下载mariadb-5.5.56-linux-x86_64.tar.gz解压获取server_audit.so插件

2、登录MySQL,执行命令获取MySQL的plugin目录

mysql> SHOW GLOBAL VARIABLES LIKE 'plugin_dir';
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| plugin_dir | /usr/lib64/mysql/plugin/ |
+---------------+--------------------------+
1 row in set (0.02 sec)

3、将server_audit.so上传到 /usr/lib64/mysql/plugin/下安装

#mkdir -p /data/home/admin/mysql/auditlogs/
mysql> INSTALL PLUGIN server_audit SONAME 'server_audit.so';
mysql> show variables like '%audit%';

4、编辑my.cnf,添加配置,指定哪些操作被记录到日志文件中

server_audit_logging=on
server_audit_file_path =/data/home/admin/mysql/auditlogs/  
server_audit_file_rotate_size=200000000
server_audit_file_rotations=200
server_audit_file_rotate_now=ON

5、重启服务,查看审计相关状态

#service mysqld restart
mysql> show variables like '%audit%';
+-------------------------------+---------------------------------------------------+
| Variable_name | Value |
+-------------------------------+---------------------------------------------------+
| server_audit_events | CONNECT,QUERY,TABLE,QUERY_DDL,QUERY_DML,QUERY_DCL |
| server_audit_excl_users | |
| server_audit_file_path | /data/mysql/auditlogs/ |
| server_audit_file_rotate_now | ON |
| server_audit_file_rotate_size | 200000000 |
| server_audit_file_rotations | 200 |
| server_audit_incl_users | |
| server_audit_loc_info | |
| server_audit_logging | ON |
| server_audit_mode | 1 |
| server_audit_output_type | file |
| server_audit_query_log_limit | 1024 |
| server_audit_syslog_facility | LOG_USER |
| server_audit_syslog_ident | mysql-server_auditing |
| server_audit_syslog_info | |
| server_audit_syslog_priority | LOG_INFO |
+-------------------------------+---------------------------------------------------+

6、参数说明:

详细请参考:https://mariadb.com/kb/en/mariadb/server_audit-system-variables/
server_audit_output_type:指定日志输出类型,可为SYSLOG或FILE
server_audit_logging:启动或关闭审计
server_audit_events:指定记录事件的类型,可以用逗号分隔的多个值(connect,query,table),如果开启了查询缓存(query cache),查询直接从查询缓存返回数据,将没有table记录
server_audit_file_path:如server_audit_output_type为FILE,使用该变量设置存储日志的文件,可以指定目录,默认存放在数据目录的server_audit.log文件中
server_audit_file_rotate_size:限制日志文件的大小
server_audit_file_rotations:指定日志文件的数量,如果为0日志将从不轮转
server_audit_file_rotate_now:强制日志文件轮转
server_audit_incl_users:指定哪些用户的活动将记录,connect将不受此变量影响,该变量比server_audit_excl_users优先级高
server_audit_syslog_facility:默认为LOG_USER,指定facility
server_audit_syslog_ident:设置ident,作为每个syslog记录的一部分
server_audit_syslog_info:指定的info字符串将添加到syslog记录
server_audit_syslog_priority:定义记录日志的syslogd priority
server_audit_excl_users:该列表的用户行为将不记录,connect将不受该设置影响
server_audit_mode:标识版本,用于开发测试

7、卸载server_audit

mysql> UNINSTALL PLUGIN server_audit;
mysql> show variables like '%audit%';
Empty set (0.00 sec)

8、审计日志:

image

二,开启审计后对mysql进行压力测试:

#unzip sysbench-master.zip
#yum install automake  autoconf libtool  libsysfs-dev  -y
#cd sysbench-master
#./autogen.sh
#./configure --prefix=/usr/local/sysbench
#make && make install

准备测试数据:

#sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua --mysql-host=10.200.172.56 --mysql-port=3306 --mysql-user=root --mysql-password='root' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=300000 --report-interval=10 --threads=128 prepare

开始测试:

sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua --mysql-host=10.200.172.56 --mysql-port=3306 --mysql-user=root --mysql-password='root' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=300000 --report-interval=10 --threads=128  run

测试完成清除数据:

sysbench /usr/local/sysbench/share/sysbench/oltp_read_write.lua --mysql-host=10.200.172.56 --mysql-port=3306 --mysql-user=root --mysql-password='root' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=300000 --report-interval=10 --threads=128  run

测试结果分析:

-- 每10秒钟报告一次测试结果,tps、每秒读、每秒写、99%以上的响应时长统计

[  10s] threads: 8, tps: 1111.51, reads/s: 15568.42, writes/s: 4446.13, response time: 9.95ms (99%)
[  20s] threads: 8, tps: 1121.90, reads/s: 15709.62, writes/s: 4487.80, response time: 9.78ms (99%)
[  30s] threads: 8, tps: 1120.00, reads/s: 15679.10, writes/s: 4480.20, response time: 9.84ms (99%)
[  40s] threads: 8, tps: 1114.20, reads/s: 15599.39, writes/s: 4456.30, response time: 9.90ms (99%)
[  50s] threads: 8, tps: 1114.00, reads/s: 15593.60, writes/s: 4456.70, response time: 9.84ms (99%)
[  60s] threads: 8, tps: 1119.30, reads/s: 15671.60, writes/s: 4476.50, response time: 9.99ms (99%)
OLTP test statistics:
   queries performed:
       read:                            938224    -- 读总数
       write:                           268064    -- 写总数
       other:                           134032    -- 其他操作总数(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等)
       total:                           1340320    -- 全部总数
   transactions:                        67016  (1116.83 per sec.)    -- 总事务数(每秒事务数)
   deadlocks:                           0      (0.00 per sec.)    -- 发生死锁总数
   read/write requests:                 1206288 (20103.01 per sec.)    -- 读写总数(每秒读写次数)
   other operations:                    134032 (2233.67 per sec.)    -- 其他操作总数(每秒其他操作次数)

General statistics:    -- 一些统计结果
   total time:                          60.0053s    -- 总耗时
   total number of events:              67016    -- 共发生多少事务数
   total time taken by event execution: 479.8171s    -- 所有事务耗时相加(不考虑并行因素)
   response time:    -- 响应时长统计
        min:                                  4.27ms    -- 最小耗时
        avg:                                  7.16ms    -- 平均耗时
        max:                                 13.80ms    -- 最长耗时
        approx.  99 percentile:               9.88ms    -- 超过99%平均耗时

Threads fairness:
   events (avg/stddev):           8377.0000/44.33
   execution time (avg/stddev):   59.9771/0.00

网友评论

登录后评论
0/500
评论
JAAT9999
+ 关注