Tracing MySql Transactions
Anyone who has had to troubleshoot or support systems built on a SQL Server database knows and loves the Profiler Tool, which allows you to see all of the queries submitted to a database.
Web page database interaction tends to be simpler, and until recently, I never had to troubleshoot a situation that was so complex that I needed to trace its database interactions. This week, though, I was looking at a function to save some records. I had a print_r at the top to prove to myself that the records were there, and I saw other records in the list in the database after the run, but my last ones were missing.
It had to be that the records were being deleted behind the insert. I needed a trace to figure out what was coming behind me to delete perfectly good records.
It turns out to be pretty easy. First, I edited the configuration file for mysql, which I found in /etc/mysql/my.cnf. (All of my work was on an Ubuntu server running both Apache and MySql. Needless to say, there are other environments where paths and file names are different.)
I found this section of the config file:
1 2 3 4 5 6 7 8 9 |
# * Logging and Replication # # Both location gets rotated by the cronjob. # Be aware that this log type is a performance killer. # As of 5.1 you can enable the log at runtime! general_log_file = /var/log/mysql/mysql.log general_log = 0 |
I changed the general_log value from 0 to 1, and restarted mysql.
1 2 3 |
sudo service mysql restart |
I then ran my page, and opened the log file named in the configuration: /var/log/mysql/mysql.log.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
130520 23:04:28 121 Connect root@localhost on 121 Init DB assessment_dev2 121 Query select DisplayFlag from zzqb_Assessments where AID = 17 121 Query select RequiresOrder from zzqb_Assessments where AID = 17 121 Query SELECT * FROM zzqb_Assessments where AID=17 121 Query INSERT INTO zzqb_AsAnswers (AID, AIID, AQID, ABonusID, Value) VALUES (17,31,3, 0, "") 121 Query select q.CorrespondingDemographicFieldID, d.Name from zzqb_AsQuestions q join zzqb_LuDemographicFields d on q.CorrespondingDemographicFieldID = d.DID where q.AID = 17 and q.AQID = 3 121 Query delete s.* FROM zzqb_AsSkips s inner join zzqb_AsQuSkipLogic sl on s.AID = sl.AID and s.AQID = sl.AQID where sl.DependentOnAQID = 3 and s.AID = 17 and s.AIID = 31 121 Query select sl.AQSLID, sl.AQID as futureAQID, q.QTypeID, sl.OutputConditionID, sl.Variable1, sl.Variable2, (SELECT count(1) FROM zzqb_AsQuSkipLogic WHERE AID = sl.AID AND AQID = sl.AQID) AS numberOfTests from zzqb_AsQuSkipLogic sl inner join zzqb_AsQuestions q on sl.AID = q.AID and sl.AQID = q.AQID where sl.AID = 17 and DependentOnAQID = 3 order by sl.AQID 121 Query delete from zzqb_AsAnswers where aid=17 and aiid=31 and aqid=4 121 Query INSERT INTO zzqb_AsAnswers (AID, AIID, AQID, ABonusID, AOID) VALUES (17,31,4, 0, 1) 121 Query select q.CorrespondingDemographicFieldID, d.Name from zzqb_AsQuestions q join zzqb_LuDemographicFields d on q.CorrespondingDemographicFieldID = d.DID where q.AID = 17 and q.AQID = 4 121 Query delete s.* FROM zzqb_AsSkips s inner join zzqb_AsQuSkipLogic sl on s.AID = sl.AID and s.AQID = sl.AQID where sl.DependentOnAQID = 4 and s.AID = 17 and s.AIID = 31 121 Query select sl.AQSLID, sl.AQID as futureAQID, q.QTypeID, sl.OutputConditionID, sl.Variable1, sl.Variable2, (SELECT count(1) FROM zzqb_AsQuSkipLogic WHERE AID = sl.AID AND AQID = sl.AQID) AS numberOfTests from zzqb_AsQuSkipLogic sl inner join zzqb_AsQuestions q on sl.AID = q.AID and sl.AQID = q.AQID where sl.AID = 17 and DependentOnAQID = 4 order by sl.AQID 121 Query delete from zzqb_AsAnswers where aid=17 and aiid=31 and aqid=5 121 Query INSERT INTO zzqb_AsAnswers (AID, AIID, AQID, ABonusID, AOID) VALUES (17,31,5, 0, 2) 121 Query select q.CorrespondingDemographicFieldID, d.Name from zzqb_AsQuestions q join zzqb_LuDemographicFields d on q.CorrespondingDemographicFieldID = d.DID where q.AID = 17 and q.AQID = 5 121 Query delete s.* FROM zzqb_AsSkips s inner join zzqb_AsQuSkipLogic sl on s.AID = sl.AID and s.AQID = sl.AQID where sl.DependentOnAQID = 5 and s.AID = 17 and s.AIID = 31 121 Query select sl.AQSLID, sl.AQID as futureAQID, q.QTypeID, sl.OutputConditionID, sl.Variable1, sl.Variable2, (SELECT count(1) FROM zzqb_AsQuSkipLogic WHERE AID = sl.AID AND AQID = sl.AQID) AS numberOfTests from zzqb_AsQuSkipLogic sl inner join zzqb_AsQuestions q on sl.AID = q.AID and sl.AQID = q.AQID where sl.AID = 17 and DependentOnAQID = 5 order by sl.AQID 121 Query delete from zzqb_AsAnswers where aid=17 and aiid=31 and aqid=6 121 Query INSERT INTO zzqb_AsAnswers (AID, AIID, AQID, ABonusID, AOID) VALUES (17,31,6, 0, 3) 121 Query select q.CorrespondingDemographicFieldID, d.Name from zzqb_AsQuestions q join zzqb_LuDemographicFields d on q.CorrespondingDemographicFieldID = d.DID where q.AID = 17 and q.AQID = 6 121 Query delete s.* FROM zzqb_AsSkips s inner join zzqb_AsQuSkipLogic sl on s.AID = sl.AID and s.AQID = sl.AQID where sl.DependentOnAQID = 6 and s.AID = 17 and s.AIID = 31 121 Query select sl.AQSLID, sl.AQID as futureAQID, q.QTypeID, sl.OutputConditionID, sl.Variable1, sl.Variable2, (SELECT count(1) FROM zzqb_AsQuSkipLogic WHERE AID = sl.AID AND AQID = sl.AQID) AS numberOfTests from zzqb_AsQuSkipLogic sl inner join zzqb_AsQuestions q on sl.AID = q.AID and sl.AQID = q.AQID where sl.AID = 17 and DependentOnAQID = 6 order by sl.AQID 121 Query select q.AQID, q.QTypeID from zzqb_AsQuestions q where AID = 17 and IsActive = 1 and AQID not in (select AQID from zzqb_AsSkips where AID = 17 and AIID = 31) order by OrderNum limit 0, 5 121 Query select AQOID from zzqb_AsQuOptions where AID = 17 and AQID = 1 121 Query delete from zzqb_AsAnswers where aid=17 and aiid=31 and aqid=1 121 Query delete s.* FROM zzqb_AsSkips s inner join zzqb_AsQuSkipLogic sl on s.AID = sl.AID and s.AQID = sl.AQID where sl.DependentOnAQID = 1 and s.AID = 17 and s.AIID = 31 121 Query select sl.AQSLID, sl.AQID as futureAQID, q.QTypeID, sl.OutputConditionID, sl.Variable1, sl.Variable2, (SELECT count(1) FROM zzqb_AsQuSkipLogic WHERE AID = sl.AID AND AQID = sl.AQID) AS numberOfTests from zzqb_AsQuSkipLogic sl inner join zzqb_AsQuestions q on sl.AID = q.AID and sl.AQID = q.AQID where sl.AID = 17 and DependentOnAQID = 1 order by sl.AQID |