没有新数据库记录的每日警报

发布于 2025-01-08 14:18:03 字数 153 浏览 0 评论 0原文

如果在某一时间之前没有新记录插入 MySQL 数据库,如何设置要发送的电子邮件警报?

我有一个简单的表,由 id 和日期时间字段组成。理想情况下,如果每天上午 10:00 之前没有记录插入数据库,我希望收到电子邮件提醒。这到底可行吗?

谢谢。

How would one set up an email alert to be sent, if there are no new records inserted into a MySQL database by a certain time?

I have a simple table consisting of an id and a datetime field. Ideally I would like to receive an email alert if no records are inserted to the database by 10:00am every day. Is this at all feasible?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

朱染 2025-01-15 14:18:03

您需要通过带有 datetime 字段的 SQL 查询设置一个 cron 作业,以获取 1 天间隔的记录。

如果没有记录(count=0),那么您需要执行发送邮件警报的代码。

查询如下:

SELECT  count(id)
FROM    mytable
WHERE   datetime_field_date BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()

请进行所需的更改。

You seed to set up a cron job by sql query with datetime field for gettng records of 1 day interval.

It no record (count=0) then you need to execute code for sending mail alert.

Query wil be like below:

SELECT  count(id)
FROM    mytable
WHERE   datetime_field_date BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()

Please do required changes.

尘曦 2025-01-15 14:18:03

假设您希望将其完全保留在数据库中,则需要创建一个每天运行一次的 EVENT(例如 10:00:01)。 了解更多

此事件将执行查询以检查自上次检查以来是否已收到任何记录。 Teez 提出的查询之类的东西就足够了。

但是,您可能希望在此添加更多智能,以允许中断。如果数据库在 10.00.01 关闭并且查询最终在 10:05 运行,您不希望在 10:04 收到的消息破坏结果(或者您会吗?取决于业务规则 - 也许该记录应该已在 09:59 插入,但数据库已关闭...)

无论如何,如果查询计数为零,那么您想要发送警报。有一个名为 MySQL Messages 的项目提供了 API 来执行此操作。 查看一下

由于这里有几个调用,您需要使用 BEGINEND 来结束语句。

如果您确实沿着这条路线走,请确保事件调度程序已打开。 了解更多

Assuming you want to keep this entirely within the database, you need to create an EVENT which runs once a day (say 10:00:01). Find out more.

This event would execute a query to check whether any records have been received since the last time you checked. Something like the query proposed by Teez would suffice.

However, you might want to add a little more smarts into this , to allow for outages. If the database is down at 10.00.01 and the query ends up running at 10:05 you wouldn't want a message received at 10:04 to corrupt the result (or would you? depends on the business rules - perhaps that record should have been inserted at 09:59 but the database was down...)

Anyway, if the query count is zero then you want to send an alert. There is a project named MySQL Messages which provides APIs to do this. Check it out.

As there are a couple of calls here you'll need to bookend the statements with BEGIN and END.

If you do go down this route, make sure the event scheduler is switched on. Find out more.

千笙结 2025-01-15 14:18:03

您可以设置一个 cron 作业,

例如

 0 10 * * * Your job over here

0 分钟

10 小时,

这样每天上午 10 点 cron 就会执行您的作业。

如需更多参考,请访问此网站 http://www.adminschoice.com/crontab-quick-reference< /a>

You can set up a cron job

For ex

 0 10 * * * Your job over here

0 - minute

10 - hour

so Every day 10 AM cron will execute you job.

For More reference go through this site http://www.adminschoice.com/crontab-quick-reference

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