案例1:在centos6系统上操作
[root@varinish01 ~]# yum -y install memcached
查询memcached安装了哪些
~~~
[root@varinish01 ~]# rpm -ql memcached
/etc/rc.d/init.d/memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/share/doc/memcached-1.4.4
/usr/share/doc/memcached-1.4.4/AUTHORS
/usr/share/doc/memcached-1.4.4/CONTRIBUTORS
/usr/share/doc/memcached-1.4.4/COPYING
/usr/share/doc/memcached-1.4.4/ChangeLog
/usr/share/doc/memcached-1.4.4/NEWS
/usr/share/doc/memcached-1.4.4/README
/usr/share/doc/memcached-1.4.4/protocol.txt
/usr/share/doc/memcached-1.4.4/readme.txt
/usr/share/doc/memcached-1.4.4/threads.txt
/usr/share/man/man1/memcached.1.gz
/var/run/memcached
~~~
案例2:在centos7系统上操作
[root@node7 ~]# yum -y install memcached
查询memcached安装了哪些
~~~
[root@node7 ~]# rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached-1.4.15
/usr/share/doc/memcached-1.4.15/AUTHORS
/usr/share/doc/memcached-1.4.15/CONTRIBUTORS
案例1:在centos6系统上操作
[root@varinish01 ~]# yum -y install memcached
查询memcached安装了哪些
[root@varinish01 ~]# rpm -ql memcached
/etc/rc.d/init.d/memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/share/doc/memcached-1.4.4
/usr/share/doc/memcached-1.4.4/AUTHORS
/usr/share/doc/memcached-1.4.4/CONTRIBUTORS
/usr/share/doc/memcached-1.4.4/COPYING
/usr/share/doc/memcached-1.4.4/ChangeLog
/usr/share/doc/memcached-1.4.4/NEWS
/usr/share/doc/memcached-1.4.4/README
/usr/share/doc/memcached-1.4.4/protocol.txt
/usr/share/doc/memcached-1.4.4/readme.txt
/usr/share/doc/memcached-1.4.4/threads.txt
/usr/share/man/man1/memcached.1.gz
/var/run/memcached
案例2:在centos7系统上操作
[root@node7 ~]# yum -y install memcached
查询memcached安装了哪些
[root@node7 ~]# rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached-1.4.15
/usr/share/doc/memcached-1.4.15/AUTHORS
/usr/share/doc/memcached-1.4.15/CONTRIBUTORS
/usr/share/doc/memcached-1.4.15/COPYING
/usr/share/doc/memcached-1.4.15/ChangeLog
/usr/share/doc/memcached-1.4.15/NEWS
/usr/share/doc/memcached-1.4.15/README.md
/usr/share/doc/memcached-1.4.15/protocol.txt
/usr/share/doc/memcached-1.4.15/readme.txt
/usr/share/doc/memcached-1.4.15/threads.txt
/usr/share/man/man1/memcached-tool.1.gz
/usr/share/man/man1/memcached.1.gz
启动服务
[root@node7 ~]# systemctl start memcached
[root@node7 ~]# ss -tunlp|grep 11211
udp UNCONN 0 0 *:11211 *:* users:(("memcached",pid=27619,fd=28))
udp UNCONN 0 0 :::11211 :::* users:(("memcached",pid=27619,fd=29))
tcp LISTEN 0 128 *:11211 *:* users:(("memcached",pid=27619,fd=26))
tcp LISTEN 0 128 :::11211 :::* users:(("memcached",pid=2761
案例3: 在centos7上编译安装memcached-v1.5.5
#!/bin/bash
#description: install memcached on centos7
#date 2018/02/21
#
MEMCACHED_VERSION=1.5.5
SOFTDIR=/usr/local/src
#install libevent
cd /usr/local/src
[ ! -f libevent-2.1.8-stable.tar.gz ] && wget http://soft.51yuki.cn/libevent-2.1.8-stable.tar.gz
[ ! -d libevent-2.1.8-stable ] && tar xf libevent-2.1.8-stable.tar.gz && cd libevent-2.1.8-stable && ./configure --prefix=/usr/libevent && make && make install
/usr/sbin/ldconfig
#install memcached
cd /usr/local/src && [ ! -f memcached-${MEMCACHED_VERSION}.tar.gz ] && wget http://www.memcached.org/files/memcached-${MEMCACHED_VERSION}.tar.gz
[ ! -d memcached-${MEMCACHED_VERSION} ] && tar xf memcached-${MEMCACHED_VERSION}.tar.gz && cd memcached-${MEMCACHED_VERSION} && ./configure -with-libevent=/usr/libevent/ -prefix=/usr/local/memcached && make && make install
#configure memcached
cat > /etc/sysconfig/memcached <<EOF
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="256"
OPTIONS=""
EOF
#add memcached user
if ! id memcached &>/dev/null ;then
groupadd -g 1120 memcached
useradd -u 1120 -g memcached -s /sbin/nologin memcached
fi
#add memcached start scripts
cd /usr/lib/systemd/system
USER="memcached"
PORT="11211"
CACHESIZE="256"
MAXCONN="1024"
OPTIONS=""
IPADDR="192.168.20.133"
cat > memcached.service <<EOF
[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 -l $IPADDR $OPTIONS
[Install]
WantedBy=multi-user.target
EOF
#start memcached
systemctl restart memcached.service
if [ "`ps -ef|grep memcached|wc -l`" -gt 1 ] ;then
echo "memcached start successful"
else
echo "memcached start fail"
fi
案例4: 在centos7上,通过saltstack来批量安装memcached服务
第一步:安装libevent
[admin@master prod]$ sudo mkdir libevent/files -p
[admin@master prod]$ cd libevent/files/
[admin@master files]$ sudo wget http://soft.51yuki.cn/libevent-2.1.8-stable.tar.gz
[admin@master libevent]$ sudo vim install.sls
include:
- pkg.pkg-init
libevent-install:
file.managed:
- name: /usr/local/src/libevent-2.1.8-stable.tar.gz
- source: salt://libevent/files/libevent-2.1.8-stable.tar.gz
- user: root
- group: root
- mode: 644
cmd.run:
- name: cd /usr/local/src && sudo tar zxf libevent-2.1.8-stable.tar.gz && cd libevent-2.1.8-stable && sudo ./configure --prefix=/usr/local/libevent && make && make install
- unless: test -d /usr/local/libevent
- require:
- file: libevent-install
测试看看:
[admin@master prod]$ sudo salt 'proxy01*' state.sls libevent.install env=prod test=true
第二步:安装memcached
[admin@master prod]$ pwd
/srv/salt/prod
[admin@master prod]$ sudo mkdir memcached/files -p
[admin@master files]$ sudo wget http://www.memcached.org/files/memcached-1.5.5.tar.gz
[admin@master files]$cd ..
[admin@master memcached]$ vim install.sls
include:
- libevent.install
memcached-install:
file.managed:
- name: /usr/local/src/memcached-1.5.5.tar.gz
- source: salt://memcached/files/memcached-1.5.5.tar.gz
- user: root
- group: root
- mode: 644
cmd.run:
- name: cd /usr/local/src && sudo tar zxf memcached-1.5.5.tar.gz && cd memcached-1.5.5 && sudo ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent && make && make install
- unless: test -d /usr/local/memcached
- require:
- cmd: libevent-install
- file: memcached-install
测试看看:
[admin@master prod]$ sudo salt 'proxy01*' state.sls memcached.install env=prod test=true
Succeeded: 15 (unchanged=2, changed=1)
Failed: 0
-------------
Total states run: 15
[admin@master files]$ ll (如果需要memcached和memcached.service,可以找一台虚拟机手动安装一遍,然后拷贝过来,适当修改一下)
total 448
-rw-r--r--. 1 root root 71 Feb 22 17:25 memcached
-rw-r--r--. 1 root root 449297 Feb 13 13:13 memcached-1.5.5.tar.gz
-rw-r--r--. 1 root root 257 Feb 22 17:26 memcached.service
[admin@master files]$ pwd
/srv/salt/prod/memcached/files
[admin@master memcached]$ cat service.sls
include:
- memcached.install
memcached-user-group:
group.present:
- name: memcached
- gid: 1120
user.present:
- name: memcached
- fullname: memcached
- shell: /sbin/nologin
- uid: 1120
- gid: 1120
/etc/sysconfig/memcached:
file.managed:
- source: salt://memcached/files/memcached
- user: root
- group: root
- mode: 644
memcached-service:
file.managed:
- name: /usr/lib/systemd/system/memcached.service
- source: salt://memcached/files/memcached.service
- user: root
- group: root
- mode: 644
service.running:
- name: memcached
- enable: True
- require:
- file: memcached-service
- user: memcached-user-group
- watch:
- file: /etc/sysconfig/memcached
测试看看:
[admin@master prod]$ sudo salt 'proxy01*' state.sls memcached.service env=prod test=true
Summary
-------------
Succeeded: 20 (unchanged=7, changed=3)
Failed: 0
-------------
Total states run: 20
如何没有问题,可以编写top.sls文件咯,我这里就直接安装
[admin@master prod]$ sudo salt 'proxy01*' state.sls memcached.service env=prod
Summary
-------------
Succeeded: 20 (changed=3)
Failed: 0
-------------
Total states run: 20
查看客户端端口是否启动
[admin@master prod]$ sudo salt 'proxy01*' cmd.run "ss -tunlp|grep 11211"
proxy01.51yuki.cn:
udp UNCONN 0 0 *:11211 *:* users:(("memcached",pid=3525,fd=31),("memcached",pid=3525,fd=30),("memcached",pid=3525,fd=29),("memcached",pid=3525,fd=28))
udp UNCONN 0 0 :::11211 :::* users:(("memcached",pid=3525,fd=35),("memcached",pid=3525,fd=34),("memcached",pid=3525,fd=33),("memcached",pid=3525,fd=32))
tcp LISTEN 0 128 *:11211 *:* users:(("memcached",pid=3525,fd=26))
tcp LISTEN 0 128 :::11211 :::* users:(("memcached",pid=3525,fd=27))
参数详解
~~~
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是100MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.20.136,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,我们这里统一使用11211
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定。
-P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid,
~~~
Memcached 管理监控工具
http://www.junopen.com/memadmin/memadmin-1.0.12.tar.gz
(需要安装php-memcache扩展)
测试:
http://10.2.13.219/memadmin/index.php?action=admin
默认用户名:admin 密码:admin