MySQL多服务器任务回退机制
我有两个网络服务器。服务器“A”和服务器“B”。两者完全相同,并连接到负载均衡器以服务客户端。
我有一个每天每小时执行的任务(cronjob)。此任务当前仅在服务器“A”上运行。问题是这个任务非常重要,如果服务器“A”由于某种原因崩溃,该任务将不再运行。
我希望服务器“B”执行该任务,但前提是服务器“A”不执行。 这两个任务都是与 MySQL 数据库交互的 PHP 脚本。
我正在考虑使用表中的一些行(使用 MySQL 锁?)作为服务器“B”的标志,以便在服务器“A”没有执行任务时启动他的任务,反之亦然。
请注意,两台服务器上的任务文件必须完全相同。
谢谢。
I have two web servers. Server 'A' and server 'B'. Both are exact duplicate of the other and are connected to a load-balancer for serving clients.
I have a tasks (cronjob) that is executed at every hour, every day. This task is currently running only on server 'A'. The problem is that this tasks is really important and if server 'A' crash for some reason the task will no longer run.
I would like server 'B' to execute the task but only if server 'A' didn't.
Both tasks are PHP scripts interacting with a MySQL database.
I'm thinking about using some rows in a table (using MySQL locks?) as a flag for the server 'B' to start his task if server 'A' didn't do it and vice versa.
Please note that the task file on both servers must be exactly the same.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提出的解决方案是我经常用于更简单任务的解决方案,通常采用类似以下内容:
UPDATE jobs SET process = '$token' WHERE process IS NULL
的表(+一些其他子句现在还没有说到点上)。SELECT * FROM jobs WHERE process = '$token'
需要记住的是,这只适用于 MySQL 实例启动时的作业:没有它就无法进行任何处理。话又说回来,可以在两台服务器上执行检查 MySQL 服务器是否处于活动状态的 cronjob,我不介意为这样的重大灾难收到 2 个警报:)
Your proposed solution is one I often employ for simpler tasks, usually with something like:
UPDATE jobs SET process = '$token' WHERE process IS NULL
(+ some other clauses not to the point right now).SELECT * FROM jobs WHERE process = '$token'
Something to keep in mind though that this only works for jobs as long as the MySQL instance is up: no processing can be done without it. Then again, a cronjob which checks whether the MySQL server is alive can be performed on both servers, I don't mind to get 2 alerts for such a major catastrophe :)