我们如何在不同端口的同一服务器上创建两个memcached服务器实例?

问题描述:

我试图在memcached conf文件中以 -l 11211 -l 11212 的方式添加。但它仅仅是听第一个即1121我们如何在不同端口的同一服务器上创建两个memcached服务器实例?

+0

为了模拟集群环境,我在一台服务器上运行两个tomcat,但两台tomcat服务器都共享一台mysql服务器。如果我错了,请纠正我。 对于粘性会话共享的目的,我需要运行memcahed作为守护进程。 – panalbish 2011-05-08 16:37:17

下面是memcached的说,-l命令是:

-l <addr>  interface to listen on (default: INADDR_ANY, all addresses) 
       <addr> may be specified as host:port. If you don't specify 
       a port number, the value you specified with -p or -U is 
       used. You may specify multiple addresses separated by comma 
       or by using -l multiple times 

首先,你需要指定你想,如果你正在使用Memcached的侦听的接口-l标志。对所有接口使用0.0.0.0并使用127.0.0.1您只是想从localhost访问memcached。其次,不要使用两个-l标志。只能使用一个,并用逗号分隔每个地址。下面的命令应该做你想要的。

memcached -l 0.0.0.0:11211,0.0.0.0:11212 

请记住,这将有一个memcached实例在两个端口上侦听。在一台机器上有两个memcached实例运行这两个命令。

memcached -p 11211 -d 

memcached -p 11212 -d 
+0

感谢您的回答。作为魅力工作:) – panalbish 2011-05-14 13:13:05

+0

哇感谢这真的会帮助我与我的群集。 – WojonsTech 2012-09-22 02:53:05

+0

这些都不适用于1.4.2,而且引用的文档似乎不再存在。 – Brunis 2014-08-01 11:28:15

首先我用mikewied的解决方案,但后来碰到了自动启动守护进程的问题。该解决方案另一个令人困惑的事情是,它不使用配置等。我正要在/etc/init.d中创建自己的启动脚本,但后来我查看了/etc/init.d/memcached文件,并看到这个美丽的解决方案

# Usage: 
# cp /etc/memcached.conf /etc/memcached_server1.conf 
# cp /etc/memcached.conf /etc/memcached_server2.conf 
# start all instances: 
# /etc/init.d/memcached start 
# start one instance: 
# /etc/init.d/memcached start server1 
# stop all instances: 
# /etc/init.d/memcached stop 
# stop one instance: 
# /etc/init.d/memcached stop server1 
# There is no "status" command. 

基本上这个问题的读者只需要阅读/etc/init.d/memcached文件。

干杯

+7

这应该是选择的解决方案 – Avision 2014-08-20 23:33:07

+0

我认为你的初始化脚本必须不同。在CenOS 6上,我读取了/etc/init.d/memcached这个文件,但是我没有看到你列出的命令在不修改它的情况下是如何工作的。也许我很慢:) – im3r3k 2015-04-03 10:24:46

+1

请注意,这对Debain Jessie不起作用,因为这个bug:https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=784357。票证中有解决方法。 – gArn 2016-07-21 13:22:04

在/etc/memcached.conf您可以编辑象下面

-l 192.168.112.22,127.0.0.1

必须使用逗号之间有两个IP地址

David Dzhagayev的回答是最好的。如果你没有正确版本的memcache初始化脚本,这里是他正在谈论的那个:

它应该与任何使用init的linux发行版一起工作。

#! /bin/bash 
### BEGIN INIT INFO 
# Provides:   memcached 
# Required-Start:  $remote_fs $syslog 
# Required-Stop:  $remote_fs $syslog 
# Should-Start:    $local_fs 
# Should-Stop:   $local_fs 
# Default-Start:  2 3 4 5 
# Default-Stop:    0 1 6 
# Short-Description: Start memcached daemon 
# Description:   Start up memcached, a high-performance memory caching daemon 
### END INIT INFO 

# Usage: 
# cp /etc/memcached.conf /etc/memcached_server1.conf 
# cp /etc/memcached.conf /etc/memcached_server2.conf 
# start all instances: 
# /etc/init.d/memcached start 
# start one instance: 
# /etc/init.d/memcached start server1 
# stop all instances: 
# /etc/init.d/memcached stop 
# stop one instance: 
# /etc/init.d/memcached stop server1 
# There is no "status" command. 

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 
DAEMON=/usr/bin/memcached 
DAEMONNAME=memcached 
DAEMONBOOTSTRAP=/usr/share/memcached/scripts/start-memcached 
DESC=memcached 

test -x $DAEMON || exit 0 
test -x $DAEMONBOOTSTRAP || exit 0 

set -e 

. /lib/lsb/init-functions 

# Edit /etc/default/memcached to change this. 
ENABLE_MEMCACHED=no 
test -r /etc/default/memcached && . /etc/default/memcached 


FILES=(/etc/memcached_*.conf) 
# check for alternative config schema 
if [ -r "${FILES[0]}" ]; then 
    CONFIGS=() 
    for FILE in "${FILES[@]}"; 
    do 
    # remove prefix 
    NAME=${FILE#/etc/} 
    # remove suffix 
    NAME=${NAME%.conf} 

    # check optional second param 
    if [ $# -ne 2 ]; 
    then 
     # add to config array 
     CONFIGS+=($NAME) 
    elif [ "memcached_$2" == "$NAME" ]; 
    then 
     # use only one memcached 
     CONFIGS=($NAME) 
     break; 
    fi; 
    done; 

    if [ ${#CONFIGS[@]} == 0 ]; 
    then 
    echo "Config not exist for: $2" >&2 
    exit 1 
    fi; 
else 
    CONFIGS=(memcached) 
fi; 

CONFIG_NUM=${#CONFIGS[@]} 
for ((i=0; i < $CONFIG_NUM; i++)); do 
    NAME=${CONFIGS[${i}]} 
    PIDFILE="/var/run/${NAME}.pid" 

case "$1" in 
    start) 
     echo -n "Starting $DESC: " 
     if [ $ENABLE_MEMCACHED = yes ]; then 
      start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE 
      echo "$NAME." 
     else 
      echo "$NAME disabled in /etc/default/memcached." 
     fi 
     ;; 
    stop) 
     echo -n "Stopping $DESC: " 
     start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE --exec $DAEMON 
     echo "$NAME." 
     rm -f $PIDFILE 
     ;; 

    restart|force-reload) 
     # 
     #  If the "reload" option is implemented, move the "force-reload" 
     #  option to the "reload" entry above. If not, "force-reload" is 
     #  just the same as "restart". 
     # 
     echo -n "Restarting $DESC: " 
     start-stop-daemon --stop --quiet --oknodo --retry 5 --pidfile $PIDFILE 
     rm -f $PIDFILE 
     if [ $ENABLE_MEMCACHED = yes ]; then 
       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE 
       echo "$NAME." 
     else 
      echo "$NAME disabled in /etc/default/memcached." 
     fi 
     ;; 
    status) 
     status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $? 
     ;; 
    *) 
     N=/etc/init.d/$NAME 
     echo "Usage: $N {start|stop|restart|force-reload|status}" >&2 
     exit 1 
     ;; 
esac 
done; 

exit 0 

好的,非常好的答案,特里斯坦CHARBONNIER。 请更换代码到文件在/ usr /共享/分布式缓存/脚本/启动memcached的:

#!/usr/bin/perl -w 
# start-memcached 
# 2003/2004 - Jay Bonci 
# This script handles the parsing of the /etc/memcached.conf file 
# and was originally created for the Debian distribution. 
# Anyone may use this little script under the same terms as 
# memcached itself. 

use strict; 

if($> != 0 and $< != 0) 
{ 
    print STDERR "Only root wants to run start-memcached.\n"; 
    exit; 
} 

my $params; my $etchandle; my $etcfile = "/etc/memcached.conf"; 

# This script assumes that memcached is located at /usr/bin/memcached, and 
# that the pidfile is writable at /var/run/memcached.pid 

my $memcached = "/usr/bin/memcached"; 
my $pidfile = "/var/run/memcached.pid"; 

if (scalar(@ARGV) == 2) { 
    $etcfile = shift(@ARGV); 
    $pidfile = shift(@ARGV); 
} 

# If we don't get a valid logfile parameter in the /etc/memcached.conf file, 
# we'll just throw away all of our in-daemon output. We need to re-tie it so 
# that non-bash shells will not hang on logout. Thanks to Michael Renner for 
# the tip 
my $fd_reopened = "/dev/null"; 

    sub handle_logfile 
    { 
     my ($logfile) = @_; 
     $fd_reopened = $logfile; 
    } 

    sub reopen_logfile 
    { 
     my ($logfile) = @_; 

     open *STDERR, ">>$logfile"; 
     open *STDOUT, ">>$logfile"; 
     open *STDIN, ">>/dev/null"; 
     $fd_reopened = $logfile; 
    } 

# This is set up in place here to support other non -[a-z] directives 

my $conf_directives = { 
    "logfile" => \&handle_logfile, 
}; 

if(open $etchandle, $etcfile) 
{ 
    foreach my $line (< $etchandle>) 
    { 
     $line ||= ""; 
     $line =~ s/\#.*//g; 
     $line =~ s/\s+$//g; 
     $line =~ s/^\s+//g; 
     next unless $line; 
     next if $line =~ /^\-[dh]/; 

     if($line =~ /^[^\-]/) 
     { 
      my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/; 
      $conf_directives->{$directive}->($arg); 
      next; 
     } 

     push @$params, $line;  
    } 

}else{ 
    $params = []; 
} 

    push @$params, "-u root" unless(grep "-u", @$params); 
    $params = join " ", @$params; 

if(-e $pidfile) 
{ 
    open PIDHANDLE, "$pidfile"; 
    my $localpid = <PIDHANDLE>; 
    close PIDHANDLE; 

    chomp $localpid; 
    if(-d "/proc/$localpid") 
    { 
     print STDERR "memcached is already running.\n"; 
     exit;  
    }else{ 
     `rm -f $localpid`; 
    } 

} 

my $pid = fork(); 

if($pid == 0) 
{ 
     reopen_logfile($fd_reopened); 
     exec "$memcached $params"; 
     exit(0); 

}else{ 
    if(open PIDHANDLE,">$pidfile") 
    { 
     print PIDHANDLE $pid; 
     close PIDHANDLE; 
    }else{ 

     print STDERR "Can't write pidfile to $pidfile.\n"; 
    } 
} 

简单的解决方案,以CentOS 6的

首页复印/etc/sysconfig/memcached/etc/sysconfig/memcached2并写入新设置到新的文件。

然后/etc/init.d/memcached复制到/etc/init.d/memcached2并在新文件中的变化:

  • 端口连接到新的端口(它应该从/etc/sysconfig/memcached2复位,所以我们这样做是以防万一)
  • /etc/sysconfig/memcached/etc/sysconfig/memcached2
  • /var/run/memcached/memcached.pid/var/run/memcached/memcached2.pid
  • /var/lock/subsys/memcached/var/lock/subsys/memcached2

现在你可以使用service memcached2 start,service memcached2 stop等。不要忘记chkconfig memcached2在机器启动时运行它。

如果有人绊倒这个问题,memcached的debian发行版有一个bug(这意味着像Ubuntu这样的风格也会受到影响)。

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=784357

由于这个错误,即使你有单独的配置文件,当您运行sudo service memcached restart,只有在/etc/memcached.conf默认的配置文件将被加载。

正如comment here提到的,临时的解决办法是

  1. 删除/lib/systemd/system/memcached.service

  2. 运行sudo systemctl daemon-reload(不用担心,它是安全的 左右)

  3. 最后,运行sudo service memcached reload