Enterprise-level memcached deployment (session sharing) and memcachedsession
Server deployment
First milestone: Install dependency
Memcache uses the libevent library for Socket processing.
[root@nfs01 ~]# yum install libevent libevent-devel nc -y
Second milestone: Installing memcache
[root@nfs01 ~]# yum install memcached -y[root@nfs01 ~]# which memcached/usr/bin/memcached
Third milestone: Start memcachedService
[root@nfs01 ~]# memcached -m 16m -p 11211 -d -u root -c 8192[root@nfs01 ~]# lsof -i :11211COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEmemcached 10796 root 26u IPv4 85717 0t0 TCP *:memcache (LISTEN)memcached 10796 root 27u IPv6 85718 0t0 TCP *:memcache (LISTEN)memcached 10796 root 28u IPv4 85721 0t0 UDP *:memcachememcached 10796 root 29u IPv6 85722 0t0 UDP *:memcache[root@nfs01 ~]# netstat -lntup |grep memcatcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 10796/memcached tcp 0 0 :::11211 :::* LISTEN 10796/memcached udp 0 0 0.0.0.0:11211 0.0.0.0:* 10796/memcached udp 0 0 :::11211 :::* 10796/memcached
Note: memcachedYou can start multiple instances at the same time.
[root@nfs01 ~]# memcached -m 16m -p 11212 -d -u root -c 8192
The fourth milestone: Write Data to boot automatically
echo 'memcached -m 16m -p 11211 -d -u root -c 8192' >>/etc/rc.local
Client deployment(Web Server)
First milestone: Installing PHP memcacheExtension
The command set is as follows:
cd /server/toolswget http://pecl.php.net/get/memcache-2.2.7.tgztar xf memcache-2.2.7.tgzcd memcache-2.2.7/application/php/bin/phpize./configure -enable-memcache --with-php-config=/application/php/bin/php-configmake && make install
Complete operation process:
[root@web01 ~]# cd /server/tools/[root@web01 tools]# wget http://pecl.php.net/get/memcache-2.2.7.tgz[root@web01 tools]# tar xf memcache-2.2.7.tgz[root@web01 tools]# cd memcache-2.2.7[root@web01 memcache-2.2.7]# /application/php/bin/phpizeConfiguring for:PHP Api Version: 20121113Zend Module Api No: 20121212Zend Extension Api No: 220121212[root@web01 memcache-2.2.7]# ./configure -enable-memcache --with-php-config=/application/php/bin/php-config[root@web01 memcache-2.2.7]# make && make install
Check whether the installation is successful
[root@web01 memcache-2.2.7]# ls -l /application/php/lib/php/extensions/no-debug-non-zts-20121212/total 252-rwxr-xr-x 1 root root 258048 Nov 7 10:03 memcache.so
Memcache. soIndicates that the plug-in is successfully installed.
Second milestone: Configuring memcacheMake the client take effect
[root@web01 memcache-2.2.7]# cd /application/php/lib/[root@web01 lib]# vim php.ini……[root@web01 lib]# tail -2 php.iniextension_dir = "/application/php/lib/php/extensions/no-debug-non-zts-20121212/"extension = memcache.so
Third milestone: Check the syntax and restart the service
[root@web01 lib]# /application/php/sbin/php-fpm -t[07-Nov-2017 10:20:44] NOTICE: configuration file /application/php-5.5.32/etc/php-fpm.conf test is successful
#Restart service
killall php-fpmkillall php-fpm/application/php/sbin/php-fpm
BrowserAccessPhpinfoMemcache appears on the pageThe information indicates that the configuration is successful..
Fourth milestone: Write and test memcacheFile
[root@web01 blog]# cat test_memcache.php<?php $memcache = new Memcache; $memcache->connect('172.16.1.31', 11211) or die ("Could not connect NFS server"); $memcache->set('key', 'Memcache connect OK'); $get = $memcache->get('key'); echo $get;?>
Memcache connect OKConnection successful
[root@web01 blog]# /application/php/bin/php test_memcache.php Memcache connect OK
Fifth milestone: modifying phpConfiguration(Set sessionShare)
[root@web01 ~]# vim /application/php/lib/php.ini
Original configuration
session.save_handler = filessession.save_path = "/tmp"
To:
session.save_handler = memcachesession.save_path = "tcp://172.16.1.31:11211"
Restart the php service after modification.
killall php-fpmkillall php-fpm/application/php/sbin/php-fpm
Phpinfo before modificationInformation
After modification, phpinfoInformation
The enterprise-level memcache (session sharing) has been deployed.
1.1.1 Advantages and Disadvantages of Memcached in cluster session shared storage
Advantages:
1) reading/writing speed is much faster than normal file files.
2) It can solve the problem of multiple servers sharing sessions.
Disadvantages:
1) session data is stored in memory, which lacks persistence, but is not a problem for session data.
2) generally, it is a single node. If multiple nodes are deployed, data cannot be synchronized between multiple nodes. The hash algorithm still causes session loss.
Alternative solution:
1) You can use another persistent system to store sessions, such as redis and ttserver, instead of memcached.
2) in high-performance concurrency scenarios, cookies are much more efficient than sessions. Therefore, large websites use cookies to solve session sharing problems.
3) Some bad Methods: lvs-p, nginx ip_hash, not recommended.
DedeCMS using memcache
Problem: After modifying the file, DedeCMS found that the background http://www.etiantia.org/dede could not be accessed
Solution:
Modify file 1:
[Root @ web01 include] # pwd/application/nginx/html/www/include [root @ web01 include] # vim common. inc. php135 // Session storage path 136 $ enkey = substr (md5 (substr ($ pai_cookie_encode, 137),); // $ sessSavePath = DEDEDATA. "/sessions _ {$ enkey}"; 138 $ sessSavePath = "tcp: // 172.16.1.31: 11211"; 139 if (! Is_dir ($ sessSavePath) mkdir ($ sessSavePath );
Modify file 2:
[root@web01 include]# vim vdimgck.php24 $enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);25 //$sessSavePath = DEDEDATA."/sessions_{$enkey}";26 $sessSavePath = "tcp://172.16.1.31:11211";27 if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);
Let DedeCMS directly use memcache sharing to solve the problem.
Special thanks:Yuan Fang