Can't Connect to Memcache via PHP on CentOS

5,715 views
Skip to first unread message

zhann

unread,
Apr 23, 2011, 5:58:17 PM4/23/11
to memcached
Hello. I am very confused as to whats going on. I have installed
memcache via yum, compiled and built the memcache.so file. I copied
the file over to '/usr/lib64/php/modules' and then added the reference
to php.ini.

Funny thing, when I try starting memcache via '/etc/init.d/memcached
start' I always get the same response:

Starting Distributed memory caching (memcached): [ OK ]

Shouldn't it fail if memcache is already running? Now, I can telnet to
memcache and when I run 'netstat -an | grep 11211' I get the
following:

tcp 0 0 0.0.0.0:11211 0.0.0.0:*
tcp 0 0 :::11211 :::*
udp 0 0 0.0.0.0:11211 0.0.0.0:*
udp 0 0 :::11211 :::*

Any ideas? I have been shooting blanks for a while, tried a lot, but
so far nothing.

Strangely, I have my suspicions that the memcache.so file is my
problem. I tried recompiling it by getting the source and building it,
but no memcache.so file is being generated anywhere.

wget http://pecl.php.net/get/memcache-2.1.2.tgz
tar -xvf memcache-2.1.2.tgz
cd memcache-2.1.2
phpize && ./configure enable-memcache && make

The entire process ends with the following lines (maybe there's more?)

checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
configure: creating ./config.status
config.status: creating config.h

But, when I try finding the memcache.so file via 'find . -name
memcache.so', nothing ... no clue as to why.
Please help
Dan

Roberto Spadim

unread,
Apr 23, 2011, 8:30:30 PM4/23/11
to memc...@googlegroups.com
hum, did you tried memcached?
pecl install memcached
there´s a nother package at pecl
pecl install memcache

check what you want: memcache or memcached

for pecl(php) is normal to use pecl command and not wget,tar,./configure,make


2011/4/23 zhann <zha...@gmail.com>:

--
Roberto Spadim
Spadim Technology / SPAEmpresarial

Dan Golob

unread,
Apr 23, 2011, 9:10:49 PM4/23/11
to memc...@googlegroups.com
Ok, I tried the pecl method, at least what I could find.

First, I did a: yum remove memcached

Then, I tried the following:

sudo pecl install memcached

pecl/memcached requires PHP (version >= 5.2.0, version <= 6.0.0, excluded versions: 6.0.0), installed version is 5.1.6
No valid packages found
install failed

So ... I am back to square one. Any thoughts?
--
My Mizozo Profile -
http://shark.mizozo.com/

Roberto Spadim

unread,
Apr 24, 2011, 12:40:32 AM4/24/11
to memc...@googlegroups.com
hum, i don´t know if memcache is supported by memcached team, there´s
a pecl package that use libmemcache that´s the most common library, or
you should update php... maybe to version 5.3.6 (last), you tried an
older version of memcached pecl library?

2011/4/23 Dan Golob <zha...@gmail.com>:

Geoffrey Hoffman

unread,
Apr 24, 2011, 6:59:40 PM4/24/11
to memc...@googlegroups.com
What us the error you are getting from PHP and are you using/trying 'memcache' or 'memcached' php class? What version of php? Are you running memcache daemon on same server where LAMP is running? If other server open port 11211 with iptables.

Sent from my iPhone

Dan Golob

unread,
Apr 24, 2011, 8:03:27 PM4/24/11
to memc...@googlegroups.com
I am using memcached ... for setup, I ran:

yum install memcached

I am running PHP 5.1.6. As for running, I tried /etc/init.d/memcached start ... for persistence, I opted for chkconfig --add memcached ... 

Now, I have actually done this several times int he past. I have never had a problem before. 

Geoffrey Hoffman

unread,
Apr 24, 2011, 9:03:44 PM4/24/11
to memc...@googlegroups.com
It may be that you installed memcached correctly but not php-memcache or pecl-memcached - if you do phpinfo( ) does it say that the client is installed? It is not always perfectly straightforward to get the correct libraries installed. Did you take CentOS's default php 5.1.6 or use a newer RPM?

What error(s) do you get if you try 

<?php
$mc = new memcache('localhost',11211);
$mc->set('foo','bar');
echo $mc->get('foo');
?>

And, you said you can connect locally with telnet?

Dan Golob

unread,
Apr 24, 2011, 9:39:58 PM4/24/11
to memc...@googlegroups.com
Hello again Geoffrey. 

I really can't say what I installed at this point, I have tried so many things. When doing a basic search in google for "apache php memcache", the first three pages are visited. It is quite frustrating, especially when I get the feeling that I completely screwed things up by installing too many things, or conflicting things.

The only errors I can see in my php.error file is:

[24-Apr-2011 17:15:01] PHP Warning:  Module 'memcache' already loaded in Unknown on line 0
[24-Apr-2011 17:15:01] PHP Notice:  Memcache::connect(): Server localhost (tcp 11211) failed with: Connection refused (111) in /var/www/html/mizozo.com/algorithm.php on line 131

This is the code I have been using:

$memcacheON = true;
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or $memcacheON=false;

I then test against hte memcacheON variable to see if I can use it later in the code ... its a vulgar way to do it, I know, but if you saw the code I was working with you would understand.

As for centos, it was installed by Bluemile, so I have no idea how they did it. All I know is that when I do a "cat /etc/redhat-release" I get as a reply "CentOS release 5.5 (Final)"

As for phpInfo, this gets very confusing to me. I will try to copy it below, but I opened it up on a domain on the server, so feel free to look yourself (mizozo.net/info.php)

memcache

memcache support enabled
Active persistent connections 0
Version 2.2.6
Revision $Revision: 303962 $

Directive Local Value Master Value
memcache.allow_failover 1 1
memcache.chunk_size 8192 8192
memcache.default_port 11211 11211
memcache.default_timeout_ms 1000 1000
memcache.hash_function crc32 crc32
memcache.hash_strategy standard standard
memcache.max_failover_attempts 20 20

Geoffrey Hoffman

unread,
Apr 24, 2011, 10:22:55 PM4/24/11
to memc...@googlegroups.com
Okay delete that file from your public server. Looks like you have memcache class for PHP. (Note there is also "Memcached") see

Can you connect with telnet locally?

If so, try connecting, setting and getting a value using my sample code.

What if any errors are getting put to screen or to php error log?

Geoffrey Hoffman

unread,
Apr 24, 2011, 10:24:52 PM4/24/11
to memc...@googlegroups.com
Sorry, I see you provided error output.

Connection refused, eh?

Are you connecting to localhost or another server running memcached?

Try opening port 11211 with iptables (if CentOS - did you say what distro you are using?)

Dan Golob

unread,
Apr 25, 2011, 8:58:28 AM4/25/11
to memc...@googlegroups.com
I have tried connecting both via localhost and 127.0.0.1 ... same results. 

I connected to telnet from the same machine, using 'telnet localhost:11211' ... not too familiar with telnet, to be honest, but I connected to something and couldn't figure out how to get out.

The php error logs only have those two that I mentioned ... just can't connect:

[24-Apr-2011 17:15:01] PHP Warning:  Module 'memcache' already loaded in Unknown on line 0
[24-Apr-2011 17:15:01] PHP Notice:  Memcache::connect(): Server localhost (tcp 11211) failed with: Connection refused (111) in /var/www/html/mizozo.com/algorithm.php on line 131

I have been avoiding the firewall, because for one I know little about it, but also because since I am connecting locally and didn't think that the firewall would be a problem. Maybe this is the reason?

Also, is there a way that I can be sure to remove everything so that I can start from scratch? I probably installed one too many (or 3 too many) modules, some may be conflicting. For example, if I had installed memcached and pecl's version, would they work in unison or break one another? As I had mentioned before, at this point I have no idea what I installed ... I have gone through so many different instruction sets that anything is possible. I try to remove things, but often get caught up in the moment and forget.

Thanks for all your help, by the way

Roberto Spadim

unread,
Apr 25, 2011, 9:00:05 AM4/25/11
to memc...@googlegroups.com
humm try to run memcache daemon, and
netstat -an | grep 11211

to check if port is open or not

2011/4/25 Dan Golob <zha...@gmail.com>:

--

Xun

unread,
Aug 23, 2012, 12:57:04 PM8/23/12
to memc...@googlegroups.com
netstat -an | grep 11211
gives me 
tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      
udp        0      0 127.0.0.1:11211             0.0.0.0:* 

I've also added the rule to iptables as shown below
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:memcache 

I am able to telnet and check version, stats of memcache via command
telnet 127.0.0.1 11211

However, I am not able to reach memcached via the browser.
i.e. this line "memcache->connect('localhost', 11211) " returns false. 

Where did I forgot to set? How come I am able to telnet to memcached, but not via the browser? 

Any insight welcome. I am stuck here for two days...

BTW, I pecl installed memcache. I tried memcached but got a bunch of errors. "make failed". 
What exactly is the difference, and which is recommended?

P.S.  I was following this instruction
Xun

Xun TANG

unread,
Aug 23, 2012, 1:09:04 PM8/23/12
to memc...@googlegroups.com
Forgot to say, another weird thing is, I tried nmap 127.0.0.1, 
but port 11211 isn't listed. 

Geoffrey Hoffman

unread,
Aug 23, 2012, 2:17:10 PM8/23/12
to memc...@googlegroups.com
if you do

php -i | grep memcache

is the extension loaded? You have the memcache.so enabled in php.ini?

ps aux | grep memcache

to check the process is running.

Xun TANG

unread,
Aug 23, 2012, 2:47:08 PM8/23/12
to memc...@googlegroups.com

To Geoffrey, the extension is loaded, memcache.so is enabled, and the process is running.

Instead, the problem is on SELinux. Thanks to Rathish, here is the solution (at least for me).

My system is CentOS 6.3

Solution:

#command to check if SELinux is on

getenforce

 

#Disable SELinux using the following command

setenforce 0


Xun

Geoffrey Hoffman

unread,
Aug 23, 2012, 3:09:35 PM8/23/12
to memc...@googlegroups.com
Xun, thanks for follow up.

setenforce 0

Oh yeah, good one. I haven't used CentOS in awhile. Glad it's working!



stevewh

unread,
Sep 7, 2012, 4:50:58 AM9/7/12
to memc...@googlegroups.com
This worked for me also. Though I'm wondering how I could have it work with SELinux enforcing.

I suspect that I don't have the iptable setup correctly (since I didn't add anythign for 11211) or that I configured things wrong.

tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN
tcp        0      0 :::11211                    :::*                        LISTEN
udp        0      0 0.0.0.0:11211               0.0.0.0:*

udp        0      0 :::11211                    :::*

Thx for any direction on this.

Cheers,
Steve

stevewh

unread,
Sep 7, 2012, 4:55:16 AM9/7/12
to memc...@googlegroups.com
figured it out -l 127.0.0.1
Reply all
Reply to author
Forward
0 new messages