Memcache连接在cron作业中抛出错误


Memcache connect throws error in cron job

当我尝试从php页面连接到memcached服务器时,它的工作没有任何问题。使用以下代码

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

但是当它试图从一个由cron job触发的php脚本连接到服务器时,它会抛出这个错误

Warning: Memcache::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/...../cron/acts_cron.php on line 3
Warning: Memcache::connect(): Can't connect to localhost:11211, php_network_getaddresses: getaddrinfo failed: Name or service not known (0) in /home/...../cron/acts_cron.php on line 3
Could not connect

什么会导致这个问题?

似乎您在hosts文件中缺少localhost条目。尝试更新/etc/hosts,并确保在其中有如下一行:

127.0.0.1               localhost.localdomain localhost

使用127.0.0.1而不是localhost修复了这个问题。

相关文章: