LNMP static and dynamic separation deploy and built memcache cache server

A, MemCache About
MemCache is a free, open-source, high-performance, distributed, distributed memory object caching system, used for dynamic Web applications in order to reduce the load on the database. It is to reduce the number of database read by caching data and objects in memory, thereby increasing the speed of the site visit. MemCaChe is a key-value pairs stored in the HashMap, data (such as strings, objects, etc.) to any memory used in the key-value store, data may be invoked from the database, the API calls, or page rendering results. MemCache design philosophy is small but powerful, its simple design facilitate rapid deployment, ease of development and solve the many problems facing large-scale data cache, and the open API makes MemCache for Java, C / C ++ / C #, Perl , Python, PHP, Ruby, and most popular programming language.
In addition, talk about why there Memcache and memcached two kind of names? In fact, Memcache is the name of the project (also the name of it when a client), but it is memcached server-side main program file name.

memcached is a key / value system, the system MySQL is much simpler, although MySQL also has a cache, but the SQL database of the resolution that is cost performance, query slower than memcached, another MySQL cache designed to be more complicated, because to consider the transaction log relative, storage engine modules, its performance is not good memcached.

memcached only do one thing, simple and efficient, stronger than MySQL in the cache, it should be easy to understand.

memcached distributed cache server as a high-speed operation, has the following characteristics:

  • Agreement simple;
  • Based on the libevent event processing;
  • Built-in memory storage;
  • memcached distributed not in communication with each other;

1, the protocol
server client communication memcached not use the XML format and the like, using a simple protocol based on the text line.
Therefore, the data can also be stored in memcached on through telnet, to obtain data.

2, event processing
libevent is a library, it will be Linux's epoll, BSD-like operating systems such as event handling kqueue packaged into a unified interface. Even if the number of connections to the server increases, the performance can also play a O (1) is. memcached use the libevent library, it can play its high performance on Linux, BSD, Solaris and other operating systems.

3, storage
in order to improve performance, the data stored in memcached memcached stored in the built-in memory storage space. Since the data exists only in memory, thus restart memcached, restart the operating system will cause all data disappear. Further, the content capacity after reaching the predetermined value, it is automatically deleted without the use of the cache based on LRU (Least Recently Used) algorithm. memcached server to cache itself is designed, and therefore did not give much thought to the issue of permanent data.

4, a distributed communication
memcached Although a "distributed" cache server, but the server is not distributed functionality. Each memcached not communicate with each other to share information. So, how distributed it? It all depends on the implementation of the client.

5, memcached application scenario
1) the front end of the cache database applications: it complicated to share the pressure data, when the data is updated, the program can notify the cache update
2 session sharing session shared memory)

6, memcached application workflow
it is a memory cache, you can read the data in the cache memory by way of the API, when a user needs to read the data, will first visit memcached cache, if the cache data directly returned to the application front-end, and if not, forwarded to back end server, then the server returns data to the user in addition, it will update the data to memcached caching.

If the actual production environment, the cache server need to restart (or off), then the data in the cache will be lost, this time to replace the server will be expanded concurrent pressure may lead to the introduction of server downtime also followed, not to provide services, so when our deal flow is as follows:
first, load balancing application from the WEB stopped in the - - -> make load balancing does not forward the data to the WEB - -> then start the cache server - - - -> through the database program initialized to the contents of the cache server - - - -> then enabled web applications - - - -> restart the database server
7, the consistency of memcached Hash algorithm
consistent Hash algorithm Key to Hash consistency through a data structure called the ring Hash cache server mapping. Briefly, consistent hashing the hash value of the entire space into a virtual ring (the cycloalkyl ring is called Hash consistency), the hypothesis space as a space hash function H 0 to 2 ^ 32 1 (i.e., a hash value is a 32-bit unsigned integer), the entire hash space is as follows:
LNMP static and dynamic separation deploy and built memcache cache server
the individual servers used for H a hash calculation, specifically using the server's IP address or host name as a key, so that each the machine can determine its position on the upper ring of the hash, and are arranged clockwise, where we assume that the position calculated memcache three nodes as follows:
LNMP static and dynamic separation deploy and built memcache cache server
Next, using the same algorithm to calculate a hash value h of the data, and thereby determine the location data on the hash ring. If we have data A, B, C, D, 4 objects, after hashing the following positions:
LNMP static and dynamic separation deploy and built memcache cache server
The consistent hash algorithm, it is bound to the data A on server01, D is bound to the server02, B , C on server03, is to find the nearest service node method in a clockwise.

这样得到的哈希环调度方法,有很高的容错性和可扩展性:
假设 server03 宕机:
LNMP static and dynamic separation deploy and built memcache cache server
可以看到此时 C、B 会受到影响,将 B、C 节点被重定位到 Server01。一般的,在一致性哈希算法中,如果一台服务器不可用,则受影响的数据仅仅是此服务器到其环空间中前一台服务器(即顺着逆时针方向行走遇到的第一台服务器)之间数据,其它不会受到影响。
考虑另外一种情况,如果我们在系统中增加一台服务器 Memcached Server 04:

LNMP static and dynamic separation deploy and built memcache cache server
此时 A、D、C 不受影响,只有 B 需要重定位到新的 Server04。一般的,在一致性哈希算法中,如果增加一台服务器,则受影响的数据仅仅是新服务器到其环空间中前一台服务器(即顺着逆时针方向行走遇到的第一台服务器)之间数据,其它不会受到影响。

综上所述,一致性哈希算法对于节点的增减都只需重定位环空间中的一小部分数据,具有较好的容错性和可扩展性。
一致性哈希的缺点:在服务节点太少时,容易因为节点分部不均匀而造成数据倾斜问题。我们可以采用增加虚拟节点的方式解决。
更重要的是,集群中缓存服务器节点越多,增加/减少节点带来的影响越小,很好理解。换句话说,随着集群规模的增大,继续命中原有缓存数据的概率会越来越大,虽然仍然有小部分数据缓存在服务器中不能被读到,但是这个比例足够小,即使访问数据库,也不会对数据库造成致命的负载压力。
二、部署LNMP动静分离&&memcache缓存服务器
环境如下:
LNMP static and dynamic separation deploy and built memcache cache server
所需源码包可在此处下载并上传至各服务器https://pan.baidu.com/s/1-2pS702mz41e94nBXSgUnA
提取码:rldk

1、部署Nginx服务

[root@nginx /]# yum -y install openssl-devel pcre-devel     # 安装所需依赖包 
[root@nginx /]# mkdir nginx           # 个人习惯而已
[root@nginx /]# cd nginx/
[root@nginx nginx]# rz          # 使用的xshell连接的服务器,使用rz上传所需的源码包
[root@nginx nginx]# tar zxf nginx-1.14.0.tar.gz       # 解压到当前目录
[root@nginx nginx]# cd nginx-1.14.0/
[root@nginx nginx-1.14.0]# useradd -M -s /sbin/nologin www          # 创建Nginx运行用户
[root@nginx nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=www --group=www && make && make install         # 编译并安装
[root@nginx nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/         # 创建命令软连接
[root@nginx nginx-1.14.0]# nginx          # 启动服务
[root@nginx nginx-1.14.0]# netstat -anput | grep 80        # 查看端口,确定服务已经启动
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6827/nginx: master 
[root@nginx nginx-1.14.0]# cd /
[root@nginx /]# vim /usr/local/nginx/conf/nginx.conf
...............................               // 省略部分内容
在server{} 字段中添加如下内容
location ~ \.php$ {
            root           /var/www/html;            # 指定PHP的网页存放路径
            fastcgi_pass   192.168.171.133:9000;           # 指定PHP服务监听端口及地址
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

[root@nginx /]# nginx -s reload              # 重启服务使配置生效

2, deploying PHP services

#首先需要为PHP安装依赖包
[root@php php]# yum -y install libxml2-devel openssl-devel bzip2-devel
[root@php php]# tar zxf libmcrypt-2.5.7 
[root@php php]# cd libmcrypt-2.5.7/
[root@php libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
[root@php libmcrypt-2.5.7]# cd ..
[root@php php]# tar zxf php-5.6.27.tar.gz 
[root@php php]# cd php-5.6.27/
[root@php php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install

#以下为调整PHP的配置文件及控制服务的启停
[root@php php-5.6.27]# cp php.ini-production /etc/php.ini    #复制源码中中提供的PHP配置文件
[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#复制其服务控制脚本文件
[root@php php-5.6.27]# chmod +x /etc/init.d/php-fpm    #赋予执行权限
[root@php php-5.6.27]# chkconfig --add php-fpm    #添加为系统服务,以便支持systemctl管理
[root@php php-5.6.27]# chkconfig php-fpm on   #开启
#复制php-fpm提供的默认配置文件并编辑它
[root@php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@php php-5.6.27]# vim /usr/local/php5.6/etc/php-fpm.conf
listen = 192.168.171.133:9000    #监听地址是本机的IP9000端口
pm.max_children = 50         #最大启动的进程数
pm.start_servers = 5          #初始启动进程数
pm.min_spare_servers = 5     #最小空闲进程
pm.max_spare_servers = 35     #最大空闲进程
#修改完成后,保存退出即可
[root@php /]# service php-fpm restart              # 重启PHP使配置生效
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@php /]# netstat -anput | grep 9000                  # 查看是否运行
tcp        0      0 192.168.171.133:9000    0.0.0.0:*               LISTEN      3054/php-fpm: maste 
# 准备网页测试文件
[root@php /]# mkdir -p /var/www/html
[root@php /]# cd /var/www/html/
[root@php html]# cat index.php 
<?php
phpinfo();
?>
[root@php html]# cat index1.php 
<?php
$link=mysqli_connect('192.168.171.135','zyz','pwd@123');
if($link) echo "恭喜你,数据库连接成功!!!"; else echo "connect shibai";
mysqli_close($link);
?>

With this, you can access the 80-port server Nginx to view two pages php files defined on the server (in the script file access connection to the database, you need to deploy a database, and create a user to connect):
3, deploy MySQL service

# 这里部署一个简单的数据库即可
[root@mysql /]# mkdir -p mysql
[root@mysql /]# cd mysql/
[root@mysql mysql]# rz             # 上传所需
[root@mysql mysql]# sh mysql.sh          # 直接sh执行脚本安装即可,安装完毕之后默认密码是123
Starting MySQL.. SUCCESS! 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql mysql]# netstat -anput | grep 3306            # 确保服务已经启动
tcp6       0      0 :::3306                 :::*                    LISTEN      3290/mysqld       
[root@mysql mysql]# mysql -u root -p        # 登录数据库
Enter password: 
mysql> create database bbs;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on bbs.* to zyz@"192.168.171.%" identified by 'pwd@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

Test Validation:
LNMP static and dynamic separation deploy and built memcache cache server
LNMP static and dynamic separation deploy and built memcache cache server
4, deploy Memcached service

[root@memcached /]# mkdir memcached
[root@memcached /]# cd memcached/
[root@memcached memcached]# rz           # 上传所需源码包
[root@memcached memcached]# tar zxf libevent-2.0.22-stable.tar.gz           # 解包
[root@memcached memcached]# cd libevent-2.0.22-stable/
[root@memcached libevent-2.0.22-stable]# ./configure && make && make install              # 编译并安装
[root@memcached libevent-2.0.22-stable]# cd ..
[root@memcached memcached]# tar zxf memcached-1.4.33.tar.gz 
[root@memcached memcached]# cd memcached-1.4.33/
[root@memcached memcached-1.4.33]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local
/ && make && make install 
[root@memcached memcached-1.4.33]# ln -s /usr/local/memcached/bin/memcached /usr/local/bin/            # 命令制作软连接
[root@memcached memcached-1.4.33]# memcached -d -m 1024 -l 192.168.171.132 -p 11211 -c 10240 -P /usr/local/memcached/memcached.pid -u root
#启动memcached服务,上述启动参数说明如下:
# -d 选项是启动一个守护进程。
# -m 分配给 Memcache 使用的内存数量,单位是 MB,默认 64MB。
# -l 监听的 IP 地址。(默认:INADDR_ANY,所有地址)
# -p 设置 Memcache 的 TCP 监听的端口,最好是 1024 以上的端口。
# -u 运行 Memcache 的用户,如果当前为 root 的话,需要使用此参数指定用户。
# -c 选项是最大运行的并发连接数,默认是 1024。
# -P 设置保存 Memcache 的 pid 文件路径。
# -M 内存耗尽时返回错误,而不是删除项
# -f 块大小增长因子,默认是 1.25
# -n 最小分配空间,key+value+flags 默认是 48
# -h 显示帮助
[root@memcached memcached-1.4.33]# netstat -anput | grep 11211              # 确定TCP及udp都在监听
tcp        0      0 192.168.171.132:11211   0.0.0.0:*               LISTEN      11666/memcached     
udp        0      0 192.168.171.132:11211   0.0.0.0:*                           11666/memcached    

5, deployment Memcache client (PHP server operating return)

[root@php /]# mkdir memcache
[root@php /]# cd memcache/
[root@php memcache]# rz      # 上传如下源码包
[root@php memcache]# ls
memcache-3.0.8.tgz
[root@php memcache]# tar zxf memcache-3.0.8.tgz 
[root@php memcache]# cd memcache-3.0.8/
[root@php memcache-3.0.8]# /usr/local/php5.6/bin/phpize       #  #执行该命令,以便生成configure文件  
# 若在执行上述命令时报错,则需要执行“yun -y install autoconf "安装提示的autoconf包。
Configuring for:          # 执行成功会显示次此几行
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@php memcache-3.0.8]# ./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config && make && make install             # 编译并安装
# 执行完上述命令后,会显示memcache.so存放的路径
[root@php memcache-3.0.8]# vim /etc/php.ini       # 编辑此文件
# 在最后一行添加如下内容,注意不要直接复制本人的路径
extension = /usr/local/php5.6/lib/php/extensions/no-debug-non-zts-20131226/memcache.so
[root@php memcache-3.0.8]# service php-fpm restart          # 重启php使配置生效
Gracefully shutting down php-fpm . done
Starting php-fpm  done

Write a test file:

[root@php memcache-3.0.8]# cd /var/www/html/
[root@php html]# vim test1.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.171.132', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 600) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>";
var_dump($get_result);
?>
#编辑完成后,保存退出即可,此测试脚本是显示memcached的版本
#并且向里面插入了一个缓存时间为600秒的键值对“test=123”,其ID为“key”

Client Access test1.php edited file, you see the following:
LNMP static and dynamic separation deploy and built memcache cache server
Installing Telnet command on the memcached server, and log the cache database, see if you can get their key-value pairs

[root@memcached /]# yum -y install telnet        # 安装Telnet命令
[root@memcached /]# telnet 192.168.171.132 11211
Trying 192.168.171.132...
Connected to 192.168.171.132.
Escape character is '^]'.
get key              # 查询ID为“key”的键值对,可以看到我们测试脚本写入的“test=123”
VALUE key 1 66
O:8:"stdClass":2:{s:8:"str_attr";s:4:"test";s:8:"int_attr";i:123;}
END
#在进行上面的get验证时,需要将test1.php文件中插入的键值对的保存时间值改大一些
#或者重新访问一下,以免缓存失效,查询不到

So far, LNMP static and dynamic separation && memcache cache server deployment has been basically completed, Next, configure PHP with memcached server communication sessions to save the session
6, using memcache session to achieve shared (to do the following in PHP server)

[root@php /]# vim /etc/php.ini 
# 在末尾添加如下内容
session.save_handler = memcache
session.save_path = "tcp://192.168.171.132:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
# 内容解释如下:
# session.save_handler:设置 session 的储存方式为 memcache 。
#默认以文件方式存取 session数据。
#session.save_path: 设置 session 储存的位置
#使用多个 memcached server 时用逗号”,”隔开,
#可以带额外的参数”persistent”、”weight”、”timeout”、”retry_interval”等等,
#类似这样的:"tcp://host:port?persistent=1&weight=2,tcp://host2:port2"
[root@php /]# service php-fpm restart          # 重启服务使配置生效
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@php /]# vim /var/www/html/test2.php     # 编写配置文件
<?php
session_start();
if (!isset($_SESSION['session_time']))
{
$_SESSION['session_time'] = time();
}
echo "session_time:".$_SESSION['session_time']."<br />";
echo "now_time:".time()."<br />";
echo "session_id:".session_id()."<br />";
?>

Client Access test2.php written test file, as follows:
LNMP static and dynamic separation deploy and built memcache cache server
Again, using the Telnet command to query the value of its session_id in the memcached server, as follows:

[root@memcached /]# telnet 192.168.171.132 11211
Trying 192.168.171.132...
Connected to 192.168.171.132.
Escape character is '^]'.
get d1n4umig3aq8okqrg7ep95t321
VALUE d1n4umig3aq8okqrg7ep95t321 0 26
session_time|i:1581082210;
END
#可以看到,查询到的session_time和我们网页访问到的值是一样的,说明其被缓存了

7, test Memcached caching database
to create a table for testing on MySQL database (all operations on the MySQL database) as follows:

[root@mysql mysql]# mysql -u root -p
Enter password: 
mysql> create database testdb;      # 创建数据库
mysql> use testdb;      # 进入库中
Database changed
mysql> create table test1(id int not null auto_increment,name varchar(20) default null,primary key (id)) engine=innodb auto_increment=1 default charset=utf8;       # 创建表
mysql> insert into test1(name) values ('aaa1'),('aaa2'),('aaa3'),('aaa4'),('aaa5');       # 向表中添加内容
mysql> select * from test1       # 查询表中内容
    -> ;
+----+------+
| id | name |
+----+------+
|  1 | aaa1 |
|  2 | aaa2 |
|  3 | aaa3 |
|  4 | aaa4 |
|  5 | aaa5 |
+----+------+
5 rows in set (0.00 sec)
mysql> desc test1;       # 可查询表结构
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> grant select on testdb.* to test@'%' identified by 'pwd@123';
# 创建用于测试的用户

Write the following test PHP files on a server, test if memcache cache data successfully:

[root@php html]# vim test3.php
<?php
$memcachehost = '192.168.171.132';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from test1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysql_connect("192.168.171.135","test","pwd@123");
mysql_select_db(testdb);
$result=mysql_query($query);
while ($row=mysql_fetch_assoc($result))
{
$arr[]=$row;
}
$f = 'mysql';
$memcache->add($key,serialize($arr),0,30);
$data = $arr ;
}
else{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
}
echo $f;
echo "<br>";
echo "$key";
echo "<br>";
//print_r($data); 
foreach($data as $a)
{
echo "number is <b><font color=#FF0000>$a[id]</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$a[name]</font></b>";
echo "<br>";
}
?>

Client Access for test script file, the page is first accessed as follows:
LNMP static and dynamic separation deploy and built memcache cache server
the client refreshes, you will see the following pages:
LNMP static and dynamic separation deploy and built memcache cache server
in front of the query to the cache expires, you can get to the corresponding cache data get in the memcache, It follows (operating on the server memcache):

[root@memcached memcached]# telnet 192.168.171.132 11211
Trying 192.168.171.132...
Connected to 192.168.171.132.
Escape character is '^]'.
get d8c961e9895ba4b463841924dbcefc2b
VALUE d8c961e9895ba4b463841924dbcefc2b 0 251
a:5:{i:0;a:2:{s:2:"id";s:1:"1";s:4:"name";s:4:"aaa1";}i:1;a:2:{s:2:"id";s:1:"2";s:4:"name";s:4:"aaa2";}i:2;a:2:{s:2:"id";s:1:"3";s:4:"name";s:4:"aaa3";}i:3;a:2:{s:2:"id";s:1:"4";s:4:"name";s:4:"aaa4";}i:4;a:2:{s:2:"id";s:1:"5";s:4:"name";s:4:"aaa5";}}
END

Guess you like

Origin blog.51cto.com/14227204/2469699