Memcached – Failed to Connect to Memcache Host

memcached

I'm totally new to memcached !
I sucessfully Installed and started memcached with this command in my host A (Ip 192.168.1.102)

memcached -u memcached -d -m 30 -l 127.0.0.1 -p 11211

I also add these entry to iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 11211 -j ACCEPT

restarted iptables service and it listened to port 11211

telnet also work !

telnet localhost 11211

Trying 127.0.0.1…
Connected to localhost.
Escape character is '^]'.

But I connect to this host A from Host B (IP 192.168.1.103) with this script memcached_test.php

<?php
$memcache = new Memcache;
$memcache->connect('192.168.1.102', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";

var_dump($get_result);
?>

When I browser this script in the server at host B (192.168.1.103). I recieved this error

Warning: Memcache::connect()
[memcache.connect]: Can't connect to
192.168.1.102:11211, Connection refused (111) in
/var/www/memcache_test.php on line 3
Could not connect

Please tell me how to solve this problem!

Best Answer

# memcached -u memcached -d -m 30 -l 127.0.0.1 -p 11211

You'll notice there is "-l 127.0.0.1" in your command line. This tells memcached to only listen on the lo interface. If you want to access it from a remote machine, you need to remove this part of the command line.