简体   繁体   English

如何监视MySQL数据库

[英]How To Monitor A MySQL Database

I have a small application that connects to a MySQL database. 我有一个连接到MySQL数据库的小型应用程序。 It is very straight forward - basically the database contains alert entries (so as alerts happen, a new record gets written to this database table). 这非常简单-基本上数据库包含警报条目(因此,发生警报时,新记录将写入此数据库表)。 The application has a Datagridview that is bound to this table to populate visually all of the alerts. 该应用程序具有绑定到此表的Datagridview,以可视方式填充所有警报。

However, what I want to achieve is to somehow 'watch' this MySQL table and auto populate the datagridview when new entries are made into this table (or changes). 但是,我要实现的是以某种方式“监视”该MySQL表,并在该表中有新条目(或更改)时自动填充datagridview。 I also want to be able to notify the user that the datagridview has been updated. 我还希望能够通知用户datagridview已更新。

I have populated my datagridview with the MySQL table, but was after some help in setting this up to be an automatic watcher of the table. 我已经用MySQL表填充了datagridview,但是在将其设置为表的自动监视程序后得到了一些帮助。 I can set everything up in a background worker, but not sure how to find out when data has changed. 我可以在后台工作程序中设置所有内容,但不确定如何确定何时更改了数据。

Thanks 谢谢

I'm not familiar with MySQL, but you will probably end up having to poll the database to do this. 我对MySQL不熟悉,但是您可能最终不得不轮询数据库才能做到这一点。 You'll probably end up with some timer based code that will go check the database at a particular interval. 您可能最终会得到一些基于计时器的代码,这些代码将按特定的时间间隔检查数据库。

However, you can do your best to minimize the amount of work done on each check. 但是,您可以最大程度地减少每次检查的工作量。 A bad way to do this would be to retrieve the whole table each time and run a whole bunch of checks and comparisons in the program. 一种不好的方法是每次都检索整个表并在程序中运行一堆检查和比较。

Instead, you could use triggers (and here's the syntax to create one) to know when a row was updated or inserted. 相反,您可以使用触发器 (这是创建触发器语法 )来知道何时更新或插入行。 How you use the triggers is up to you. 您如何使用触发器取决于您。

One alternative would be to have a timestamp column on the table that automatically updates whenever there's an insert or update. 一种替代方法是在表上有一个时间戳列,该列会在有插入或更新时自动更新。 Similarly you could have a separate table with just the primary key and the same timestamp column (if you don't want to clutter up your main table with extra columns). 同样,您可以有一个仅包含主键和相同timestamp列的单独表(如果您不想用多余的列来使主表弄乱)。

Either way, you would keep track of the timestamp you last checked at, and find everything that was inserted or updated after that point in time. 无论哪种方式,您都将跟踪上次检查的时间戳,并找到在该时间点之后插入或更新的所有内容。

Perhaps add Insert, Update, Delete triggers to the table to log row in a new table with two columns.. 也许在表中添加“插入”,“更新”,“删除”触发器以将新行记录在具有两列的新表中。

  1. uid of row changed 行的uid已更改
  2. type of change insert/update/delete 更改类型插入/更新/删除

Then you access this table and it tells you the specifically which rows were inserted, updated, deleted 然后访问此表,它会告诉您具体插入,更新,删除了哪些行

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

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