MemcachedCache Database
1th ChapterWhat isMemcache?
Open source software for dynamic Web applications to reduce the load on the database, by caching data and objects in memory to reduce the number of service databases , thereby increasing the speed of website access , Working mechanism is to open up a space in memory , build a hash table memcached self-manage these hash table
2nd ChapterWhy use a cached database?
1. mysql --- data is stored on disk , Io slower ---- Data Warehouse Storage Select MySQL This disk database
2. memcached--- data in memory , io fast --- high concurrency , business volume should choose memcache this memory data
3rd ChapterCache Database Usage Scenarios:
1. as a database cache --- data can be cached in memory when MySQL cannot withstand large concurrent requests
2. session sharing --- as long as you log in once , you do not need to log in again
4th Chapter Cookiesand theSession
website program to determine user login information , the first odd way : The server in your browser to write a cookie, this cookie contains your user name and login information , because cookies are stored in a local browser , so third-party tools can easily steal privacy information from cookies, the session emerged
4.1At the beginning:
cookies= content ( user name , login information )
4.2after improvement:
Cookies=session ID
session= user name , login information
General enterprise application is cookies+session
5th Chapter Memcachedsub-deployment cache cluster
Memcached is inherently unsupported for distributed clusters and can only be deployed through programs to support sub-deployment storage
5.1 memcachedcharacteristics of distributed cache clusters:
all MC server memory content is different, these server content together close to the database capacity , such as 1t database , a cache database memory is not so large and therefore divided into ten servers
5.2NormalHashalgorithm:
The Ongoinghash algorithm on the load balancer of the client program or MC, so that the same content is assigned to a MC server , This algorithm can bring a lot of data flow to the node outage , which can cause avalanche effect easily .
5.3ConsistencyHashalgorithm:
by generating a hash circle , data is matched to the nearest server by the clockwise distance , which minimizes the flow of node data to the node outage.
6th ChapterDeploymentMemcachedService: 1.1Installing server-side software:
[Email protected] ~]# yum-y install memcached
1.1.1ViewMemcachedconfiguration file
[Email protected] ~]# cat/etc/sysconfig/memcached
Port= "11211" listening port
User= "memcached" named user
Maxconn= "1024x768" Concurrent access number
Cachesize= the size of the memory
Options= ""
1.2StartMemcacheService
[[email protected] ~]#/etc/init.d/memcached start
Starting memcached: [OK]
1.3writing data to the cache database
[[email protected] ~]# printf "set key008 0 0 10\r\noldboy1234\r\n" |nc 127.0.0.1 11211
STORED writing data successfully
key008--- the name of the data
0---
0 ---time out , the data will not be accessible over this time
The--- represents the number of characters written to the data , where oldboy1234 is a number of characters, so 10
1.4view data in a database
[[email protected] ~]# printf "Get key008\r\n" |nc 127.0.0.1 11211
VALUE key008 0 10
oldboy1234
END
1.5Delete Data
[[email protected] ~]# printf "Delete key008\r\n" |nc 127.0.0.1 11211
DELETED
[[email protected] ~]# printf "Get key008\r\n" |nc 127.0.0.1 11211
END
7th ChapterClient Deploymentmemcache:2.1installationMemcache
1089 CD memcache-2.2.5
1090/application/php/bin/phpize
1091./configure--enable-memcache--with-php-config=/application/php/bin/php-config--with-zlib-dir
1092 make && make install
1093 echo $?
2.1.1Check directory exists after executing commandMemcache.socan be
[Email protected] memcache-2.2.5]# ll/application/php-5.6.32/lib/php/extensions/no-debug-non-zts-20131226/
Total 256
-rwxr-xr-x 1 root root 260643 Feb 21:21 memcache.so
2.1.2IncreasePhpinMemcacheModule
[[Email protected] memcache-2.2.5]# sed ' $a extension=memcache.so '/application/php/lib/php.ini-i.ori
[[email protected] memcache-2.2.5]# pkill PHP
[Email protected] memcache-2.2.5]#/application/php/sbin/php-fpm-t
[19-feb-2018 21:30:59] Notice:configuration file/application/php-5.5.32/etc/php-fpm.conf Test is successful
[Email protected] memcache-2.2.5]#/APPLICATION/PHP/SBIN/PHP-FPM
[Email protected] memcache-2.2.5]#/application/php/sbin/php-fpm-m |grep memcache
Memcache Check if PHP has added memcache module functionality
2.2 PHPCode Testing: 2.2.1in the Site directoryBlogwrite a test file in:
Cat >>/application/nginx/html/blog/mc.php<< ' EOF '
<?php
$memcache = new Memcache;
$memcache->connect ('10.0.0.21', 11211) or Die ("Could not Connect");
$memcache->set (' key20180314 ', ' Hello,world ');
$get _value = $memcache->get (' key20180314 ');
echo $get _value;
?>
Eof
2.2.2TestWebpage access is normal
1.1.1check the presence of data on the cache server:
[[email protected] ~]# printf "Get key20180314\r\n" |nc 10.0.0.21 11211
VALUE key20180314 0 11
Hello,world
END
8th Chapter WebManagementMemcached:
Tar XF memadmin-1.0.12.tar.gz-c/application/nginx/html/blog/
Browser Access http://blog.etiantian.org/memadmin
Host: option to fill in the IP address of the cache server
9th ChaptermakeMemcachedCacheWordpressBlog Data: 1.1putobject-cache.phpProgram files are uploaded to the site Directory
cd/application/nginx/html/blog/wp-content/
Rz
[email protected] wp-content]# LL
Total 28
-rw-r--r--1 Nobody 65534 9 index.php
-rw-r--r--1 root root 10112 Mar 12:05 object-cache.php
1.2Modify the file in theIpAddress
grep 127.0.0.1 object-cache.php
Sed-i ' s#127.0.0.1#10.0.0.21# ' object-cache.php
1.2.1Log in to the website to send a blog:
1.1.1LoginMemcache Webinterface to check:Cache Post Success
10th Chapter Memcached SessionSession Sharing:
method One : through the program implementation , WEB01 only need to write to memcache session,web02 read from memcache Session, as normal data read and write , more Universal
method Two : through php configuration file , PHP default session stored in the file , modified to be stored in the memcached
Sed ' s#session.save_handler = Files#session.save_handler = memcache# '/application/php/lib/php.ini-i
Sed ' $a session.save_path = "tcp://127.0.0.1:11211" '/application/php/lib/php.ini-i
/application/php/sbin/php-fpm-t
Pkill PHP
/application/php/sbin/php-fpm
using this function , you need to use the session function of PHP
Memcached Cache Database