15

I am trying to connect to memcache as they suggest:

$memcache = new Memcache();
$memcache->pconnect('localhost',11211);

But i get:

Notice: Memcache::pconnect() [memcache.pconnect]: Server localhost (tcp 11211) failed with: Connection refused (111) in /home/user/public_html/website.com/includes/basedatos.php on line 26

Any idea why?

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378
  • 1
    is memcahced running on your computer on port 11211? – dm03514 May 27 '12 at 18:17
  • 1
    That generally means memcache isn't running. Have you set it up? – Amber May 27 '12 at 18:17
  • hm.. @Amber it seems i followed the wrong steps. my question is the second step i took from installing it via pecl, what do i need to do to set it up? – Toni Michel Caubet May 27 '12 at 18:18
  • 2
    memcache is two parts: a client interface (the PHP version of which is installed via PECL or similar) and the actual memcache server daemon, which needs to be installed as a completely separate program or system package. What OS are you running on? If it's a debian-based flavor of Linux, try `sudo apt-get install memcached` – Amber May 27 '12 at 18:19
  • @Amber I see, so i am using CentOS, shall i try? and after that apt-get is there anything else i need to do? thanks a lot – Toni Michel Caubet May 27 '12 at 18:21
  • On CentOS you'll probably need to do `yum install memcached`. After that, you may need to start the server (most likely with `/etc/inid.d/memcached start`. (`yum` might automatically start it up for you, though; I forget.) – Amber May 27 '12 at 18:22
  • [root@vps ~]# /etc/inid.d/memcached start -bash: /etc/inid.d/memcached: No such file or directory [root@vps ~]# /etc/inid.d/memcache start -bash: /etc/inid.d/memcache: No such file or directory [root@vps ~]# – Toni Michel Caubet May 27 '12 at 18:26

3 Answers3

23

You need to actually install the memcached server so that it can be connected to. On CentOS, this can be done with...

sudo yum install memcached

(on debian flavors of linux, use apt-get instead of yum)

Amber
  • 507,862
  • 82
  • 626
  • 550
  • @ToniMichelCaubet - the syntax Amber provides is correct, [read this](http://www.lullabot.com/articles/installing-memcached-redhat-or-centos). You may need to do some web-searching around the problem since we don't have access to try a few other things that would come to mind if we were at your console. Do some digging `:)`. – halfer May 27 '12 at 18:43
  • (Also the fact that some distros have sane package repositories that actually include most of the things you might want in a basic server. *cough*) – Amber May 27 '12 at 18:53
  • I was able to do it like this http://kb.liquidweb.com/install-memcached-on-centos-5/ but now i have this problem http://stackoverflow.com/questions/10776545/memcache-call-to-a-member-function-get-on-a-non-object :( – Toni Michel Caubet May 27 '12 at 19:13
  • i think this is incorrect isnt that so, because he is asking about Memcache and the given answer is for the "Memcached" two different things – mahen3d Sep 26 '13 at 02:09
  • @user1179459 memcached is the memcache server. – Amber Sep 28 '13 at 00:09
  • so there are two php extensions called `memcache` and `memcached`, both of which are only client apis to a third and totally separate server program called `memcached`... this seems stupid... – user3338098 Apr 13 '15 at 20:19
  • Found a great tutorial on doing this on a mac. Includes some Drupal stuff too but I was mostly interested in the brew direction. http://mark.shropshires.net/blog/setting-memcached-mac-local-drupal-development – danielson317 Apr 19 '15 at 23:45
  • Thanks +1 simple solution – Goper Leo Zosa Sep 06 '16 at 09:38
18

This may be obvious, but make sure that memcached is actually running:

service memcached start
Andrew Rout
  • 225
  • 2
  • 9
  • For me, it was actually because it was running, but I had changed from the default port in /etc/memcached.conf – Kzqai Feb 23 '15 at 15:32
6

memcache is diffrent with memcached

and i thing you have installed memcache because $memcache = new Memcache(); worked

you must started it on your server

check this 10 baby steps to install Memcached Server and access it with PHP

S. Ali Mihandoost
  • 2,873
  • 3
  • 18
  • 24