Lab 13: Audit Logging

Time: 45 minutes | Level: Advanced | DB: MySQL 8.0, PostgreSQL 15

Overview

Audit logging records who did what and when — essential for compliance (PCI-DSS, HIPAA, SOC 2), security incident investigation, and access monitoring. This lab covers MySQL's audit log plugin and PostgreSQL's pgaudit extension.


Step 1: MySQL — Enable Audit Log Plugin

docker run -d --name mysql-lab \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  mysql:8.0

for i in $(seq 1 30); do docker exec mysql-lab mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2; done

docker exec mysql-lab mysql -uroot -prootpass <<'EOF'
-- Install the audit log plugin
INSTALL PLUGIN audit_log SONAME 'audit_log.so';

-- Configure audit logging
SET GLOBAL audit_log_policy = 'ALL';           -- Log everything
SET GLOBAL audit_log_format = 'JSON';          -- JSON format for easy parsing
SET GLOBAL audit_log_rotate_on_size = 104857600; -- Rotate at 100MB

-- Verify plugin is active
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM information_schema.PLUGINS 
WHERE PLUGIN_NAME = 'audit_log';

-- Check current audit settings
SHOW VARIABLES LIKE 'audit_log%';
EOF

📸 Verified Output:

💡 audit_log_policy options: ALL (log everything), LOGINS (connection events only), QUERIES (SQL statements only), NONE (disable). Start with LOGINS in production, add QUERIES for sensitive tables.


Step 2: Generate Audit Events

📸 Verified Output:


Step 3: Read and Parse MySQL Audit Log

📸 Verified Output:


Step 4: Filter Audit Log by Policy


Step 5: PostgreSQL pgaudit Setup

📸 Verified Output:

💡 pgaudit log categories: ddl (CREATE/ALTER/DROP), write (INSERT/UPDATE/DELETE), read (SELECT), role (GRANT/REVOKE/CREATE USER), function (function calls), misc (FETCH/COPY).


Step 6: Generate Audit Events in PostgreSQL


Step 7: Read and Parse PostgreSQL Audit Log

📸 Verified Output:

💡 pgaudit log format: AUDIT: SESSION,statement_id,substatement_id,class,command,object_type,object_name,statement


Step 8: Capstone — Audit Compliance Report

📸 Verified Output:


Summary

Feature
MySQL
PostgreSQL
Log Level

Plugin

audit_log.so

pgaudit

System

Policy

audit_log_policy=ALL/LOGINS/QUERIES

pgaudit.log=ddl,write,role,read

Configurable

Format

JSON, XML, CSV

PostgreSQL log format

Depends on log_line_prefix

Connections

Logged with status code

log_connections=on

Connection-level

DDL

Included in QUERIES/ALL

pgaudit.log=ddl

Object-level

Passwords

Redacted automatically

Redacted in pgaudit

Auto-masked

Application audit

N/A

Trigger-based audit_log table

Row-level

Key Takeaways

  • pgaudit is the standard PostgreSQL audit extension — install it on all production servers

  • MySQL audit_log plugin is built-in (Community Edition has JSON format, Enterprise has filtering)

  • log_connections + log_disconnections = baseline for session auditing in PostgreSQL

  • Trigger-based audit tables supplement system logs — capture before/after values

  • Compliance requirements: PCI-DSS requires 12 months of audit log retention; HIPAA requires 6 years

Last updated