Reputation: 35607
I have PHP 5.3.3 installed on Centos 6.4 with the memcached.so
extension, and httpd is running with version 2.2.15-26. Here is my index.php
:
$mc = new \Memcached();
$mc->addServer('127.0.0.1', 11211);
$mc->set("test", "blah");
var_dump($mc->getResultCode());
var_dump($mc->getResultMessage());
var_dump($mc->get("test"));
die;
When I run it from the command line, it works. I get the following:
10:22:33 $ php index.php
int(0)
string(7) "SUCCESS"
string(4) "blah"
The memcache server also works from telnet. However, when I run index.php
from the web, it fails. I get the following (from viewing webpage source):
int(47)
string(51) "SERVER HAS FAILED AND IS DISABLED UNTIL TIMED RETRY"
bool(false)
Short of re-installing my OS and trying different versions of crap, can anyone explain what might cause this issue?
Upvotes: 11
Views: 3728
Reputation: 21
I have similar issue on CentOS and what I found it I am running SELinux which prevent httpd to connect to memcached. You need to set below,
# setsebool -P httpd_can_network_memcache 1
# getsebool httpd_can_network_memcache
httpd_can_network_memcache --> on
Upvotes: 2
Reputation: 918
I had exactly the same issue as described by the OP. It turned out that the problem was caused by the server list the memcached extension maintains internally. My code was something like:
$serversList = $memcached->getServerList();
if (empty($serversList)) {
$memcached->addServer($host, $port);
}
My initial call to the testing script was done with a wrong value for $port. The call was done from web (apache) not from cli. After i corrected the port and i ran the code again, it was skipping the 'if' and used the existing servers list which was flawed and so it failed again.
Seeing the failure from the web i tested with the cli and it was working perfectly. In the cli the servers list was different from the one in the web. In fact, the server list was empty at each start of the script, even if my script was setting it with each run. Yet it was persisting between calls on the web.
Anyway, after clearing the servers list on the web and setting the correct server, it worked as expected from the web too.
Upvotes: 0
Reputation: 11
I had this problem in WAMP 2.4, running a simple Memcache test script worked from the command line, but not in the browser.
The answer turned out to be stunningly mundane: WAMP had two php.ini files, and I was editing the wrong one.
E.g. Apache used this one: c:\wamp\bin\apache\Apache2.4.4\bin\php.ini WAMP also had this one: c:\wamp\bin\php\php5.4.12\php.ini
Putting the extension=php_memcache.dll in the correct .ini file fixed things.
My clue something like this was the problem was that phpInfo()'s loaded config file reported different values in the two cases.
Upvotes: 0
Reputation: 3135
When I look at examples I see it being used without the namespace "\" modifier. Try without it maybe?
http://www.php.net/manual/en/memcache.examples-overview.php
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
Upvotes: -1
Reputation: 11
Please ensure that your memcache service should bind all IPs. Default value is 127.0.0.1. change it to 0.0.0.0 for supporting all defined Ips. In additional, don't forget to control your iptables or firewall.
Upvotes: 0
Reputation: 51
Is it an SELinux problem ? Cli can access to Memcached but daemon no. Try this :
getenforce
to know if you have SELinux enabledsetenforce 0
to disable itreboot
If is good, You must configure Apache to access to Memcached.
Upvotes: 2