简体   繁体   English

获得MySQL数据库的差异

[英]Getting a MySQL database difference

I have a mysql database. 我有一个mysql数据库。 What I'd like to do is perform an arbitrary action on it, and then figure out what changed. 我想做的是对它执行任意操作,然后找出发生了什么变化。 Something like this: 像这样的东西:

//assume connection to db already established

before();//saves db state
perform_action();//does stuff to db
diff();//prints what happened

I'd want it to output something like: 我希望它输出如下内容:

Row added in table_0 ]details]
Row added in table_1 [details]
Row modified in table_5 [details]
Row deleted in table_2 [details]

Any ideas? 有任何想法吗?


To further clarify: You know how on stackoverflow, if you check a post's edits, you can see red lines/green highlights indicating what's been changed? 为了进一步澄清:你知道如何在stackoverflow上,如果你检查帖子的编辑,你可以看到红线/绿色突出显示已经改变了什么? I want something like that, but for mysql databases. 我想要这样的东西,但对于mysql数据库。

Instead of copying your whole database in order to save the state for a later diff, you might be better off by using triggers: 您可能最好使用触发器,而不是复制整个数据库以保存稍后差异的状态:

http://dev.mysql.com/doc/refman/5.0/en/triggers.html http://dev.mysql.com/doc/refman/5.0/en/triggers.html

When you setup appropriate triggers, you can log changes to a table - for example, you can setup a trigger that automatically logs the old values and the new values for every update. 设置适当的触发器时,可以将更改记录到表中 - 例如,您可以设置触发器,以自动记录每个更新的旧值和新值。 To see the changes, query the table that was filled by the trigger. 要查看更改,请查询由触发器填充的表。

Of course, the trigger is not restricted to changes made by your application, it will also log updates done by other applications. 当然,触发不限于您的应用程序所做的更改,这也将登录其他应用程序进行更新。 But this is also the case if you diff the old version of the database with the new version of the database. 但是,如果您使用新版本的数据库区分旧版本的数据库,情况也是如此。

I think normally your application would log any interesting changes as it makes them. 我认为通常你的应用程序会记录任何有趣的变化。 Or you would set up history tables for everything with datetimes. 或者您可以使用日期时间为所有内容设置历史记录表。

To do it the way you describe, you could dump the contents of the database into a file before and after your action and do a diff on the two files. 要按照您描述的方式执行此操作,您可以在操作之前和之后将数据库的内容转储到文件中,并对这两个文件执行diff操作。 In php, you can check out xdiff: http://us.php.net/manual/en/book.xdiff.php 在php中,你可以看看xdiff: http ://us.php.net/manual/en/book.xdiff.php

If this is something you're doing only occasionally in controlled circumstances to test some queries you're not sure about, you can dump and diff on the command line. 如果您只是偶尔在受控环境中进行测试某些您不确定的查询,则可以在命令行上进行转储和差异。

One way is to parse the log files, which will give you exact SQL statements executed in your database. 一种方法是解析日志文件,这将为您提供在数据库中执行的确切SQL语句。 I'm not exactly sure how to separate SQL statements made by your application from other applications (if thats the case) 我不确定如何将应用程序生成的SQL语句与其他应用程序分开(如果是这样)

The only thing I can think of is to do some combination of a few somewhat hackey things: 我唯一能想到的就是做一些有点hackey事情的组合:

  1. Save a [temporary?] table of row IDs, to check for new rows. 保存[临时?]行ID表,以检查新行。 If you need to know what was in deleted or modified rows before, you'll need to copy the whole DB, which would be rather messy. 如果您之前需要知道删除或修改过的行中的内容,则需要复制整个数据库,这样会非常混乱。

  2. Have each row have a datestamp that gets modified on update; 每行都有一个在更新时修改的日期戳; grab rows for whom the updated datestamp is newer than when the analysis started. 获取更新后的日期戳比分析开始时更新的行。

  3. Have a layer between your application and the database (if you have something like the classic $db->query() , it would make this easy), log queries sent, which can then be looked at. 在您的应用程序和数据库之间有一个层(如果您有类似经典的$db->query() ,它会使这很简单),发送日志查询,然后可以查看。

I suppose the real question is if you want to know what queries are being executed against the DB, or if you want to know what they queries you're running are actually doing. 我想真正的问题是,如果你想知道对数据库执行了什么查询,或者你想知道他们正在运行的查询实际上在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM