当前位置:首页 >> 服务器

CentOS 7.x安装部署Memcached服务器的详细方法

操作系统:CentOS 7.x 64位

实现目的:安装部署Memcached服务器

一、防火墙设置

CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

2、安装iptables防火墙

yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# sample configuration for iptables service

# you can edit this manually or use system-config-firewall

# please do not ask us to add additional ports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 11211 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

/usr/libexec/iptables/iptables.init restart #重启防火墙

二、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

三 、系统约定

软件源代码包存放位置:/usr/local/src

源码包编译安装位置:/usr/local/软件名字

四、下载软件包

1、下载libevent

http://ftp.lfs-matrix.net/pub/blfs/conglomeration/libevent/libevent-2.0.22-stable.tar.gz

2、下载memcached

http://memcached.org/files/memcached-1.4.34.tar.gz

五、安装编译工具包

yum install -y apr* autoconf automake bison bzip2 bzip2* 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 libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-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.34.tar.gz

cd memcached-1.4.34

./configure --enable-sasl --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 #结束服务

系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链接

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服务器部署完成。