MySQL 数据库审计跟踪触发器
我需要跟踪 MySql Db 中某些表的更改(审计跟踪)。我正在尝试实施此处建议的解决方案。
我有一个包含以下列的 AuditLog 表:AuditLogID、TableName、RowPK、FieldName、OldValue、NewValue、TimeStamp。
mysql 存储过程如下(执行正常,并创建过程):
对过程的调用,例如: CALL addLogTrigger('ProductTypes', 'ProductTypeID'); 执行,但不创建任何触发器(见图)。 SHOW TRIGGERS 返回空集。
请让我知道可能出现的问题,或者实现此目的的替代方法。
DROP PROCEDURE IF EXISTS addLogTrigger;
DELIMITER $
CREATE PROCEDURE addLogTrigger(IN tableName VARCHAR(255), IN pkField VARCHAR(255))
BEGIN
SELECT CONCAT(
'DELIMITER $\n', 'CREATE TRIGGER ', tableName, '_AU AFTER UPDATE ON ', tableName, ' FOR EACH ROW BEGIN ',
GROUP_CONCAT(
CONCAT(
'IF NOT( OLD.', column_name, ' <=> NEW.', column_name, ') THEN INSERT INTO AuditLog (',
'TableName, ',
'RowPK, ',
'FieldName, ',
'OldValue, ',
'NewValue'
') VALUES ( ''',
table_name, ''', NEW.',
pkField, ', ''',
column_name, ''', OLD.',
column_name, ', NEW.',
column_name,
'); END IF;'
)
SEPARATOR ' '
), ' END;$'
)
FROM
information_schema.columns
WHERE
table_schema = database()
AND table_name = tableName;
END$
DELIMITER ;
替代文本 http://pssnet.com/~devone/pssops3/testing/callprocedure。 .png
I need to track changes (audit trail) in certain tables in a MySql Db. I am trying to implement the solution suggested here.
I have an AuditLog Table with the following columns: AuditLogID, TableName, RowPK, FieldName, OldValue, NewValue, TimeStamp.
The mysql stored procedure is the following (this executes fine, and creates the procedure):
The call to the procedure such as: CALL addLogTrigger('ProductTypes', 'ProductTypeID');
executes, but does not create any triggers (see the image). SHOW TRIGGERS returns empty set.
Please let me know what could be the issue, or an alternate way to implement this.
DROP PROCEDURE IF EXISTS addLogTrigger;
DELIMITER $
CREATE PROCEDURE addLogTrigger(IN tableName VARCHAR(255), IN pkField VARCHAR(255))
BEGIN
SELECT CONCAT(
'DELIMITER $\n', 'CREATE TRIGGER ', tableName, '_AU AFTER UPDATE ON ', tableName, ' FOR EACH ROW BEGIN ',
GROUP_CONCAT(
CONCAT(
'IF NOT( OLD.', column_name, ' <=> NEW.', column_name, ') THEN INSERT INTO AuditLog (',
'TableName, ',
'RowPK, ',
'FieldName, ',
'OldValue, ',
'NewValue'
') VALUES ( ''',
table_name, ''', NEW.',
pkField, ', ''',
column_name, ''', OLD.',
column_name, ', NEW.',
column_name,
'); END IF;'
)
SEPARATOR ' '
), ' END;
alt text http://pssnet.com/~devone/pssops3/testing/callprocedure.png
)
FROM
information_schema.columns
WHERE
table_schema = database()
AND table_name = tableName;
END$
DELIMITER ;
alt text http://pssnet.com/~devone/pssops3/testing/callprocedure.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您会发现这个存储过程不会创建触发器;它创建 SQL 语句来创建触发器。将此过程的输出喷射到某个文件中,然后运行它。
查看输出,其中似乎存在一些虚假的竖线字符,可能会带来麻烦;我的视力不太好,所以我不能确定。
I think you'll find this stored procedure doesn't create the triggers; it creates SQL statements to create the triggers. Squirt the output from this procedure into a file somewhere and then run it.
Looking at the output there appear to be some spurious vertical bar characters in it that could give trouble; my eyesight isn't what it might be so I can't be sure.