从 mysql 发送终端/shell 命令到终端并在循环光标时检索答案

发布于 2024-12-27 18:59:57 字数 195 浏览 6 评论 0原文

我在 macOS 上使用 php 和 MySQL。 我想从数据库中选择大量电子邮件,并使用终端/shell 中的 dig 命令对我选择的每封电子邮件执行 dns 查找,例如: "dig gmail.com" 。 当然,我可以通过 php 循环这个选择,但与 MySQL 上的循环游标相比,它会非常慢。 如何在 macOS 上将终端命令从 mysql 发送到终端并检索答案?

I'm using php with MySQL on macOS.
I would like to select a large amount of emails from a database and perform a dns lookup for each email in my selection using a dig command from the terminal/shell, something like: "dig gmail.com" .
Of course, I can loop this select through php but it will be very slow compared to looping cursor on MySQL.
How to send terminal commands from mysql to the terminal and retrieve answer on macOS?

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

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

发布评论

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

评论(2

眼眸里的那抹悲凉 2025-01-03 18:59:58

你不能从 SQL 查询中执行 shell 命令(感谢上帝),否则这将是一个可怕的安全漏洞......你必须从 php 执行它。

PS 然而,可以从 MySQL 命令行实用程序执行 shell 命令

\! ls

...但如果我理解你的问题,它无助于解决你当前的问题。

You can't execute shell commands from within an SQL query (thank god), or else it would be a horrible security vulnerability... You would have to do it from php.

P.S. It is however possible to execute shell commands from the MySQL command line utility

\! ls

...but if I understand your question, it won't help solve your current problem.

一绘本一梦想 2025-01-03 18:59:58

(我假设当您谈论电子邮件地址时,您真正指的是 ADDR_SPEC)

但是与mysql上的循环游标相比会非常慢

不,不是真的。唯一的区别是,根据您的实现方式,PHP 方法要求您在开始迭代之前检索整个结果集。然而,将其分解为更小的结果集是微不足道的。

此外,算法性能的限制是 DNS 查找的速度 - 这就是延迟 - 如果您的目标是使其速度更快,那么您应该并行运行多个请求。

接下来您应该考虑的是,每个 MX 可能有多个邮箱,例如 [电子邮件受保护][电子邮件受保护]....如果您已正确设置 DNS 缓存如果您正在处理非常大的数据集或将不止一次执行此操作,使用唯一的 MX 主机值更有意义,例如,

 SELECT DISTINCT SUBSTR(addr_spec FROM LOCATE('@', addr_spec)) AS mx2chk
 FROM yourtable
 WHERE addr_spec LIKE '%@%'
 AND (email_checked IS NULL
   OR email_checked<NOW() - INTERVAL 300 DAY )
 ;

确实,如果您标记数据,那么您可以使用自己的数据库来验证 MX。

从终端/shell 使用 dig 命令

请不要告诉我您正在从 PHP 控制进程运行 shell 来执行 DNS 查找?

(I'm assuming that you really mean ADDR_SPEC when you're talking about email addresses)

but it will be very slow in compare with looping cursor on mysql

No not really. The only difference is that depending on how you implement this the PHP approach requires that you retrieve the entire result set before you start iterating through it. However breaking this up into smaller result sets is trivial.

Also, the limitation on the performance of your algorithm is the speed of DNS lookups - and that's all about latency - if your objective is to make this go faster then you should be running multple requests in parallel.

The next thing you should consider is that you've probably got multiple mailboxes for each MX, e.g. [email protected], [email protected].... While if you've got DNS caching setup properly there will be less overhead than going to the source each time, if you're working with a very large data set or will be doing this more than once, it makes a lot more sense to just work with unique MX host values, e.g.

 SELECT DISTINCT SUBSTR(addr_spec FROM LOCATE('@', addr_spec)) AS mx2chk
 FROM yourtable
 WHERE addr_spec LIKE '%@%'
 AND (email_checked IS NULL
   OR email_checked<NOW() - INTERVAL 300 DAY )
 ;

Indeed, if your flagging the data then you can use your own database to verify the MX.

using a dig command from terminal/shell

Please don't tell me that you're running a shell from a PHP controlling process to do a DNS lookup?

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