1. 安装环境准备
1.1 主机环境准备
1.1.1. 关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
1.1.2. 软件下载
软件版本:memcached-1.6.6.tar.gz 下载地址:http://memcached.org/downloads
1.1.3. 部署规划
软件安装路径: /usr/local/memcache 端口规划: 11222
1.1.4. 系统主机时间、时区、系统语言
本节视实际情况需要操作
修改时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改系统语言环境
echo 'LANG="en_US.UTF-8"' >> /etc/profile && source /etc/profile
配置主机NTP时间同步
yum -y install ntp
systemctl enable ntpd && systemctl start ntpd
echo 'server ntp1.aliyun.com' >> /etc/ntp.conf
echo 'server ntp2.aliyun.com' >> /etc/ntp.conf
2. Memcached安装部署
2.1 Memcached依赖安装及部署
添加用户与用户组(用户名请自行定义)
groupadd -r middle && useradd -s /sbin/nologin -r -M -g middle middle
下载memcached-1.6.6.tar.gz安装包,并解压安装
yum -y install gcc gcc-c++ automake autoconf libevent-devel libevent make wget
cd /opt
wget http://memcached.org/files/memcached-1.6.6.tar.gz
tar -zxvf memcached-1.6.6.tar.gz
cd memcached-1.6.6
./configure --prefix=/usr/local/memcached
make && make install
Memcached启动命令
/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -m 256 -u middle -p 11222 -c 1024 -P /var/run/memcached.pid
Memcached启动选项简介 -d是启动一个守护进程; -m是分配给Memcached使用的内存数量,单位是MB; -u是运行Memcached的用户; -l是监听的服务器IP地址,可以有多个地址; -p是设置Memcached监听的端口,,最好是1024以上的端口; -c是最大运行的并发连接数,默认是1024; -P是设置保存Memcached的pid文件。
2.2 配置memcached系统服务
2.2.1. 针对6系统添加系统服务
1、添加防火墙策略 (1)所有机器可访问
iptables -A INPUT -p tcp --dport 11222 -j ACCEPT
service iptables save
(2)特定IP192.168.142.166可访问本机11222端口
iptables -A INPUT -p tcp -s 192.168.142.166 --dport 11222 -j ACCEPT
service iptables save
2、添加memcached系统服务启动脚本
vi /etc/init.d/memcached
#!/bin/bash
#
# memcached start/stop the memcached daemon
#
# chkconfig: 345 81 19
# description: memcached is a memory cache server.
#
prog="memcached"
exec=/usr/local/memcached/bin/memcached
CMD="$exec -u middle -d -m 256 -p 11222 -c 1024 -P /tmp/memcached.pid"
lockfile=/var/lock/subsys/memcached
PID=`ps -ef|grep -v grep |grep memcached |awk '{print $2}'`
# source function library.
. /etc/rc.d/init.d/functions
start() {
if [ $UID -ne 0 ]; then
echo "User has insufficient privilege."
exit 4
fi
[ -x $exec ] || exit 5
echo -n $"starting $prog: "
daemon $CMD
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
}
stop() {
if [ $UID -ne 0 ]; then
echo "User has insufficient privilege."
exit 4
fi
kill -9 $PID
retval=$?
echo -n $"Stopping $prog: "
if [ $retval -ne 0 ]; then
failure $"stopping $prog"
else
echo
[ $retval -eq 0 ] && rm -f $lockfile
fi
}
restart() {
stop
start
}
rh_status() {
# run checks to determine if the service is running or use generic status
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
"start")
rh_status_q && exit 0
$1
;;
"stop")
rh_status_q || exit 0
$1
;;
"restart")
rh_status_q || exit 7
$1
;;
"status")
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
;;
esac
exit $?
2、配置memcached系统服务及自启动
chmod +x /etc/init.d/memcached
chkconfig --add memcached && chkconfig memcached on
chkconfig --list memcached
3、启动与停止memcached服务
service memcached start
ps -ef|grep memcached
service memcached stop
2.2.2. 针对7系统添加系统服务
1、添加防火墙策略 (1)所有机器可访问
firewall-cmd --permanent --zone=public --add-port=11222/tcp
firewall-cmd --reload
(2)特定IP192.168.142.166可访问本机11222端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11222" accept"
firewall-cmd --reload
(3)特定IP段192.168.142.0/24可访问本机11222端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.0/24" port protocol="tcp" port="11222" accept"
firewall-cmd --reload
2、添加memcached系统服务启动脚本
cat >/usr/lib/systemd/system/memcached.service<<EOF
[Unit]
Description=memcached
Requires=network-online.target
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/memcached/bin/memcached -l 127.0.0.1 -m 256 -p 11222 -c 1024 -P /tmp/memcached.pid
User=middle
Group=middle
[Install]
WantedBy=multi-user.target
EOF
2、配置memcached系统服务及自启动
systemctl daemon-reload
systemctl enable memcached.service
3、启动与停止memcached服务
systemctl start memcached
ps -ef|grep memcached
systemctl stop memcached
3. Memcached加固
3.1 最小化权限用户启动
groupadd -r middle && useradd -s /sbin/nologin -r -M -g middle middle
/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -m 256 -u middle -p 11222 -c 1024 -P /var/run/memcached.pid
3.2 禁止监听在公网
Memcached启动时指定绑定的IP地址为 127.0.0.1。
/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -m 256 -u middle -p 11222 -c 1024 -P /var/run/memcached.pid
3.3 修改默认11211端口
默认情况下,memcached默认使用11211端口,请修改默认监听端口,如本文档使用的是11222
/usr/local/memcached/bin/memcached -d -l 127.0.0.1 -m 256 -u middle -p 11222 -c 1024 -P /var/run/memcached.pid
3.4 定期升级
使用官方最新稳定版本
4. Memcached优化
4.1 优化内核参数
cat >>/etc/sysctl.conf<<EOF
fs.file-max = 6815744
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 10000
net.core.somaxconn=4000
net.ipv4.tcp_syncookies = 1
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
EOF
sysctl -p
4.2 系统资源限制
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65525
* hard nofile 65525
* soft nproc 65525
* hard nproc 65525
EOF
5. 报错处理
报错提示:
checking for libevent directory... configure: error: libevent is required. You can get it from https://www.monkey.org/~provos/libevent/
If it's already installed, specify its path using --with-libevent=/dir/
报错原因:memcached安装依赖libevent库 解决方案:安装libevent库
1)直接yum安装:
yum -y install libevent libevent-devel
如果没有可用安装包,可配置阿里云
cd /etc/yum.repos.d/
wget http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache fast
yum -y install libevent libevent-devel
2)使用源码包安装
(1)下载安装包:https://libevent.org/
wget https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
tar -zxvf libevent-2.1.21-stable.tar.gz
cd libevent-2.1.21-stable
./configure --prefix=/usr/local/libevent
make && make install
cd memcached-1.6.6
./configure --prefix=/usr/local/memcached
make && make install