You may store some data in the global variable
$_SESSION for convenient access when establishing a PHP website. The ini configuration file of PHP contains
[Session] configurations for storing the data to a file or the Memcached server. The configuration item
session.save_handler = memcached makes the decision. In most cases, the session data does not require persistence. The session information is cached into Memcached to improve the website performance.
ApsaraDB for Memcache and self-built Memcached both comply with the standard Memcached protocol. Some users, in a hope to reduce server memory usage and cut down on investment in Memcached maintenance, want to migrate session information stored in the self-built Memcached to ApsaraDB for Memcache without modifying the code. This document guides you to tackle with the problems, which may arise during the migration process.
The most important difference between ApsaraDB for Memcache and self-built Memcached is the account and password authentication.
ApsaraDB for Memcache: ApsaraDB for Memcache is a distributed cluster offering external services in a uniform way. It achieves load balance without SPOF, and you can adjust the configuration flexibly without restarting the service. When external services are involved, the corresponding security mechanism is in place, such as the whitelist, throttling, and account and password authentication.
Self-built Memcached: Most of the self-built Memcached instances do not require an account or password, so the SASL authentication is missing in self-built Memcached compared with ApsaraDB for Memcache. To migrate session information stored in self-built Memcached to ApsaraDB for Memcache, the account and password need to be configured in php.ini.
The feature is not supported in older versions of PHP Memcached extensions. You must upgrade PHP Memcached extension to Version 2.2.0. The example code is as follows:
tar zxvf memcached-2.2.0.tgz
./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl
Find the upgraded memcached.so, run the
statcommand to confirm whether it has been upgraded (pay attention to the last modified time).
Modify the php.ini configuration.
Session section: Find the
[Session]section and modify the storage engine to:
session.save_handler = memcached **(Attention: it has a d)**
Modify the storage address, that is, the Memcache access address, to:
session.save_path = "be6b6b8221cc11e4.m.cnhzalicm10pub001.ocs.aliyuncs.com:11211" (Attention: for an extension with a d, no preceding tcp:// is needed. For an extension without a d, the preceding tcp:// is required)
Modify the key time for caching to Memcached:
session.gc_maxlifetime = 1440 (Unit: second. A reasonable life time is strongly recommended to make sure that only hotspot data is cached in ApsaraDB for Memcache)
Memcached section: In the global section of php.ini, create a separate [memcached] section, and add the following configuration in the blank space:
memcached.use_sasl = On
memcached.sess_binary = On
memcached.sess_sasl_username = "your_ocs_name"
memcached.sess_sasl_password = "your_ocs_password"
memcached.sess_locking = Off
The installation steps are completed. For other useful parameters in the preceding Memcached and Session sections, see the following links.
Write the following testing code as in session.php:
$sn = session_id();
echo "session id:".$sn."\n";
The output is as follows:
Get the data written by session.php from Memcache through the testing code in get.php.
$memc = new Memcached();
/*Attention: Here the key has a prefix defined by the memcached.sess_prefix field in php.ini. The default value is "memc.sess.key." The prefix is followed by the sessionid "ttrct9coa2q62r2sodlq4qf376" as shown above. */
It indicates the PHP SESSION has been successfully written into the Memcache.