Memcached服务端自动启动

经测试,要使得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:

  1. #!/bin/sh
  2. # chkconfig: -
  3. # description:  The memcached daemon is a network memory cache service.
  4. # processname: memcached
#!/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、更复杂的做法,创建完美的启动脚本

网上找到以下两个脚本:

  1. #!/bin/sh
  2. #
  3. # memcached:    MemCached Daemon
  4. #
  5. # chkconfig:    -
  6. # description:  MemCached Daemon
  7. #
  8. # Source function library.
  9. . /etc/rc.d/init.d/functions
  10. . /etc/sysconfig/network
  11. #[ ${NETWORKING} = "no" ] && exit
  12. #[ -r /etc/sysconfig/dund ] || exit
  13. #. /etc/sysconfig/dund
  14. #[ -z "$DUNDARGS" ] && exit
  15. start()
  16. {
  17. echo -n $"Starting memcached: "
  18. daemon $MEMCACHED -u daemon -d -m  -l 127.0.0.1 -p
  19. echo
  20. }
  21. stop()
  22. {
  23. echo -n $"Shutting down memcached: "
  24. killproc memcached
  25. echo
  26. }
  27. MEMCACHED="/usr/local/memcached/bin/memcached"
  28. [ -f $MEMCACHED ] || exit
  29. # See how we were called.
  30. case "$1" in
  31. start)
  32. start
  33. ;;
  34. stop)
  35. stop
  36. ;;
  37. restart)
  38. stop
  39. sleep
  40. start
  41. ;;
  42. *)
  43. echo $"Usage: $0 {start|stop|restart}"
  44. exit
  45. esac
  46. exit
#!/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
  1. #!/bin/sh
  2. #
  3. # memcached:    MemCached Daemon
  4. #
  5. # chkconfig:    -
  6. # description:  MemCached Daemon
  7. #
  8. # Source function library.
  9. . /etc/rc.d/init.d/functions
  10. . /etc/sysconfig/network
  11. start()
  12. {
  13. echo -n $"Starting memcached: "
  14. daemon /usr/local/bin/memcached -u daemon -d -m  -l 10.10.10.220 -p
  15. echo
  16. }
  17. stop()
  18. {
  19. echo -n $"Shutting down memcached: "
  20. killproc memcached
  21. echo
  22. }
  23. [ -f /usr/local/bin/memcached ] || exit
  24. # See how we were called.
  25. case "$1" in
  26. start)
  27. start
  28. ;;
  29. stop)
  30. stop
  31. ;;
  32. restart|reload)
  33. stop
  34. start
  35. ;;
  36. condrestart)
  37. stop
  38. start
  39. ;;
  40. *)
  41. echo $"Usage: $0 {start|stop|restart|reload|condrestart}"
  42. exit
  43. esac
  44. exit
#!/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
这一段的详细含义。需要进一步学习!

Memcached服务端自动启动(转载)的更多相关文章

  1. 在 Windows Server 上搭建 *** 服务端(转载加亲测)

    转载自:https://diveng.io/build-shadowsocks-server-on-windows-server.html 下面的教程建议大家使用第一种方法安装,说是比较简单.我则使用 ...

  2. PHP5.5在windows 安装使用 memcached 服务端的方法以及 php_memcache.dll 下载

    PHP5.5 在windows下安装 memcached 的方法 下载服务端资源 http://download.csdn.net/detail/zsjangel/7104727 下载完成后,解压(我 ...

  3. Centos下使用gitosis配置管理git服务端(转载)

    From:http://www.cnblogs.com/ahauzyy/archive/2013/04/08/3043384.html 说明:由于条件有限,我这里使用的是同一台centos的,但教程内 ...

  4. 自动安装memcached服务端与PHP扩展Memcached

    该脚本基于阿里云服务器安装脚本,并只能运用于centos / aliyun os,该脚本使用时,需要与阿里云安装脚本的install.sh放在同一目录下.有缘人切忌乱用: #! /bin/bash # ...

  5. Memcached服务介绍及安装指南

    一.memcached服务介绍 1.为什么需要memcached服务 A:第一种场景 网站访问大多数情况下都需要查询数据库操作,如果网站的流量很大并且大多数的访问会造成数据库高负荷的状况下,由于大部分 ...

  6. Memcached服务安装

    安装Memcached服务 memcache分为服务端和客户端程序 服务端程序用来支持存储k-v值,程序名称memcached 客户端与服务端通信,进行存取值(常用的如php的memcache扩展,m ...

  7. LNMP 添加 memcached服务

    LNMP 添加 memcached服务   由于memcached具有更多的功能和服务,已经不推荐使用memcache了.(缺少个字母d) 1. 首先安装memcached服务端. 这里使用yum源安 ...

  8. 转载 ----HTML5 ---js实现json方式提交数据到服务端

    json提交给服务器我们在提交之前需要通过js的相关函数来把数据转换成json格式的数据再进行post或get了,下面来看看.   大概需求就是前端要把数据组装成json,传给后端.首先,在客户端,通 ...

  9. 修改memcached服务的端口号

    windows下修改memcached服务的端口号(默认端口:11211) 如果不是作为服务启动memcached的话,memcached -p 端口号就可以了. 通过修改注册表可以简单实现 运行:r ...

随机推荐

  1. idea报错could not autowired .但是可以正常运行

    转 http://www.cnblogs.com/softidea/p/5763285.html 解决办法: File-->Project Setting-->Facets-->Sp ...

  2. SQL的主键和外键和唯一约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  3. 移动web端使用rem实现自适应原理

    1.先获取到物理像素和实际像素的像素比 var pixclPatio = 1 / window.devicePixelRatio; 2.viewport视口设置为像素比大小 document.writ ...

  4. YY的GCD(bzoj 2820)

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  5. javaweb学习总结(十一)——使用Cookie进行会话管理(转)

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  6. UVa1476 Error Curves

    画出函数图像后,发现是一个类似V字型的图. 可以用三分法找图像最低点 WA了一串,之后发现是读入优化迷之被卡. /*by SilverN*/ #include<iostream> #inc ...

  7. 如何在requirejs下引用bootstrap

    原本以为只要require过来就能用 require(['jquery','underscore','bootstrap','cache'],function($,U,B,C){ 但发现会报错,类似未 ...

  8. 转 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法

    转自: http://www.cnblogs.com/Anker/p/3355573.html 今天从Apache官网上http://httpd.apache.org/下载httpd web服务器,由 ...

  9. 27深入理解C指针之---字符串基础

    一.字符串:是以ASCII字符NUL结尾的字符序列,NUL表示为\0 1.定义:将字符按顺序存储在数组中,以NUL结尾. 2.特征: 1).每个字符串长度只是包含所有的字符,不包括最后的NUL,手动分 ...

  10. 第6章 I/O多路复用

    前一章节客户端同时处理两个输入:标准输入和TCP套接字,然而问题在于客户端阻塞于fgets调用期,服务器进程被杀死后,服务器tcp虽然可以正确发送一个fin,但进程正阻塞于标准输入,它无法看到eof, ...