server_audit.so插件参数说明

为了方便查看和理解,把MariaDB官网关于server_audit插件的一些参数说明跟一个中文博客的内容杂糅在一起。

官网地址:https://mariadb.com/kb/en/library/mariadb-audit-plugin-system-variables/

中文博文:http://blog.51cto.com/daisywei/1905207

server_audit插件
用途:审计mysql5.6版本以上或者MariaDB数据的用户操作行为

有以下系统参数作为审计功能配置使用:

server_audit_output_type:指定日志输出类型,可为SYSLOG或FILE
Description: Specifies the desired output type. Can be SYSLOG or FILE. For example: SET GLOBAL server_audit_output_type=file file: log records will be saved into the rotating log file. The name of the file set by server_audit_file_path variable. syslog: log records will be sent to the local syslogd daemon with the standard <syslog.h> API. The default value is 'file'.
Commandline: --server-audit-output-type=value
Scope(作用范围): Global
Dynamic(值是否动态): Yes
Data Type: enum(枚举类型)
Default Value: file
Valid Values: SYSLOG or FILE

server_audit_logging:启动或关闭审计
Description: Enables/disables the logging. Expected values are ON/OFF. For example: SET GLOBAL server_audit_logging=on If the server_audit_output_type is FILE, this will actually create/open the logfile so the server_audit_file_path should be properly specified beforehand. Same about the SYSLOG-related parameters. The logging is turned off by default.
Commandline: --server-audit-logging[={0|1}]
Scope: Global
Dynamic: Yes
Data Type: boolean
Default Value: OFF

server_audit_events:指定记录事件的类型,可以用逗号分隔的多个值(connect,query,table),如果开启了查询缓存(query cache),查询直接从查询缓存返回数据,将没有table记录
Description: If set it specifies the set of types of events to log. For example: SET GLOBAL server_audit_events='connect, query'
Commandline: --server-audit-events=value
Scope: Global
Dynamic: Yes
Data Type: string
Default Value: Empty string
Valid Values: CONNECT, QUERY and TABLE (QUERY_DDL, QUERY_DML added in 1.2.0 and QUERY_DCL added in 1.3.0, QUERY_DML_NO_SELECT added in 1.4)

server_audit_file_path:如server_audit_output_type为FILE,使用该变量设置存储日志的文件,可以指定目录,默认存放在数据目录的server_audit.log文件中
Description: When server_audit_output_type=file, sets the path and the filename to the log file. If the specified path exists as a directory, then the log will be created inside that directory with the name 'server_audit.log'. Otherwise the value is treated as a filename. The default value is 'server_audit.log', which means this file will be created in the database directory.
Commandline: --server-audit-file-path=value
Scope: Global
Dynamic: Yes
Data Type: string
Default Value: server_audit.log

server_audit_file_rotate_size:限制日志文件的大小
Description: When server_audit_output_type=file, it limits the size of the log file. Reaching that limit turns on the rotation - the current log file is renamed as 'file_path.1'. The empty log file is created as 'file_path' to log into it. The default value is 100000.
Commandline: --server-audit-rotate-size=#
Scope: Global
Dynamic: Yes
Data Type: numeric
Default Value: 1000000

server_audit_file_rotations:指定日志文件的数量,如果为0日志将从不轮转
Description: When server_audit_output_type=file', this specifies the number of rotations to save. If set to 0 then the log never rotates. The default value is 9.
Commandline: --server-audit-rotations=#
Scope: Global
Dynamic: Yes
Data Type: numeric
Default Value: 9
Range: 0 to 999

server_audit_file_rotate_now:强制日志文件轮转
Description: When server_audit_output_type=file, the user can force the log file rotation by setting this variable to ON or 1.
Commandline: --server-audit-rotate-now[={0|1}]
Scope: Global
Dynamic: Yes
Data Type: boolean
Default Value: OFF

server_audit_incl_users:指定哪些用户的活动将记录,connect将不受此变量影响,该变量比server_audit_excl_users优先级高
Description: If not empty, it contains a comma-delimited list of users whose activity will be logged. For example: SET GLOBAL server_audit_incl_users='user_foo, user_bar'. CONNECT records aren't affected by this variable - they are always logged. This setting has higher priority than server_audit_excl_users. So if the same user is specified both in incl_ and excl_ lists, they will still be logged.
Commandline: --server-audit-incl-users=value
Scope: Global
Dynamic: Yes
Data Type: string
Default Value: Empty string

server_audit_syslog_facility:默认为LOG_USER,指定facility,其实这里的意思当为SYSLOG文件变量时,给记录定义一个“设施”记录到这个syslog中,可以用这个facility参数来过滤log。
Description: SYSLOG-mode variable. It defines the 'facility' of the records that will be sent to the syslog. Later the log can be filtered by this parameter.
Commandline: --server-audit-syslog-facility=value
Scope: Global
Dynamic: Yes
Data Type: enum
Default Value: LOG_USER
Valid Values: LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS, LOG_UUCP, LOG_CRON, LOG_AUTHPRIV, LOG_FTP, and LOG_LOCAL0–LOG_LOCAL7

server_audit_syslog_ident:设置ident,作为每个syslog记录的一部分,更改新的值后,需要重启这个审计功能
Description: SYSLOG-mode variable. String value for the 'ident' part of each syslog record. Default value is 'mysql-server_auditing'. New value becomes effective only after restarting the logging.
Commandline: --server-audit-syslog-ident=value
Scope: Global
Dynamic: Yes
Data Type: string
Default Value: mysql-server_auditing

server_audit_syslog_info:指定的info字符串将添加到syslog记录,随时可更改
Description: SYSLOG-mode variable. The 'info' string to be added to the syslog records. Can be changed any time.
Commandline: --server-audit-syslog-info=value
Scope: Global
Dynamic: Yes
Data Type: string
Default Value: Empty string

server_audit_syslog_priority:定义记录日志的syslogd priority优先级
Description: SYSLOG-mode variable. Defines the priority of the log records for the syslogd.
Commandline: --server-audit-syslog-priority=value
Scope: Global
Dynamic: Yes
Data Type: enum
Default Value: LOG_INFO
Valid Values:LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG

server_audit_excl_users:该列表的用户行为将不记录,connect将不受该设置影响
Description: If not empty, it contains the list of users whose activity will NOT be logged.    For example: SET GLOBAL server_audit_excl_users='user_foo, user_bar'. CONNECT records aren't affected by this variable - they are always logged. The user is still logged if it's specified in server_audit_incl_users.
Commandline: --server-audit-excl-users=value
Scope: Global
Dynamic: Yes
Data Type: string
Default Value: Empty string

server_audit_mode:标识版本,用于开发测试
Description: This variable doesn't have any distinctive meaning for a user. Its value mostly reflects the server version with which the plugin was started and is intended to be used by developers for testing.
Commandline: --server-audit-mode[=#]


可用shell脚本的方式,根据自己数据库审计活动配置相关参数到/etc/mysql目录my.cnf配置文件中,但是必须重启mysql服务
例如:
server_audit_logging=on
server_audit_events=‘query_dml,query_ddl‘ 
server_audit_file_path =/var/log
server_audit_file_rotate_size=2G
server_audit_file_rotations=30

<think>我们面对的问题是:在MySQL 8中安装MariaDB的server_audit插件时出现错误,提示“undefined symbol PSI_server”。这是因为MySQL 8和MariaDB的插件在内部符号上存在不兼容。 根据引用[2]和引用[3]的信息,我们知道MariaDB的审计插件可以用于MySQL,但需要特别注意版本兼容性。在MySQL 5.7中,使用MariaDB 10.6版本的插件会出现问题,而我们现在是MySQL 8,问题可能更复杂。 错误信息“undefined symbol PSI_server”表明插件在尝试调用MySQL服务器内部的一个符号(PSI_server)时找不到。这通常是因为插件的编译环境(MariaDB)与运行环境(MySQL 8)的符号表不一致。 ### 解决方案 #### 方法1:使用与MySQL 8兼容的MariaDB审计插件版本 1. **获取正确的插件版本**:需要下载与MySQL 8兼容的MariaDB审计插件。根据引用[3]的提示,我们可以尝试使用MariaDB 10.5版本的插件(因为10.5相对稳定且可能兼容性较好)。但注意,MySQL 8的内部API变化较大,可能没有直接兼容的版本。 2. **下载步骤**(以MariaDB 10.5为例): ```bash # 下载MariaDB 10.5的二进制包(注意选择与MySQL 8相同的操作系统架构,如x86_64) wget https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.5.3/bintar-linux-x86_64/mariadb-10.5.3-linux-x86_64.tar.gz tar -zxvf mariadb-10.5.3-linux-x86_64.tar.gz ``` 3. **提取插件**: ```bash # 在解压后的目录中查找server_audit.so find mariadb-10.5.3-linux-x86_64 -name "server_audit.so" # 假设找到的路径是:mariadb-10.5.3-linux-x86_64/lib/plugin/server_audit.so cp mariadb-10.5.3-linux-x86_64/lib/plugin/server_audit.so /usr/local/mysql/lib/plugin/ chown mysql:mysql /usr/local/mysql/lib/plugin/server_audit.so ``` 4. **加载插件**: ```sql INSTALL PLUGIN server_audit SONAME 'server_audit.so'; ``` 如果仍然出现相同的错误,则说明这个版本也不兼容。 #### 方法2:使用Percona的审计插件(推荐) 由于MariaDB插件在MySQL 8上兼容性问题较多,建议使用Percona的审计插件(如果使用的是Percona Server)或者使用MySQL企业版的审计插件(需要企业版许可)。但如果是社区版,可以考虑使用Percona的插件。 1. **下载Percona Audit Plugin**: 访问Percona的下载页面,找到与MySQL 8.0兼容的版本。例如,从Percona Server for MySQL 8.0中提取插件。 2. **提取插件**: 安装Percona Server(或仅下载二进制包),然后从`/usr/lib/percona-server/plugin/`(具体路径根据安装)中获取`audit_log.so`。 3. **复制到MySQL插件目录**: ```bash cp audit_log.so /usr/local/mysql/lib/plugin/ chown mysql:mysql /usr/local/mysql/lib/plugin/audit_log.so ``` 4. **加载插件**: ```sql INSTALL PLUGIN audit_log SONAME 'audit_log.so'; ``` #### 方法3:使用MySQL社区版替代方案 如果以上方法都不可行,可以考虑使用以下替代方案记录登录信息: 1. **通用日志(General Log)**:记录所有查询,包括连接。 ```sql SET GLOBAL general_log = 'ON'; SET GLOBAL log_output = 'TABLE'; ``` 然后从`mysql.general_log`表中查询连接事件。 2. **使用init_connect配置**(仅记录普通用户的连接): ```ini [mysqld] init_connect='INSERT INTO audit.access_log (user, host, login_time) VALUES (CURRENT_USER(), SUBSTRING_INDEX(USER(),"@",-1), NOW())' ``` 注意:需要先创建审计表,并且超级用户不会执行`init_connect`。 ### 错误排查 如果尝试加载插件时出现错误,可以通过以下命令查看详细的错误信息: ```bash # 使用ldd检查依赖 ldd /path/to/server_audit.so # 使用nm检查符号 nm -D /path/to/server_audit.so | grep PSI_server ``` 如果确实缺少符号,可能需要重新编译插件或寻找其他兼容版本。 ### 总结 由于MySQL 8的内部API与MariaDB存在差异,直接使用MariaDB的审计插件可能不可行。建议: 1. 优先尝试Percona的审计插件(如果使用Percona Server)。 2. 使用MySQL企业版审计插件(如果有许可)。 3. 采用通用日志或init_connect作为替代方案。 > ⚠️ **注意**:修改插件目录文件前,请备份数据并确保MySQL服务已停止。 --- ### 参考引用 - [^1] MySQL升级后插件安装报错问题 - [^2] MySQL 5.7安装MariaDB审计插件的注意事项 - [^3] MariaDB审计插件与MySQL的兼容性说明 - [^4] MySQL 5.6审计功能实现方法 §§相关问题§§ 1. 如何检查MySQL插件的依赖项? 2. Percona审计插件在MySQL社区版中的安装步骤是什么? 3. 使用通用日志记录登录信息对性能的影响如何? 4. 如何配置MySQL的init_connect来记录用户登录?</think>### 解决MySQL 8安装MariaDB审计插件报错:`undefined symbol PSI_server` #### 问题分析 在MySQL 8中安装MariaDB的`server_audit.so`插件时出现`undefined symbol PSI_server`错误,这是因为: 1. **API不兼容**:MySQL 8重构了性能模式接口(Performance Schema Interface),移除了`PSI_server`等旧符号[^2][^3] 2. **版本差异**:MariaDB插件针对MariaDB环境编译,与MySQL 8的ABI(应用二进制接口)不匹配 3. **符号缺失**:`PSI_server`是MariaDB内部使用的性能监控符号,在MySQL 8中已被替代或移除 --- ### 解决方案(分步操作) #### 方法1:使用兼容的Percona审计插件(推荐) ```bash # 下载Percona审计插件(兼容MySQL 8) wget https://downloads.percona.com/downloads/percona-server/Percona-Server-8.0.31-23/binary/tarball/Percona-Server-8.0.31-23-Linux.x86_64.glibc2.28-minimal.tar.gz # 解压并提取插件 tar -xzf Percona-Server-*.tar.gz find ./ -name audit_log.so # 查找插件路径 cp percona-server/lib/plugin/audit_log.so /usr/lib/mysql/plugin/ # 加载插件 mysql> INSTALL PLUGIN audit_log SONAME 'audit_log.so'; ``` #### 方法2:从源码编译兼容插件 ```bash # 1. 下载MariaDB 10.5源码(最后兼容MySQL的版本) wget https://downloads.mariadb.org/interstitial/mariadb-10.5.23/source/mariadb-10.5.23.tar.gz # 2. 编译审计插件 tar -xzf mariadb-10.5.23.tar.gz cd mariadb-10.5.23/plugin/server_audit cmake . -DMYSQL_INCLUDE_DIR=/usr/include/mysql -DMYSQL_LIB_DIR=/usr/lib/mysql make # 3. 替换符号(关键步骤) perl -i -pe 's/PSI_server/PSI_v1/g' server_audit.c make # 重新编译 # 4. 部署插件 cp server_audit.so /usr/lib/mysql/plugin/ chown mysql:mysql /usr/lib/mysql/plugin/server_audit.so ``` #### 方法3:使用MySQL原生审计功能(无需插件) ```sql -- 启用原生审计(MySQL 8.0.16+) SET GLOBAL audit_log_policy = 'ALL'; SET GLOBAL audit_log_format = 'JSON'; SET GLOBAL audit_log_file = '/var/log/mysql_audit.log'; -- 查看日志 SELECT * FROM mysql.audit_log_filter; SELECT * FROM mysql.audit_log_user; ``` --- ### 关键配置说明 1. **符号替换原理** MySQL 8使用`PSI_v1`替代了`PSI_server`接口[^3],需修改源码: ```c // 修改前 extern PSI_server_service_t PSI_server; // 修改后 extern PSI_server_service_t PSI_v1; ``` 2. **权限要求** ```sql GRANT AUDIT_ADMIN ON *.* TO 'audit_user'@'localhost'; ``` 3. **验证安装** ```sql SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%audit%'; ``` 4. **审计日志示例** ```json { "timestamp": "2023-08-21T09:30:45Z", "user": "admin", "host": "192.168.1.10", "action": "connect", "status": 0 } ``` --- ### 替代方案对比 | 方案 | 兼容性 | 维护难度 | 功能完整性 | |---------------------|--------|----------|------------| | Percona插件 | ★★★★☆ | ★★☆☆☆ | ★★★★★ | | 修改后的MariaDB插件 | ★★★☆☆ | ★★★★★ | ★★★★☆ | | MySQL原生审计 | ★★★★★ | ★☆☆☆☆ | ★★★★☆ | > 📌 **建议**:生产环境优先使用Percona插件或MySQL原生审计,开发环境可尝试源码编译方案[^2][^3]。 --- ### 常见问题排查 1. **加载失败** ```bash # 检查依赖 ldd /usr/lib/mysql/plugin/server_audit.so | grep "not found" ``` 2. **日志不记录** ```sql -- 检查审计状态 SHOW GLOBAL VARIABLES LIKE 'audit%'; ``` 3. **性能影响** 在my.cnf添加限制: ```ini [mysqld] audit_log_rotate_on_size=100M # 限制日志大小 audit_log_exclude_accounts=backup@localhost # 排除特定账号 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值