Audit Trail in Mysql

This script is based in the code published in the bolg A little noise http://thenoyes.com/littlenoise/?p=43, it adds some functionality:

  1. Adds triggers for insert and delete writing all the values inserted / deleted to the auditlog table
  2. It takes the primary key from information schema instead of grabbing it as  a parameter
  3. Stores the primary key as a varchar with the description (pkdesk saves the fields, and pkvalue the value of the primary key)
  4. Added the mysql username
  5. The tablename works with a like clause so if you send ‘%’ as table name you will get the script for all the tables in the current database.

Considerations:

  • If the script file already exists you will get an error
  • Tables that doesn’t have a primary key won’t script out.
  • If you change something in the script, don’t forget to comment out the table drop/creation part so you wont loose your previous data.

Hope this come handy for everyone, it took me some time to write it down, so I would like to share it for everyone.

Finally, the script: Download

Posted in Mysql | 19 Comments