Is there a way to watch a mysql database for changes using perl?

我们两清 提交于 2019-11-30 04:03:43

问题


I'm looking for a solution similar to the inotify method of watching files for changes. I'm aware that I could watch the binlog file of the mysql database and run queries to pick out the new results but that seems very inefficient and inelegant; as does simply doing masses of queries in a loop waiting for new results.


回答1:


If you add a TRIGGER to the table(s) you're interested in, you can use that to alert the watching application. You could do that in a number of ways:

  1. Create an audit table in the database, and have the trigger write the relevant info there; and have your watching application poll the audit table for new entries. You're still polling, but in a controlled way which won't hit the server too hard.
  2. Have the trigger call an external app through a UDF.



回答2:


As far as MyISAM tables go you can watch information_schema.TABLES.UPDATE_TIME. That would save you from polling all tables you're interested in. For InnoDB, watching binlog is the best I can think of.




回答3:


Another approach is to do a push/signal instead of a DB poll. Have whatever process updates the database, notify your Perl code that an update was done via any IPC you pick (heck, a log file being appended to with name of table being changed might do the trick well enough).

This is especially effective if the updates a fairly rare/low volume yet the reaction time to them must be quick.

An additional benefit is portability - works for any MySQL backend or for any other DB engine.



来源:https://stackoverflow.com/questions/4418598/is-there-a-way-to-watch-a-mysql-database-for-changes-using-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!