2

I am moving some servers from Ubuntu 16 to Ubuntu 18 and I am having trouble getting php sessions and memcache working.

On the current (Ubuntu 16) setup, there are three servers sharing sessions using memcache (note: not memcached!).

I believe I have replicated the current setup on the new servers, but it doesn't work and I get the following error in the apache error log:

PHP Warning:  session_start(): Failed to read session data: memcache (path: tcp://10.32.82.6:11211?persistent=1&weight=1&timeout=1&retry_interval=15,tcp://10.32.82.7:11211?persistent=1&weight=1&timeout=1&retry_interval=15,tcp://10.32.82.8:11211?persistent=1&weight=1&timeout=1&retry_interval=15)

In phpinfo() I see that the session.save_path and session.save_handler are set correctly (as reflected in the log message above).

If I go to any one of the three machines, I can telnet to port 11211 on any of the other servers.

If I look at the logs (set to -vv), I can see stuff happening and quite a few "NOT_FOUND" statements, but I don't really know how to interpret these logs.

In /etc/php/7.2/mods-available/memcache.ini:

memcache.allow_failover="1"
memcache.max_failover_attempts="4"
memcache.session_redundancy="4"

On the current (Ubuntu 16) setup, I remember trying both memcached and memcache and figuring out that memcache was the right solution. Trying memcached on the new setup is my last resort, as I feel that memcache should work, and I am just missing something, or something has changed in Ubuntu 18 that I am not aware of.

Ben Holness
  • 914
  • 2
  • 10
  • 28

2 Answers2

5

I had the same issue with PHP 7.1 and memcached, but i think this also will solve your problem.

The issue is with the read function which return false or null if the session doesn't exists. You need to override that function to return an empty string.

How i did it:

created a class which extended SessionHandler

class MyMemcachedSessionHandler extends SessionHandler {
    public function read($id)
    {
        $data = parent::read($id);
        if(empty($data)) {
            return '';
        } else {
            return $data;
        }
    } 
}

and before session_start() i registered it

$myMemcachedSessionHandler = new MyMemcachedSessionHandler();
session_set_save_handler($myMemcachedSessionHandler);

i hope this will help

0

I had the same problem. I have three servers, upgraded one to PHP7.2 and the others running on 7.0 temporarily. I tracked down the issue to a config change between the two for the memcached library. The 7.0 version had memcached.sess_prefix set to memc.sess.key.. The 7.2 has memc.sess.. I fixed it by running this before session_start() on the 7.2 box:

ini_set('memcached.sess_prefix', 'memc.sess.key.');
Dave Child
  • 297
  • 5
  • 15