code学习

MariaDB-10.2.10 二进制安装和开启审计日志

MariaDB-10.2.10 二进制安装和开启审计日志

一、快速安装过程

useradd mysql -s /sbin/nologin -M
mkdir /data1/mariadb/{data,logs,tmp,undolog,binlog}
cd /data1;chown -R mysql.mysql mariadb 
tar xf mariadb-10.2.10-linux-glibc_214-x86_64.tar.gz -C /usr/local/
cd /usr/local;ln -sv mariadb-10.2.10-linux-glibc_214-x86_64 mysql
cp /etc/my.cnf  /usr/local/mysql/.my.cnf 
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data1/mariadb/data  --defaults-file=./.my.cnf

启动命令:
启动方式一: /usr/local/mysql/bin/mysqld_safe  --defaults-file=/etc/my.cnf &
启动方式二:cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql; chmod +x  /etc/init.d/mysql; /etc/init.d/mysql start| service mysql start 
启动方式三:cp support-files/systemd/mariadb.service /usr/lib/systemd/system/mariadb.service; systemctl  start   mariadb
           

二、warning日志处理

启动mariadb服务后 发现一直出现大量的日志:

MariaDB-10.2.10 二进制安装和开启审计日志

发现原因是mariadb配置文件/etc/my.cnf 开启了线程池参数导致的问题

#thread_handling                     = pool-of-threads
#thread_pool_max_threads             = 1200
#thread_pool_size                    = 128
#thread_pool_stall_limit              = 10
           

关闭上面的参数停止输出上述warning日志

三、开启审计日志插件

install plugin server_audit soname 'server_audit.so';
set global server_audit_file_path='/data/mariadb/logs';
set global server_audit_events='connect,QUERY,table';
set global server_audit_file_rotate_size=104857600;
set global server_audit_file_rotations=100;
set global server_audit_excl_users='root';
set global server_audit_logging=on;
           

检查审计日志内容:

[root@tidb05 ~]# mysql -uwujianwei -h 127.0.0.1 -p'wujianwei' -e "select user,host from mysql.user;"

[root@tidb05 ~]# grep -w 'wujianwei' /data1/mariadb/logs/server_audit.log 
20220404 20:09:21,tidb05,wujianwei,127.0.0.1,1828,0,CONNECT,,,0
20220404 20:09:21,tidb05,wujianwei,127.0.0.1,1828,26,QUERY,,'select @@version_comment limit 1',0
20220404 20:09:21,tidb05,wujianwei,127.0.0.1,1828,27,READ,mysql,user,
20220404 20:09:21,tidb05,wujianwei,127.0.0.1,1828,27,QUERY,mysql,'select user,host from mysql.user',0
20220404 20:09:21,tidb05,wujianwei,127.0.0.1,1828,0,DISCONNECT,,,0
           

四、开启审计日志报错分析

执行sql指令:

set global server_audit_events='connect,QUERY_DML_NO_SELECT,QUERY_DDL,QUERY_DCL,table';
           

报错如下:

2022-04-04 20:02:36 140204444202752 [ERROR] /usr/local/mysql/bin/mysqld: Error while setting value 'connect,QUERY_DML_NO_SELECT,QUERY_DDL,QUERY_DCL,table' to 'server-audit-events'
           

问题分析:

从官网得知:

https://mariadb.com/kb/en/mariadb-audit-plugin-log-settings/

在 MariaDB Audit Plugin version 1.4.4版本才出现QUERY_DML_NO_SELECT 这个参数

server_audit: MariaDB Audit Plugin version 1.4.2 STARTED 目前版本的审计插件是不支持 QUERY_DML_NO_SELECT 这个过滤参数的,所以报错。
           
(Mon Apr  4 20:16:19 2022)[root@MySQL][(none)]>set global server_audit_events='connect,QUERY_DDL,QUERY_DCL,QUERY_DML,table';
Query OK, 0 rows affected (0.00 sec)

或者
(Mon Apr  4 20:20:08 2022)[root@MySQL][(none)]>set global server_audit_events='connect,QUERY,table';
Query OK, 0 rows affected (0.00 sec)
           
[root@tidb05 ~]# cat /etc/my.cnf
[client]
port	= 3306
user=root
password=Tsdsd3u78uRe
socket=/tmp/mysql.sock
host=localhost
prompt="(\\D)[\\u@MySQL][\\d]>"

[mysql]
#prompt="\u@db1 \R:\m:\s [\d]> "
#prompt="(\\D)[\\u@MySQL][\\d]>"
no-auto-rehash


[mysqld]
##MariaDB审计插件
loose-server_audit_events='connect,query,table'
loose-server_audit_logging=on
loose-server_audit_file_path=/data1/mariadb/logs/server_audit.log
loose-server_audit_file_rotate_size=100M
loose-server_audit_file_rotations=100


user	= mysql
port	= 3306
basedir	= /usr/local/mysql
datadir	= /data1/mariadb/data
socket	= /tmp/mysql.sock
pid-file = /data1/mariadb/tidb05.pid

character_set_server                = utf8mb4
collation_server                    = utf8mb4_unicode_ci
skip_name_resolve                   = 0

##若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = "+8:00"

open_files_limit                    = 65535
back_log                            = 1024
max_connections                     = 1500
max_connect_errors                  =100000000

table_cache                         = 1024
table_open_cache                    = 8192 
table_definition_cache              = 4096
table_open_cache_instances          = 64
thread_stack                        = 512K
thread_cache_size                   =1536                            
max_prepared_stmt_count             =1048576                       
sql_mode                            =STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

external_locking                    = FALSE
max_allowed_packet                  = 64M
sort_buffer_size                    = 4M
join_buffer_size                    = 4M
wait_timeout                        = 600
interactive_timeout                 = 600


tmp_table_size = 64M
max_heap_table_size = 64M
slow_query_log = 1
slow_query_log_file = /data1/mariadb/logs/slow.log
log-error = /data1/mariadb/logs/error.log
long_query_time = 1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1

server-id = 172160159
log-bin = /data1/mariadb/binlog/mysql-bin
binlog_format = row
binlog_checksum = 1
sync_binlog = 1
binlog_cache_size = 16M
max_binlog_cache_size = 4G
max_binlog_size = 1G
log_slave_updates                   = 1
skip_slave_start = 0
slave_parallel_workers              = 4  
#注意:MySQL 8.0开始,binlog_expire_logs_seconds选项也存在的话,会忽略expire_logs_days选项
expire_logs_days = 7
read_only                           =on

#master_info_repository = TABLE
#relay_log_info_repository = TABLE
#gtid_mode = on
#enforce_gtid_consistency = 1
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'

relay_log_recovery = 1
relay_log_purge = 1

key_buffer_size = 64M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0


transaction_isolation = REPEATABLE-READ
## InnoDB Settings ######
default_storage_engine              = InnoDB
default_tmp_storage_engine          =innodb
innodb_data_file_path               = ibdata1:1024M:autoextend
innodb_temp_data_file_path          =ibtmp1:12M:autoextend 
innodb_data_home_dir                = /data1/mariadb/data
innodb_log_group_home_dir           = /data1/mariadb/data          
innodb_file_per_table               = 1

innodb_buffer_pool_size = 2875M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 128M
innodb_log_file_size = 2G
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 2G
innodb_undo_directory = /data1/mariadb/undolog
innodb_undo_tablespaces = 95

innodb_log_block_size               = 512
innodb_page_size                    = 16k
innodb-status-file                  = 1
innodb_support_xa                   = 1
innodb_sync_spin_loops              = 30

# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 8000
innodb_io_capacity_max = 12000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_max_dirty_pages_pct_lwm = 10
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 15
innodb_spin_wait_delay   = 128
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0
innodb_undo_log_truncate = 1

#注意:MySQL 8.0.16开始删除该选项
##internal_tmp_disk_storage_engine = InnoDB
innodb_checksums = 1
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
query_cache_size = 0
query_cache_type = 0
innodb_undo_logs = 128

innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864

innodb_autoinc_lock_mode = 1


ft_min_word_len                     = 4
lower_case_table_names              = 0
myisam_recover                      = 1
net_read_timeout                    = 600
net_write_timeout                   = 600
#thread_handling                     = pool-of-threads
#thread_pool_max_threads             = 1200
#thread_pool_size                    = 128
#thread_pool_stall_limit              = 10
tmpdir                              = /data1/mariadb/tmp
secure-file-priv                    = /data1/mariadb/tmp
#复制过滤mysql
replicate_wild_ignore_table=mysql.%

#--------Persistent Optimizer Statistics
innodb_stats_auto_recalc                        =on                        
innodb_stats_persistent                         =on 
innodb_stats_persistent_sample_pages = 64

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"

[mysqldump]
quick
max_allowed_packet = 128M
           

继续阅读