views:

125

answers:

3

I have a mysql trigger that logs every time a specific table is updated.

Is there a way to also log WHICH PHP SCRIPT triggered it? (without modifying each php script of course, that would defeat my purpose)

Also, is there a way to log what was the SQL statement right before the UPDATE that triggered it?

Thanks

Nathan

+1  A: 

Short answers: no and no. Sorry.

What are you trying to achieve? Perhaps there's another way....

Roland Bouman
I'm trying to investigate an ongoing sql injection attack. We have a LOT of code across a LOT of files, using different programming languages. I was hoping to get hints as to where to look...
nute
I assume this is a web application. I'd try to scan the web access log, that way you should have all the information that would enable you to track down the problem.You can also send me private email - [email protected] hacked a few sites in my day (white hat) and perhaps I can find th e problem that way.
Roland Bouman
A: 

no, but you can get some more specific direction.

first, if you're using persitent connections, turn them off. this will make your logs easier to use.

second, since it sounds like you have multiple code bases accessing the same database, create a different user for each code base with exactly the same rights and make each code base log in with a different user. now when you look at the log, you can see which application is doing what.

third, if you have the query log on, then the UPDATE immediately preceding the trigger will be the UPDATE that caused the trigger.

fourth, if your apps use any sort of encapsulation for the mysql connection, it should be trivial to modify it to write the call stack at the time a query is sent to the database to a file.

longneck
A: 

I've read through a few of the answers and the comments. I had one idea that would be usefuls only if your queries are passing through a single point. For example, if you have a database class that all queries are executed through.

If that is the case, you could possibly add a comment to the query itself. The comment would include the function call trace, and would be added to the query as an SQL comment.

Next, you would turn query logging on and be able to see where each query is getting called from in the log file.

If your queries do not pass through a single point, you may be out of luck.

One final suggestion would be to take a look at MySQL Proxy. I have not used it much but it is designed to do intermediate processing of queries. However, I still think you would need to modify your PHP scripts to pass additional information.

jonstjohn
Thanks, unfortunately we do not use a single class for dealing with sql. Our old codebase is basically the most basic, mysql_query() calls whenever needed.
nute