使用MySQL记录所有表上的所有事件 [英] Log all events on all tables with MySQL

查看:526
本文介绍了使用MySQL记录所有表上的所有事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表database_log(id,user,timestamp,tablename,old_value,new_value)中的所有表上记录所有事件。

I need to log all events on all tables in table database_log (id, user, timestamp, tablename, old_value, new_value).

我以为我可以创建所有表(〜25)上的相同触发器与一点PHP脚本动态替换名称的表。但是在这种情况下,我可以检索旧的和新的值,因为所有的表都没有相同的列,所以我不能将所有字段连接到old_value和new_value中的存储(即使我在模式中检索字段因为我不能使用一个concat()来选择所有的值并存储在变量中)。

I thought I can create the same trigger on all tables (~25) with a little php script dynamically replace the name's table. But in this case I can retrieve the old and new value, because all tables haven't the same columns so I can't just concat all field for store in the "old_value" and "new_value" (even if I retrieve fields in schema because I can't use a concat() on it for select all value and store in variable).

例如:

 SELECT * into v_myvar FROM my_table where id=OLD.id;
 CALL addLog(v_myvar)

其中addLog是使用旧值并添加行的过程与其他信息,可以节省我的生命。

Where addLog is procedure taking my old value and add a line with other informations, could save my life.

所以,我正在寻找一个性感的解决方案,一个触发器和/或一个过程(通过表)或一个有用的工具。有人有解决方案?

So, I'm looking for a sexy solution with one trigger and/or one procedure (by table) or a useful tool. Someone have a solution ?

谢谢

推荐答案

SET GLOBAL general_log_file = '/var/log/mysql/mysql.log'; 

一般查询日志是mysqld正在做的一般记录。当客户端连接或断开连接时,服务器将信息写入此日志,并记录从客户端收到的每个SQL语句。

The general query log is a general record of what mysqld is doing. The server writes information to this log when clients connect or disconnect, and it logs each SQL statement received from clients.

请参阅MySql 文档

这篇关于使用MySQL记录所有表上的所有事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆