6

I have been asked to audit any/all changes in a MySQL table. Does anyone know of any tools out there to help me do this or do I need to write my own solution?

If I write my own audting, my initial thought it to make a separate table and build a string of changes in the PHP code. Something like "fieldname1 -> oldvalue | fieldname2 -> oldvalue, ...". If you see a major problem with this method, please let me know.

Haabda
  • 1,413
  • 2
  • 13
  • 17

4 Answers4

3

Use a trigger to detect changes and to write before/after values to a log table.

Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
  • Sadly, I am stuck using MySQL 4. I'll edit my question for clarification. Great suggestion though. – Haabda Oct 20 '08 at 15:29
3

The only sure-fire way to capture all changes to a DB table is to use triggers on the Server. The risk of modifing your own code to audit the changes is that changes from another application/user etc will not be captured.

Having said that, I'm not sure that MySQL 4 had trigger support.

Matt
  • 1,370
  • 2
  • 16
  • 40
  • I guess I'm just going to have to implement my back-asswards solution. Maybe I can get permission to update to MySQL 5. – Haabda Oct 20 '08 at 15:36
  • Triggers were added in MySQL 5.0. At this point there really isn't a good reason not to upgrade from 4 to 5. 5.1 is in release candidate, and 6.0 is in Alpha development. – acrosman Oct 20 '08 at 21:29
  • can triggers track which user is making the changes? – andho Oct 25 '11 at 08:03
2

If you wind up hand-rolling a solution due lack of trigger support, I strongly recommend that you don't just dump the changes in a string blob. One day you will be asked to query that data and you will wind up having to do a bunch of string parsing to get your data back out. (I speak from experience here.)

The simplest approach is just to create a shadow audit table that has all of the same columns as the original table, plus a change date column and a sequential id. Then you have the entire history at hand to reconstruct in whatever format you need, and you can query it at will.

Jason DeFontes
  • 2,235
  • 15
  • 14
0

SoftTree DB Audit has MySQL support, might do what you're after:

http://www.softtreetech.com/idbaudit.htm

Keith Lawrence
  • 301
  • 2
  • 6