简体   繁体   中英

PHP sessions not working with memcached

I am trying to setup memcached to allow session sharing on 2 load balanced Apache servers - in my php.ini I have:

Node 1

session.save_handler = memcached
session.save_path = "tcp://NODE1_IP:11211"

and

Node 2

session.save_handler = memcached
session.save_path = "tcp://NODE2_IP:11211"

In /etc/sysconfig/memcached, I have:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"

In phpinfo I have:

session.save_handler    memcached   memcached
session.save_path   tcp://NODE1_IP:11211    tcp://NODE1_IP:11211

When I login to my application I have session_start on the login page and on the page which checks if the user is logged in but when I:

print_r($_SESSION);

Nothing gets shown as the session looks like it is not being set.

One thing of note is that in phpinfo I see at the top of the session section, these lines:

Session Support enabled
Registered save handlers    files user
Registered serializer handlers  php_serialize php php_binary wddx

Registered save handlers lists files and user but nothing about memcached - might that be why this is not working? How can I change/add to that?

Should the node1_ip be the public or private IP or doesn't it matter?

I can telnet to localhost 11211 but not to NODE1_IP 11211 - get connected refused. Port 11211 is open - tried changing /etc/sysconfig/memcached to "-l NODE1_IP:11211" but still get connection refused

If I change the options in /etc/sysconfig/memcached to nothing ie OPTIONS "" then I can telnet to NODE1_IP 11211 so it appears the format of what I am putting into memcached config is wrong?

Is there anything else I need to do? I have tried changing the save paths, adding and removing tcp, using 127.0.0.1, using actual IP addresses, using all the IP addresses seperated with a comma but still cannot get this to work.

Assuming you are using sticky sessions on load balancer, and cannot guaranty requests from the same client are served by the same apache.

OPTIONS="-l 127.0.0.1" tells memcached to listen on local loopback only, ie it is not accessible from anywhere but localhost. You need to change that to listen on the private network interface (be sure it is not publicly accessible from the internet).

To keep session available on both apache servers, you need to sync memcached. The simplest way is to replicate all data by pointing save_path to both servers, and set session_redundancy to 3:

session.save_path = "tcp://NODE1_IP:11211, tcp://NODE2_IP:11211"
memcache.allow_failover=1
memcache.session_redundancy=3

so the session will be saved to both memcached servers.

If you don't want to sacrifice memory for redundancy, leave only 1 memcached running, and point both apache servers to it.

EDIT :

php_memcache library should be installed, eg for debian distros:

php 5.x: apt-get install php5-memcache

php 7.x: apt-get install php-memcache

If you have:

session.save_handler = memcached

It means you are using memcache D extension and not memcache one.

And in that case, the config should be written without the tcp:// protocol:

session.save_path NODE1_IP:11211

session.save_path=tcp://NODE1_IP:11211 is for the memcache extension, without "d" at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM