---
title: "Memcached服务端自动启动"
author: "bardo"
date: "2011-03-12T02:28:17Z"
tags: []
categories: ["编程语言"]
slug: "memcached"
draft: false
lastmod: "2011-03-12T02:29:52Z"
id: 954770
url: "https://bardo.iteye.com/blog/954770"
---

经测试，要使得Memcached能够提供session共享服务，必须启动Memcached服务端为系统服务。本人较为初级，一般都是按向导安装的。  
所以，要将其设为自动启动的服务也就困难了。

上网搜索了一下，结果，得到以下一些结果，做个记录：

## 1、最傻的做法

通常：启动Memcache的服务器端的命令为：  
\# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid

-d选项是启动一个守护进程，  
\-m是分配给Memcache使用的内存数量，单位是MB，我这里是10MB，  
\-u是运行Memcache的用户，我这里是root，  
\-l是监听的服务器IP地址，如果有多个地址的话，我这里指定了服务器的IP地址192.168.0.200，  
\-p是设置Memcache监听的端口，我这里设置了12000，最好是1024以上的端口，  
\-c选项是最大运行的并发连接数，默认是1024，我这里设置了256，按照你服务器的负载量来设定，  
\-P是设置保存Memcache的pid文件，我这里是保存在 /tmp/memcached.pid，

想开机自动启动的话，只需在/etc/rc.d/rc.local中加入一行，上面命令  
有人用以下命令：  
/usr/local/memcached/bin/memcached -d -m 20 -p 11211 -u apache  
上面有些东西可以参考一下：即，ip不指定时，默认是本机，用户，最好选择是：apache 或 deamon  
这样，也就是属于哪个用户的服务，由哪个用户启动。

 

## 2、较正规的方法：

To add a service to chkconfig you will normally need a couple of special comments below the shebang of a shell script:

```
#!/bin/sh
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
```

 

After adding the lines to /etc/init.d/memcached you can then issue

chkconfig --add memcached  
There are of course additional run levels a process can start at so to check that you would issue

chkconfig --list | grep "memcached"  
A common run level for memcached would be

chkconfig --level 345 memcached on

说明：chkconfig --add memcached 用来添加memcached服务  
chkconfig --list | grep "memcached" 检查服务是否添加  
还可以简写为这样：  
chkconfig  --list | grep mem

chkconfig --level 345 memcached on 设置运行级别。  
建议：最好使用chkconfig --level 235 memcached on 这样的话与apache级别相同，即只要有apache，就有memcached

## 3、更复杂的做法，创建完美的启动脚本

网上找到以下两个脚本：

 

```
#!/bin/sh
#
# memcached:    MemCached Daemon
#
# chkconfig:    - 90 25
# description:  MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0
start()
{
        echo -n $"Starting memcached: "
        daemon $MEMCACHED -u daemon -d -m 1024 -l 127.0.0.1 -p 11211
        echo
}
stop()
{
        echo -n $"Shutting down memcached: "
        killproc memcached
        echo
}
MEMCACHED="/usr/local/memcached/bin/memcached"
[ -f $MEMCACHED ] || exit 1
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 3
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac
exit 0
```

  

```
#!/bin/sh
#
# memcached:    MemCached Daemon
#
# chkconfig:    - 90 25 
# description:  MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
 
start() 
{
        echo -n $"Starting memcached: "
        daemon /usr/local/bin/memcached -u daemon -d -m 4096 -l 10.10.10.220 -p 58728
        echo
}
 
stop() 
{
        echo -n $"Shutting down memcached: "
        killproc memcached 
        echo
}
 
[ -f /usr/local/bin/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
```

  

在上述指定目录创建了上述某一个脚本以后，就可以进行以下操作：  
   
\[root@crm ~]# chkconfig  --add memcached  
\[root@crm ~]# chkconfig  --level 235  memcached  on  
\[root@crm ~]# chkconfig  --list | grep mem  
memcached       0:off   1:off   2:on   3:on    4:off   5:on   6:off

接下来，可以用以下命令启动与停止 memcached

/etc/rc.d/init.d/memcached  start   
/etc/rc.d/init.d/memcached  stop  
/etc/rc.d/init.d/memcached  restart  
如：  
\[root@crm ~]# /etc/rc.d/init.d/memcached  restart  
Shutting down memcached: \[  OK  ]  
Starting memcached:      \[  OK  ]

同时，还可以用：  
service memcached start  
这样的命令操作

然后，可以用ps命令查看进程信息。  
\[root@crm ~]# ps aux | grep mem  
daemon   23781  0.0  0.2 13892 9860 ?  Ss 16:51:00  /.../memcached -u daemon -d -m 1024 -l 172.16.0.106 -p 11211

以上两个脚本前一个脚本中，对网络进行检查。其它都是针对服务启动与停止的命令提示设置。  
有人说，复杂的脚本并不好懂，自己也不会写，却想要更完善的，怎么办？  
那就到网上找高手的。最好的捷径就是到对应的RPM包中去找。（如果直接用RPM包安装，这些事情都不用做了）  
当然，memcached多数情况下都是编译安装，因为，很多时候都是找不到对应的版本。  
脚本中 # chkconfig: - 55 45 运行级别这一列参数用的是 -，这样，是不在脚本中写死，可以通过 chkconfig  --level 235  memcached  on 灵活设置。  
最后就是，目前仍不了解  
. /etc/sysconfig/network  
#\[ ${NETWORKING} = "no" ] && exit 0  
#\[ -r /etc/sysconfig/dund ] || exit 0  
#. /etc/sysconfig/dund  
#\[ -z "$DUNDARGS" ] && exit 0  
这一段的详细含义。需要进一步学习！