MySQL 数据库审计跟踪触发器

发布于 2024-08-31 18:47:25 字数 1726 浏览 2 评论 0原文

我需要跟踪 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-09-07 18:47:25

我想您会发现这个存储过程不会创建触发器;它创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文