ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# memcached部署[6.x][7.x] [TOC] ## 一、环境准备 ### 1. 环境搭建: ```sh [root@xxx ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@xxx ~]# uname -r 3.10.0-693.el7.x86_64 [root@xxx ~]# hostname -I 10.0.0.21 172.16.1.21 [root@xxx ~]# hostnamectl set-hostname cache01 ``` ### 2. 防火墙、selinux ```sh sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0 systemctl stop firewalld.service systemctl disable firewalld.service ``` ## 二、服务安装 ### 1. 安装程序 1)yum安装 ```sh [root@xxx ~]# yum install memcached -y [root@xxx ~]# rpm -qa memcached memcached-1.4.4-5.el6.x86_64 ``` 2) 编译安装 需要高版本可以编译安装,但不特殊需求的话,推荐yum安装方式 ```sh wget http://memcached.org/files/memcached-1.5.9.tar.gz tar -zxf memcached-1.5.9.tar.gz cd memcached-1.4.39 ./configure && make &&make install ``` ### 2. 服务启动 1)查看配置文件 ```sh [root@xxx ~]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="" ``` 2)启动停止 ```sh #centos7 systemctl start memcached.service systemctl enable memcached.service #centos6 /etc/init.d/memcached start /etc/init.d/memcached stop ``` ## 三、多实例启动 ### 1. 多实例启动 多实例启动启动非常简单,只需要指定不同的端口即可 ```sh memcached -m 16m -p 11211 -d -u root -c 8192 memcached -m 16m -p 11212 -d -u root -c 8192 [root@xxx ~]# lsof -i :11211 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME memcached 11828 root 26u IPv4 68020 0t0 TCP *:memcache (LISTEN) memcached 11828 root 27u IPv6 68021 0t0 TCP *:memcache (LISTEN) memcached 11828 root 28u IPv4 68024 0t0 UDP *:memcache memcached 11828 root 29u IPv6 68025 0t0 UDP *:memcache [root@xxx ~]# lsof -i :11212 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME memcached 11843 root 26u IPv4 68355 0t0 TCP *:11212 (LISTEN) memcached 11843 root 27u IPv6 68356 0t0 TCP *:11212 (LISTEN) memcached 11843 root 28u IPv4 68359 0t0 UDP *:11212 memcached 11843 root 29u IPv6 68360 0t0 UDP *:11212 ``` ### 2. 开机自启 将启动命令加入/etc/rc.local即可开机启动 ```sh [root@xxx ~]# tail -2 /etc/rc.local /usr/bin/memcached -m 16m -p 11211 -d -u root -c 8192 /usr/bin/memcached -m 16m -p 11212 -d -u root -c 8192 ``` ### 3. 停止多实例 单实例直接killall或pkill都可以关闭服务,但若是多实例,这样会导致所有实例被关停。需要查出来pid后用kill命令杀掉 ```sh ps -ef|grep [m]emcached|grep "11211"|awk '{print $2}'|xargs kill ``` 或者在启动memcached服务时,指定pid文件,便于关闭,然后根据该文件中的pid来杀掉 ```sh memcached -m 16m -p 11211 -d -u root -c 8192 /var/run/11211.pid kill `cat /var/run/11211.pid ```