操作系统:CentOS 6.x 64位
实现目的:安装部署Memcached服务器
一、防火墙设置
vi /etc/sysconfig/iptables #编辑防火墙配置文件,添加下面代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
:wq! #保存退出
service iptables restart #最后重启防火墙使配置生效
二、关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
三 、系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
四、下载软件包
1、下载libevent
2、下载memcached
http://www.memcached.org/files/memcached-1.4.25.tar.gz
五、安装编译工具包
yum install -y apr* autoconf automake bison cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng* libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* libX* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils ppl telnet t1lib t1lib* nasm nasm* wget zlib-devel gmp-devel
六、安装配置memcached
1、安装libevent
cd /usr/local/src
tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make
make install
2、安装memcached
cd /usr/local/src
tar zxvf memcached-1.4.25.tar.gz
cd memcached-1.4.25
./configure --prefix=/usr/local/memcached -with-libevent=/usr/local/libevent
make
make install
3、测试Memcached
groupadd memcached #创建组
useradd -g memcached memcached -s /bin/false #创建账号
ln -s /usr/local/memcached/bin/memcached /usr/local/bin/memcached #添加软连接
启动Memcache的服务器端的命令为:
/usr/local/memcached/bin/memcached -d -m 4096 -u root -l 192.168.4.6 -p 11211 -c 1024 -P /usr/local/memcached/memcached.pid
或者
/usr/local/memcached/bin/memcached -d -m 4096 -u memcached -l 192.168.4.6 -p 11211 -c 1024
参数说明:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是4096MB
-u是运行Memcache的用户,比如root或者memcached
-l是监听的服务器IP地址,这里指定服务器的IP地址192.168.4.6,不设置默认监听服务器所有IP地址
-p是设置Memcache监听的端口,默认为11211
-c选项是最大运行的并发连接数,默认是1024
-P是设置保存Memcache的pid文件,/usr/local/memcached/memcached.pid
开机自启动,在/etc/rc.d/rc.local中加入一行
/usr/local/memcached/bin/memcached -d -m 4096 -u root -l 192.168.4.6 -p 11211 -c 1024 -P /usr/local/memcached/memcached.pid
也可以使用下面的命令:
/usr/local/memcached/bin/memcached -d -m 4096 -p 11211 -u memcached
ip不指定时,默认监听本地所有ip地址,用户最好选择是非root用户,例如memcached
关闭Memcached服务
cat /usr/local/memcached/memcached.pid #查看进程
kill 22856 #结束进程
或者
killall memcached #结束服务
4、设置Memcached开机启动
vi /etc/rc.d/init.d/memcached
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: - 90 25
# description: MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
MEMCACHED="/usr/local/memcached/bin/memcached"
start()
{
echo -n $"Starting memcached: "
daemon $MEMCACHED -u memcached -d -m 4096 -p 11211 -c 1024
echo
}
stop()
{
echo -n $"Shutting down memcached: "
killproc memcached
echo
}
[ -f $MEMCACHED ] || exit 0
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
condrestart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|condrestart}"
exit 1
esac
exit 0
:wq! #保存退出
chmod 775 /etc/rc.d/init.d/memcached #赋予文件执行权限
chkconfig memcached on #设置开机启动
/etc/rc.d/init.d/memcached start #启动
至此,Linux下Memcached服务器部署完成。