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