Memcached服务器端与PHP-Memcache客户端安装配置_服务器应用_Linux公社-Linux系统门户网站
https://www.linuxidc.com/Linux/2017-11/148428.htmlinux
前端应用服务器或动态或静态,对数据库进行或读取,或写入的操做时。数据库扛不住这么大量查询请求后端真实数据库,因为咱们通常后端数据库采用的都是关系型数据库,属于写入到磁盘里的,磁盘的i/o很容易就达到瓶颈。大大的影响了速度web
因此咱们能够在数据库的前端搭建memcached,memcached是缓存数据库,不支持数据持久化,服务器重启,或者服务重启,内存数据所有丢失。可是近年来有一些技术大牛开发了一些memcached数据持久化的一些技术,如感兴趣可自行搜索,本案例主要是memcached案例算法
memcached高性能内存对象缓存系统,键值对存储方式,服务端默认经过11211端口工做,该缓存程序是典型的C/S架构,服务端是C语言开发的,客户端可使用多种语言开发数据库
缓存的数据量大道指定值或者过时以后基于LRU算法自动删除不是用的缓存。删除“最近最少使用”的记录的机制;当memcached指定的内存空间不足时,从其最近未被使用的记录中搜索,并将其空间分配给新的记录。能够禁止LRU功能,内存用尽,程序返回错误提示vim
主机名 | IP地址 | 角色信息 |
---|---|---|
web | 192.168.111.3 | 该主机上部署telnet程序进行测试 |
memcached1 | 192.168.111.4 | 该主机只部署memcached,magent,keepalived |
memcached2 | 192.168.111.5 | 该主机只部署memcached,magent,keepalived |
VIP | 192.168.111.100 | magent的VIP |
[root@localhost ~]# yum -y install telnet
两台机器同样 [root@memcached1 ~]# yum -y install libevent [root@memcached1 ~]# rpm -ql libevent-2.0.21-4.el7.x86_64 /usr/lib64/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5.1.9 /usr/lib64/libevent_core-2.0.so.5 /usr/lib64/libevent_core-2.0.so.5.1.9 /usr/lib64/libevent_extra-2.0.so.5 /usr/lib64/libevent_extra-2.0.so.5.1.9 /usr/lib64/libevent_openssl-2.0.so.5 /usr/lib64/libevent_openssl-2.0.so.5.1.9 /usr/lib64/libevent_pthreads-2.0.so.5 /usr/lib64/libevent_pthreads-2.0.so.5.1.9 /usr/share/doc/libevent-2.0.21 /usr/share/doc/libevent-2.0.21/ChangeLog /usr/share/doc/libevent-2.0.21/LICENSE /usr/share/doc/libevent-2.0.21/README [root@memcached1 ~]# yum -y install memcached [root@memcached1 ~]# memcached -d -m 32m -p11211 -u root [root@memcached1 ~]# netstat -lnpt | grep 11211 tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 58821/memcached tcp6 0 0 :::11211 :::* LISTEN 58821/memcached #-d:以守护进程方式启动 #-m:分配给memcached内存使用量,默认为64m #-u:运行该程序的用户 #-p:指定开放那个端口提供服务 #mkdir magent [root@memcached1 ~]# tar zxf magent-0.5.tar.gz -C magent/ [root@memcached1 ~]# vim ketama.h #ifndef SSIZE_MAX #define SSIZE_MAX 32767 #endif #添加到文件开始 [root@memcached1 ~]# sed -i '1 s/$/ -lm/' Makefile [root@memcached1 ~]# head -1 Makefile LIBS = -levent -lm #tar zxf libevent* #cd libevent* #./configure --prefix=/usr/ && make && make install [root@memcached1 magent]# scp magent 192.168.111.5:/usr/bin/ [root@memcached1 magent]# cp magent /usr/bin/ [root@memcached1 magent]# chmod +x /usr/bin/magent #对端机器同样加执行权限
#yum -y install keepalived cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } script_user root #须要制定脚本运行用户 notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script check_m { script "/opt/magent.sh" interval 2 weight 20 } vrrp_instance VI_1 { state MASTER interface ens32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.111.100/32 dev ens32 label ens32:2 } track_script { check_m } #这个配置要包括到"vrrp_instance"中去 } memcached2: [root@memcached2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } script_user root #须要制定脚本运行用户 notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVELa vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script check_m { script "/opt/magent.sh" interval 2 weight 20 } vrrp_instance VI_1 { state BACKUP interface ens32 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.111.100/32 dev ens32 label ens32:2 } track_script { check_m } #这个配置要包括到"vrrp_instance"中去 } [root@memcached1 ~]# systemctl start keepalived.service [root@memcached1 ~]# ip a | grep ens32:2 inet 192.168.111.100/32 scope global ens32:2 编写监控magent使用到的脚本 主: [root@memcached1 ~]# vim /opt/magent.sh #!/bin/bash k=`ps -ef |grep keepalived |grep -v grep |wc -l` if [ $k -gt 0 ];then magent -u root -n 51200 -l 192.168.111.100 -p 12000 -s 192.168.111.4:11211 -b 192.168.111.5:11211 #-u指定运行magent程序的用户;-n最大链接数,默认4096;-lmagent对外坚挺ip;-pmagent对外监听端口;-smemcached朱缓存IP地址;-bmemcached被缓存IP地址 else pkill -9 magent fi 备: [root@memcached2 ~]# vim /opt/magent.sh #!/bin/bash k=`ip a |grep 192.168.111.100 |wc -l` if [ $k -gt 0 ];then magent -u root -n 51200 -l 192.168.111.100 -p 12000 -s 192.168.111.4:11211 -b 192.168.111.5:11211 #若是VIP地址转移到本机了,启动magent程序,不然magent出于关闭状态 else pkill -9 magent fi [root@memcached1 ~]# chmod +x /opt/magent.sh [root@memcached1 ~]# systemctl restart keepalived.service [root@memcached1 ~]# ps aux | grep magent root 71271 0.0 0.0 11948 336 ? Ss 18:58 0:00 magent -u root -n 51200 -l 192.168.111.100 -p 12000 -s 192.168.111.4:11211 -b 192.168.111.5:11211 #程序已经运行,此时对端该程序没有运行 [root@memcached1 ~]# netstat -lnpt | grep 12000 tcp 0 0 192.168.111.100:12000 0.0.0.0:* LISTEN 71271/magent
主上关掉keepalived,即magent没有了对外监听的ip: [root@memcached1 ~]# systemctl stop keepalived.service 从上查看: [root@memcached2 ~]# ps aux | grep magent root 67100 0.0 0.0 11948 340 ? Ss 04:46 0:00 magent -u root -n 51200 -l 192.168.111.100 -p 12000 -s 192.168.111.4:11211 -b 192. 168.111.5:11211root 67254 0.0 0.0 112720 984 pts/0 R+ 04:47 0:00 grep --color=auto magent [root@memcached2 ~]# ip a| grep ens32:2 inet 192.168.111.100/32 scope global ens32:2
[root@localhost ~]# telnet 192.168.111.100 12000 Trying 192.168.111.100... Connected to 192.168.111.100. Escape character is '^]'. set test 0 0 5 nihao STORED get test VALUE test 0 5 nihao END 如今直接链接真实memcached数据库看是否有数据 [root@localhost ~]# telnet 192.168.111.4 11211 Trying 192.168.111.4... Connected to 192.168.111.4. Escape character is '^]'. get test VALUE test 0 5 nihao END quit Connection closed by foreign host. [root@localhost ~]# telnet 192.168.111.5 11211 Trying 192.168.111.5... Connected to 192.168.111.5. Escape character is '^]'. get test VALUE test 0 5 nihao END