Skip navigation links
Articles
Simple MySQL Auditing
Add to My Link Library +0 Vote Up -0Vote Down
Simple auditing, i.e., knowing what changed recently, can save you tons of time while troubleshooting.I know that, in the ideal world:\tEverything is supposed to be done through configuration management.\tEverything is documented and all changes are tracked through a VCS.\tEvery DDL or set global is trapped via MySQL Proxy and logged.But there are always ways to bypass the gatekeepers. Changes can go in unnoticed. An hour or so later, your database performance suddenly changes for the worse, and you get that phone call.First you check if anything caused an actual error. You look around at a few log files and nothing shows up. The next thing you ask yourself is, did someone change anything in the last little while. Of course, everybody says no. After a few hours of digging, comparing schemas, diff-ing old and current config files, you actually find what has changed, put it back the way it was and everything is back to normal. You ask the question again, did anybody change the global variable from X to Y, and someone finally admits it. But they thought that it was not relevant since they did that change on the weekend and the system performance didn't go down the toilet until Monday morning around 9:00am. Sound familiar? We have all gone through this many times, and it doesn't matter what process you have in place -- something always slips through the cracks.I came up with some simple stored procedure that will compare two data sets and keep track of the changes historically. It's loosely based on slowly-changing dimension type 2 in the data warehouse world (google Ralph Kimball if you want to get all the gory details). This method tracks only changes, so you should be able to keep historical rows forever, unless you are constantly dropping and creating whatever you are tracking.Say you want to keep track of "my.cnf" or global variables changes. This one of the simplest tables to track, the "information_schema" -- it has only two columns. Others, such as tables or routines, would make the SQL to compare a little more involved.First you need a table to track this with. I called mine "historical_global_variable". It has the two columns from the original table plus three additional ones:


Report this link: