11

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?

16
  • 1
    php has a php.ini file for the CLI and the web\Apache version, make sure you have made appropriate changes in both Commented Apr 23, 2013 at 1:40
  • don't forget to reload the changes Commented Apr 23, 2013 at 1:41
  • @Dagon - The /etc/php.ini file contains "extension=memcached.so" and both the CLI and the web are using it. I think I would get a "Memcached not found" error if the extension wasn't installed in the .ini file... Commented Apr 23, 2013 at 1:45
  • @wayne - I've restarted httpd and rebooted many times. Commented Apr 23, 2013 at 1:45
  • 1
    I believe what @casper123 was asking was whether you've checked your server for multiple installed versions of PHP. Maybe you have one that Apache uses and another that's run in the CLI - maybe one that's installed with yum and another that's compiled from source. Commented Apr 24, 2013 at 23:16

6 Answers 6

2

Is it an SELinux problem ? Cli can access to Memcached but daemon no. Try this :

  • getenforce to know if you have SELinux enabled
  • setenforce 0 to disable it
  • reboot
  • Retry your test

If is good, You must configure Apache to access to Memcached.

Sign up to request clarification or add additional context in comments.

1 Comment

Apache user probably isn't entitled or something weird. Thus it doesn't work when called via Apache.
2

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

2 Comments

Wow! This worked! And I didn't have to reboot either. Thanks a lot!
In my case, 1. Confirmedport 11211 is open 2. Memory set is enough 3. memcache server is running 4. Php can use memcache class. 5. PHP new memcache didn't return it's version and fail to set data It's the solution to me!!! save my day. Thanks.
0

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.

1 Comment

I doubt this is the issue - assuming he's describing running it on the local webserver via CLI. SELinux would be my guess... blasted thing!
0

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.

Comments

0

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.

Comments

-1

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");

1 Comment

When no namespace is defined for the current file Memcache and \Memcache are exactly the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.