2011-05-08 81 views
30

我試圖在memcached conf文件中以 -l 11211 -l 11212 的方式添加。但它僅僅是聽第一個即1121我們如何在不同端口的同一服務器上創建兩個memcached服務器實例?

+0

爲了模擬集羣環境,我在一臺服務器上運行兩個tomcat,但兩臺tomcat服務器都共享一臺mysql服務器。如果我錯了,請糾正我。 對於粘性會話共享的目的,我需要運行memcahed作爲守護進程。 – panalbish 2011-05-08 16:37:17

回答

43

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

60

首先我用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

0

在/etc/memcached.conf您可以編輯象下面

-l 192.168.112.22,127.0.0.1

必須使用逗號之間有兩個IP地址

2

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 
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"; 
    } 
} 
1

簡單的解決方案,以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在機器啓動時運行它。

0

如果有人絆倒這個問題,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

相關問題