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

时间:2011-05-08 15:31:20

标签: memcached

我试图加入方式 -l 11211 -l 11212 在memcached conf文件中。但它只是听第一个,即1121

7 个答案:

答案 0 :(得分:65)

首先我使用了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文件。

干杯

答案 1 :(得分:45)

以下是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

首先,如果使用-l标志,则需要指定要memcached监听的接口。对所有接口使用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

答案 2 :(得分:3)

David Dzhagayev的回答是最好的。如果你没有正确版本的memcache init脚本,那么就是他正在谈论的那个:

它应该适用于使用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

答案 3 :(得分:1)

好的,非常好的答案,Tristan CHARBONNIER。 请将代码替换为文件/ usr / share / memcached / scripts / start-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";
    }
}

答案 4 :(得分:1)

Centos 6的简单解决方案

首先将/etc/sysconfig/memcached复制到/etc/sysconfig/memcached2并将新设置写入新文件。

然后将/etc/init.d/memcached复制到/etc/init.d/memcached2并更改新文件:

  • PORT到您的新端口(应该从/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 startservice memcached2 stop等。机器启动时不要忘记chkconfig memcached2 on运行它。

答案 5 :(得分:1)

如果其他人偶然发现了这个问题,那么memcached的debian发行版就会出现错误(这意味着像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 restart。如果没有,请运行sudo service memcached force-reload

答案 6 :(得分:0)

在/etc/memcached.conf中你可以像下面那样进行编辑

-l 192.168.112.22,127.0.0.1

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