2020年 · Linux

memcached-1.4.13 + repcached主从自动安装脚本

适用场景:因业务需要,可以实现cache冗余,避免因cache服务器down掉而导致数据丢失
repcached是memcached的补丁,实现了不同memcached实例内存数据同步的功能,类似于实现了主从的功能,但是每个memcached都是可以写入的,即可以双向复制主从的都是可读可写的,所以也不是简单的主从结构,更像是多主互从的结构,通过repcachd可以解决memcached单机内存数据不能持久化的问题,因为可以实现memcached主从结构,项目地址:http://repcached.sourceforge.net/
测试环境:
[root@localhost ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.5.1804 (Core)
Release: 7.5.1804
Codename: Core

#!/bin/bash
# Author:Jian
# Date:2017-11-16
# Version:0.1
# Description: 自动安装memcached-1.4.13并自动打上对应版本的repcached-1.4.13补丁,适用于CentOS64位系统,在CentOS6/7上通过测试
url1=”http://memcached.org/files/old/memcached-1.4.13.tar.gz”
url2=”http://mdounin.ru/files/repcached-2.3.1-1.4.13.patch.gz”
url3=”https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz”
string=`cat /etc/redhat-release`
vers=`echo ${string##*release}|sed ‘s/^[[:space:]]*//g’|awk -F”.” ‘{print $1}’`
download_dir=/usr/local/src/

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin:/usr/local/memcached/bin

if which memcached &>/dev/null ; then
echo -e “\033[31mYou have installed memcached on your system,please uninstall it first!\033[0m”
exit 1
fi

check_user() {
if [ $UID -ne 0 ] ;then
echo -e “\033[31mYou must be root to run this script!\033[0m” 1>&2
exit 1
fi
}
install_progs() {
for prog in `echo $@`
do
if ! which $prog &>/dev/null; then
echo “Installing $prog …”
yum install $prog -y &>/dev/null
if [ $? -eq 0 ];then
echo “Install $prog OK”
else
echo “Download $prog failed,exit”
exit 2
fi
fi
done
}

download() {
cd $download_dir
for url in `echo $@`
do
if [ ! -e ${download_dir}${url##*/} ];then
echo “Downloading ${url##*/} …”
if wget -q -t 8 -c -T 3 $url ;then
echo “Download ${url##*/} OK.”
else
echo -e “\033[31mDownload ${url##*/} failed.\033[0m”
fi
else
echo “File ${download_dir}${url##*/} exists.”
fi
done
}

# libevent-2.1.8-stable.tar.gz
install_libevent() {
cd $download_dir
if [ -f /usr/local/libevent/lib/libevent-2.1.so.6 ];then
echo “Libevent has installed on your system”
else
echo “Installing libevent…”
tar xf ${url3##*/} &>/dev/null
cd $(echo “${url3##*/}” |sed ‘s/\.tar\.gz//g’)
./configure –prefix=/usr/local/libevent
make && make install
echo “/usr/local/libevent/lib” > /etc/ld.so.conf.d/libevent.conf
ldconfig
fi

}

install_memcached() {
cd $download_dir
tar xf ${url1##*/} &>/dev/null
gzip -d -c ${download_dir}${url2##*/} > ${download_dir}repcached-2.3.1-1.4.13.patch
cd $(echo “${url1##*/}” |sed ‘s/\.tar\.gz//g’)
patch -p1 <../repcached-2.3.1-1.4.13.patch
./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent –enable-replication –enable-64bit
make && make install
echo ‘export PATH=/usr/local/memcached/bin:$PATH’ > /etc/profile.d/memcached.sh
source /etc/profile.d/memcached.sh
}

check_user
install_progs wget gcc
download $url1 $url2 $url3
if [ -f ${download_dir}${url1##*/} ] && [ -f ${download_dir}${url2##*/} ] && [ -f ${download_dir}${url3##*/} ];then
install_libevent
install_memcached
else
echo “There is a lack of necessary installation packages,please check.”
echo “You can download the package manually and put the package into: /usr/local/src”
exit 1
fi
if [ -f /usr/local/memcached/bin/memcached ];then
echo -e “\033[32mCongratulations,you have successfuly installed memcached!\033[0m”
cd $download_dir
rm -rf ${download_dir}$(echo “${url1##*/}” |sed ‘s/\.tar\.gz//g’)
rm -rf ${download_dir}$(echo “${url3##*/}” |sed ‘s/\.tar\.gz//g’)
else
echo -e “\033[31mInstalled memcached failed,please check!\033[0m”
fi
echo ‘PORT=”11211″‘ > /etc/sysconfig/memcached
echo ‘MAXCONN=”1024″‘ >> /etc/sysconfig/memcached
echo ‘CACHESIZE=”64″‘ >> /etc/sysconfig/memcached
echo ‘OPTIONS=””‘ >> /etc/sysconfig/memcached

if id -u memcached &>/dev/null || id -g memcached &>/dev/null;then
echo -e “\033[31mUser or group ‘memcached’ exists and now use ‘nobody’as a user\033[0m”
echo ‘USER=”nobody”‘ >> /etc/sysconfig/memcached
else
groupadd -r memcached
useradd -r -s /sbin/nologin -g memcached memcached
echo ‘USER=”memcached”‘ >> /etc/sysconfig/memcached
chown -R memcached:memcached /usr/local/memcached
fi
exit 0

########################################################
# 下面分为CentOS6、CentOS7提供的服务脚本 #
########################################################
###centos6服务脚本,将其写入/etc/rc.d/init.d/memcached文件###

#!/bin/bash
#
# Init file for memcached
#
# chkconfig: – 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached

. /etc/rc.d/init.d/functions
. /etc/sysconfig/memcached

## Default variables

RETVAL=0
prog=”/usr/local/memcached/bin/memcached”
desc=”Distributed memory caching”
lockfile=”/var/lock/subsys/memcached”

start() {
echo -n $”Starting $desc (memcached): ”
daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS
RETVAL=$?
[ $RETVAL -eq 0 ] && success && touch $lockfile || failure
echo
return $RETVAL
}

stop() {
echo -n $”Shutting down $desc (memcached): ”
killproc $prog
RETVAL=$?
[ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure
echo
return $RETVAL
}

restart() {
stop
start
}

reload() {
echo -n $”Reloading $desc ($prog): ”
killproc $prog -HUP
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
echo
return $RETVAL
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e $lockfile ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $”Usage: $0 {start|stop|restart|condrestart|status}”
RETVAL=1
esac
exit $RETVAL

###centos7服务脚本,将其写入/usr/lib/systemd/system/memcached.service文件###

[Unit]
Description=Memcached
Before=httpd.service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/local/memcached/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

[Install]
WantedBy=multi-user.target

####################################################################
启动master:
[root@localhost ~]# /usr/local/memcached/bin/memcached -v -l 192.168.31.7 -p 11211 -uroot
replication: listen
replication: accept
启动salve:
[root@localhost ~]# /usr/local/memcached/bin/memcached -v -l 192.168.31.67 -p 11211 -uroot -x 192.168.31.7 -X 11212
replication: connect (peer=192.168.31.7:11212)
replication: marugoto copying
replication: start

测试:
[root@localhost ~]# telnet 192.168.31.7 11211
Trying 192.168.31.7…
Connected to 192.168.31.7.
Escape character is ‘^]’.
set key1 0 0 18
blog.ohyeahwoo.com
STORED
get key1
VALUE key1 0 18
blog.ohyeahwoo.com
END
quit
Connection closed by foreign host.
[root@localhost ~]# telnet 192.168.31.67 11211
Trying 192.168.31.67…
Connected to 192.168.31.67.
Escape character is ‘^]’.
get key1
VALUE key1 0 18
blog.ohyeahwoo.com
END

模拟宕机测试,先插入数据到master  然后KILL掉master进程,在slave上GET key2是否能正常取出数据:
[root@localhost ~]# telnet 192.168.31.7 11211
Trying 192.168.31.7…
Connected to 192.168.31.7.
Escape character is ‘^]’.
set key2 0 0 4
blog
STORED
Connection closed by foreign host.    #此处模拟master宕机断开连接
[root@localhost ~]# telnet 192.168.31.67 11211
Trying 192.168.31.67…
Connected to 192.168.31.67.
Escape character is ‘^]’.
get key2
VALUE key2 0 4
blog
END

memcached启动参数说明:
-p:监听的tcp端口默认为11211
-l:使用那个IP启动服务
-d:作为守护进程运行
-u:指定启动用户
-m:指定使用内存的最大值
-M:内存用光时报错(而非使用LRU 算法清除内存)
-c:最大并发连接默认为1024
-v:打印提示信息
-vv:详细信息
-vvv:超详细信息
-P:保存进程Id到一个文件,与-d结合使用有意义
-R:每个连接可处理的最大请求
-x:主从复制中指定监听的master IP
-X:主从复制中指定监听的master 端口

参考:https://blog.51cto.com/11829889/1982597
https://www.cnblogs.com/you0329/p/8591059.html