Memcached cache database application practices, memcached Cache
1.1 database comparison
Cache:Store data in the memory. cache is enabled only when the disk is not competent.
Disadvantages: power-off data loss (dual-power), the purpose of using cache to store data is only to cope with high concurrency business.
Database:Mysql (Relational Database Service ensures data consistency and prevents data loss. If there are too many functions, the performance will not be high) === Data Reference
Cache database:Memcache redis (non-relational database with extremely high performance, but data integrity is not guaranteed) === business data provider
Memcachedb writes memory data to the disk.
Redis is mainly used in memory, but regular backup of memory data to Hard Disk
1.1.1 database selection
Data storage, data warehouse select mysql Disk Database
For highly concurrent applications, select a memory database such as memcache.
1.1.2 database category
Relational Database mysql
Non-Relational Database Service (NOSQL) memcached redis MongoDB
1.2 introduction to memcached
Memcached is an open-source, high-performance pure-memory cache service software. Mem indicates memory, cache indicates cache, and d indicates daemon.
Memcache is the project name and software. Its architecture isC/SArchitecture.
Memcached Official Website: http://memcached.org/
1.2.1 memcache advantages
① For users, accessing the website is faster and the experience is better.
② For websites, the database pressure is reduced. The database is requested only when the memory has no data. The data written for the first time also requests the database. Generally, companies do not push data to Memcached only when users read the database.
② Improves concurrent Website access and reduces the number of servers.
1.3 Use Cases of Memcached in enterprises 1.3.1 as front-end cache applications for Databases
When the database (mysql) cannot withstand large concurrent requests, You Can cache the data to the memory (cache the database) and then solve the problem.
As the front-end cache of the database, the maximum purpose is to reduce the pressure on the database to be accessed.
1.3.2 session persistence as the backend session of the Cluster
Sessions are stored on files, databases, memcache, or memory servers,
Cookies are stored in the client browser.
Session is a file on a server that is similar to a hash table. It contains the information we need and can be extracted from it when we need it.
SessionCookie dependencyExistAfter the request client arrives at the server, the server will randomly generate a string as the user's identifier. the string is returned to the client through cookie, the client browser will put the string as the key in the session id. the random string key can have no value first. If the user submits the request again, the user information such as the user name and password in the request information is saved in the value of a random string. When the request arrives at the server, the user name and password are correct and the random string is authorized, A value marked as a random string in sessionid proves that the user is logged on. The client again accesses the server with the random string, and the server will know that the user has logged on without verification, directly return the request information.
SessionAnd cookieDifferences
1. cookie data is stored in the user's browser, and session data is stored on the server.
2. Cookies can be extracted and analyzed in local browsers, with poor security. For security purposes, information such as the login account can be cached in the session.
3. The session will be stored on the server for a certain period of time. Increasing the access volume will put pressure on the server. You can use cache tools, such as memcache.
1.3.3 how to determine user information during website development
Initial technical method: the server writes a cookie in your browser, which contains your username and logon information. Because cookies are stored in local browsers, third-party tools can easily steal cookies.
Start:
Cookie name: content (username, Logon Information)
After improvement:
Local browser storage:
Cookie name: content (session id number)
Server storage:
Session id: content (user name, Logon Information)
Mainstream Use Cases: cookies + sessions
1.3.4 different solutions for session sharing
1. NFS sharing for session files
2. rsync scp sharing for session files
3. Store the session content in the database (mysql). All machines can read the session content through ip: port.
4. Store the session content in the cache database. All machines can read the session content through ip: port.
Benefits: Use the features of power failure and restart to lose data. Regularly clean up data; improve concurrency
1.3.5 advantages of memcache principles
Start Memcached. A memory is allocated based on the specified memory size parameter. After we read all kinds of business data from the database, the data will be stored in the Memcached cache together. The next time the user requests the same data, the program will directly retrieve the data from Memcached and return it to the user.
Advantages:
① For users, accessing the website is faster and the experience is better. #
② For websites, the database pressure is reduced. The database is requested only when the memory has no data. The data written for the first time also requests the database. Generally, companies do not push data. Only users who have read the database can store the data in Memcached.
③ Improves concurrent Website access and minimizes the number of servers.
Schematic diagram
1.4 Memcached distributed cache Cluster
Memcached does not support distributed clusters by nature. It must support distributed storage through programs.
1.4.1 features of Memcached distributed cache Cluster
1. The memory content of all MC servers is different. The contents of these servers add up to the database capacity. For example, for a 1 TB database, the memory of a cache database is not that large, so it is divided into 10 cache servers.
2. Use the HASH algorithm on the client (Web) program or MC Load balancer to distribute the same content to an MC server.
3. Common HASH algorithms may cause a large amount of data flow (failure) due to node downtime, which may cause an avalanche effect.
4. Consistent HASH can minimize the data flow (Invalidation) of a node when the node goes down.
Normal hashAlgorithm
First, the key is processed as a 32-Bit String, taking the first eight digits. After hash calculation, the key is processed as an integer and returned, and then mapped to one of the servers to obtain the configuration of one of the servers, use this configuration to complete distributed deployment. Without changing the number of servers, normal hash distribution can work well. When the number of servers changes, the problem arises. Imagine adding a server. After the same key is hashed, the result of the modulo operation on the server is definitely different from that of the previous operation. This leads to data loss.
Consistent hashAlgorithm
Consistent Hash Algorithm
Advantage: in distributed cache, one of them is down, and the key migration efficiency is the highest.
Sort the server list and match adjacent Servers Based on mHash ($ key ).
Consistent hashAlgorithmMinimize Data Flow
References
http://blog.csdn.net/cywosp/article/details/23397179http://blog.csdn.net/zhangskd/article/details/50256111
Chapter 2 installing the memcached2.1.1 environment using memcached 2nd
[root@cache01 ~]# cat /etc/redhat-releaseCentOS Linux release 7.4.1708 (Core) [root@cache01 ~]# uname -r3.10.0-693.el7.x86_64[root@cache01 ~]# getenforceDisabled[root@cache01 ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)[root@cache01 ~]# hostname -I10.0.0.21 172.16.1.21
2.1.2 install memcached
[root@cache01 ~]# yum -y install memcached
2.1.3 view Configuration
[root@cache01 ~]# cat /etc/sysconfig/memcached PORT="11211"USER="memcached"MAXCONN="1024"CACHESIZE="64"OPTIONS=""
2.1.4 view the Startup Script
[root@cache01 ~]# cat /usr/lib/systemd/system/memcached.service [Unit]Description=Memcached Before=httpd.serviceAfter=network.target[Service]Type=simpleEnvironmentFile=-/etc/sysconfig/memcachedExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS[Install]WantedBy=multi-user.target
2.1.5 start the service
[root@cache01 ~]# systemctl start memcached.service
2.2 Manage memcached2.2.1 memcached database syntax format
set key 0 0 10<command name> <key> <flags> <exptime> <bytes>\r\n
\ N wrap and move the cursor to the beginning of the line
\ R move the cursor to the beginning of the line without wrapping
Parameters |
Description |
<Flags> |
It is used together with the data and sending block to save any 16-bit unsigned integer (written in decimal) on the server when retrieving the content ). The client can use it as a "bit domain" to store specific information; it is not transparent to the server. |
<Exptime> |
The end time. If it is 0, this item will never expire (although it may be deleted to make room for other cache projects ). If it is not 0 (Unix timestamp or second offset of the current time point), the client cannot obtain this content after the termination time is reached. |
<Bytes> |
Is the byte length of the subsequent data block, excluding "\ r \ n" for paging ". It can be 0 (followed by an empty data block ). |
<Data block> \ r \ n |
<Data block> is the 8-bit data of a large segment. Its length is specified by <bytes> in the preceding command line. |
2.2.2 database usage
Write and read data
[root@cache01 ~]# printf "set key008 0 0 10\r\noldboy0987\r\n"|nc 10.0.0.21 11211STORED[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211VALUE key008 0 10oldboy0987END
The length of the written data is invalid. The definition is too large.
[root@cache01 ~]# printf "set key009 0 0 11\r\noldboy0987\r\n"|nc 10.0.0.21 11211[root@cache01 ~]# printf "get key009\r\n"|nc 10.0.0.21 11211END
The length of the written data is invalid and the definition is too small.
[root@cache01 ~]# printf "set key010 0 0 9\r\noldboy0987\r\n"|nc 10.0.0.21 11211CLIENT_ERROR bad data chunkERROR[root@cache01 ~]# printf "get key010\r\n"|nc 10.0.0.21 11211END
Timeliness
[root@cache01 ~]# printf "set key011 0 10 10\r\noldboy0987\r\n"|nc 10.0.0.21 11211STORED[root@cache01 ~]# printf "get key011\r\n"|nc 10.0.0.21 11211VALUE key011 0 10oldboy0987END[root@cache01 ~]# printf "get key011\r\n"|nc 10.0.0.21 11211END
Delete data
[root@cache01 ~]# printf "delete key008\r\n"|nc 10.0.0.21 11211DELETED[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211END
2.3 install and use the memcache php Client
Command set
# Compile php_memtar zxvf memcache-2.2.5.tgzcd/application/php/bin/phpize. /configure -- enable-memcache -- with-php-config =/application/php/bin/php-config -- with-zlib-dirmakemake install # activate php_memcachedsed-I '$ a extension = memcache. so '/application/php/lib/php. inipkill php/application/php/sbin/php-fpm-t/application/php/sbin/php-fpm/application/php/bin/php-m | grep memcache
Check the current environment
View php modules
1. view the php module 2 [root @ web06 ~] #/Application/php/bin/php-m 3 [PHP Modules] 4 bcmath 5 Core 6 ctype 7 curl 8 date 9 dom10 ereg11 fileinfo12 filter13 ftp14 gd15 hash16 unzip json18 libxml19 mbstring20 mcrypt21 mhash22 mysql23 login openssl25 login pcre27 PDO28 login Login Phar31 posix32 Reflection33 session34 login SimpleXML36 soap37 sockets38 SPL39 login standard41 login using xml44 xmlreader45 login using forty 48 zlib49 50 [Zend Modules]View Code View php Module
Execution Process
Compile and install
[root@web06 memcache-2.2.5]# make installInstalling shared extensions: /application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/[root@web06 memcache-2.2.5]# ls /application/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so[root@web06 memcache-2.2.5]# sed -i '$a extension=memcache.so' /application/php/lib/php.ini[root@web06 memcache-2.2.5]# pkill php[root@web06 memcache-2.2.5]# /application/php/sbin/php-fpm -t[17-Nov-2017 11:39:13] NOTICE: configuration file /application/php-5.5.32/etc/php-fpm.conf test is successful[root@web06 memcache-2.2.5]# /application/php/sbin/php-fpm[root@web06 memcache-2.2.5]# /application/php/bin/php -m|grep memcachememcache
2.3.1 compile a test file
[root@web01 blog]# cat /application/nginx/html/blog/mc.php<?php $memcache = new Memcache; $memcache->connect('10.0.0.21', 11211) or die ("Could not connect"); $memcache->set('key20171117', 'hello,world'); $get_value = $memcache->get('key20171117'); echo $get_value;?>
Browser access
Database read Test
[root@cache01 ~]# printf "get key20171117 \r\n"|nc 10.0.0.21 11211 VALUE key20171117 0 11hello,worldEND
2.4 manage memcached on the web
Memadmin software used
Official Website: http://www.junopen.com/memadmin/
Put the package in a directory such as a site, and access the package through the browser.
[root@web06 tools]# tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/blog/
The default username and password are admin.
Add a new memcached Server
Web Interface Management in Chinese, relatively simple
2.5 memcached data cache
Program Implementation
2.5.1 implement memcached storage on the blog site
[root@web06 ~]# cat /application/nginx/html/blog/wp-content/object-cache.php
2.6 memcached session sharing
Method 1:
Through the program implementation, web01 only needs to write sessions to memcahce, and web02 reads sessions from memcahce(More universal)
Method 2:
Through the php configuration file, php stores the session in the file by default and changes it to the memcached
sed -i 's#session.save_handler = files#session.save_handler = memcache#;$a session.save_path = "tcp://10.0.0.21:11211"' /application/php/lib/php.ini
To use this function, you must use the php session function.
This article is from "miserable youth". You are welcome to reprint it. Please indicate the source for reprinting! Http://blog.znix.top