Haabda
Haabda

Reputation: 1433

How should I audit changes in a MySQL table (using MySQL 4)?

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.

Upvotes: 6

Views: 6315

Answers (4)

Jason DeFontes
Jason DeFontes

Reputation: 2285

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.

Upvotes: 2

Keith Lawrence
Keith Lawrence

Reputation: 311

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

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

Upvotes: 0

Matt
Matt

Reputation: 1370

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.

Upvotes: 3

Ed Guiness
Ed Guiness

Reputation: 35267

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

Upvotes: 3

Related Questions