zoukankan      html  css  js  c++  java
  • Memcached服务端自动启动(转载)

    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:

    Shell代码 复制代码 收藏代码
    1. #!/bin/sh   
    2. # chkconfig: - 55 45  
    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、更复杂的做法,创建完美的启动脚本

    网上找到以下两个脚本:

    Shell代码 复制代码 收藏代码
    1. #!/bin/sh   
    2. #   
    3. # memcached:    MemCached Daemon   
    4. #   
    5. # chkconfig:    - 90 25  
    6. # description:  MemCached Daemon   
    7. #   
    8. # Source function library.   
    9. . /etc/rc.d/init.d/functions   
    10. . /etc/sysconfig/network   
    11. #[ ${NETWORKING} = "no" ] && exit 0  
    12. #[ -r /etc/sysconfig/dund ] || exit 0  
    13. #. /etc/sysconfig/dund   
    14. #[ -z "$DUNDARGS" ] && exit 0  
    15. start()   
    16. {   
    17.         echo -n $"Starting memcached: "  
    18.         daemon $MEMCACHED -u daemon -d -m 1024 -l 127.0.0.1 -p 11211  
    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 1  
    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 3  
    40.         start   
    41.         ;;   
    42.     *)   
    43.         echo $"Usage: $0 {start|stop|restart}"  
    44.         exit 1  
    45. esac   
    46. 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
    #[ ${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
    

      

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

  • 相关阅读:
    剑指offer--38.左旋转字符串
    剑指offer--37.和为S的两个数字
    剑指offer--35.数组中只出现一次的数字
    剑指offer--34.数字在排序数组中出现的次数
    剑指offer--33.丑数
    剑指offer--36.整数中1出现的次数(从1到n整数中1出现的次数)
    剑指offer--32.把数组排成最小的数
    剑指offer--31.二叉树中和为某一值的路径
    剑指offer--30.二叉搜索树的后序遍历序列
    剑指offer--29.从上往下打印二叉树
  • 原文地址:https://www.cnblogs.com/xiaoerlang/p/3328067.html
Copyright © 2011-2022 走看看