panalbish
panalbish

Reputation: 363

How can we create two instances of memcached server in same server in different port?

I tried to add in the way -l 11211 -l 11212 in memcached conf file. But it is just listening to first one i.e 1121

Upvotes: 35

Views: 37677

Answers (7)

gohdan
gohdan

Reputation: 41

Simple solution to Centos 6

First copy /etc/sysconfig/memcached to /etc/sysconfig/memcached2 and write new settings to the new file.

Then copy /etc/init.d/memcached to /etc/init.d/memcached2 and change in the new file:

  • PORT to your new port (it should be reset from /etc/sysconfig/memcached2, so we do it just in case)
  • /etc/sysconfig/memcached to /etc/sysconfig/memcached2
  • /var/run/memcached/memcached.pid to /var/run/memcached/memcached2.pid
  • /var/lock/subsys/memcached to /var/lock/subsys/memcached2

Now you can use service memcached2 start, service memcached2 stop etc. Don't forget chkconfig memcached2 on to run it when machine boots up.

Upvotes: 1

narasi
narasi

Reputation: 463

In case someone else stumbles upon this question, there is a bug on the debian distribution of memcached (which means flavours like Ubuntu would also be affected).

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

Because of this bug, even when you have separate configuration files, when you run sudo service memcached restart, only the default configuration file in /etc/memcached.conf will be loaded.

As mentioned in the comment here, the temporary solution is to

  1. Remove /lib/systemd/system/memcached.service

  2. Run sudo systemctl daemon-reload (don't worry, it is safe to do so)

  3. Finally, run sudo service memcached restart if you are okay with losing all cache information. If not, run sudo service memcached force-reload

Upvotes: 2

Davyd Dzhahaiev
Davyd Dzhahaiev

Reputation: 1031

First I used mikewied's solution, but then I bumped into the problem of auto starting the daemon. Another confusing thing in that solution is that it doesn't use the config from etc. I was about to create my own start up scripts in /etc/init.d but then I looked into /etc/init.d/memcached file and saw this beautiful solution

# 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.

Basically readers of this question just need to read the /etc/init.d/memcached file.

Cheers

Upvotes: 73

Yuri Malov
Yuri Malov

Reputation: 1257

Ok, very good answer, Tristan CHARBONNIER. Please replace code into file /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";
    }
}

Upvotes: 1

Tristan CHARBONNIER
Tristan CHARBONNIER

Reputation: 1231

The answer from David Dzhagayev is the best one. If you don't have the correct version of memcache init script, here is the one he is talking about:

It should work with any linux distro using init.

#! /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

Upvotes: 3

xiongjiabin
xiongjiabin

Reputation: 323

in /etc/memcached.conf you can just edit like below

-l 192.168.112.22,127.0.0.1

must use comma between two ip address

Upvotes: 0

mikewied
mikewied

Reputation: 5343

Here's what memcached says the -l command is for:

-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

First off you need to specify the interface you want memcached to listen on if you are using the -l flag. Use 0.0.0.0 for all interfaces and use 127.0.0.1 is you just want to be able to access memcached from localhost. Second, don't use two -l flags. Use only one and separate each address by a comma. The command below should do what you want.

memcached -l 0.0.0.0:11211,0.0.0.0:11212

Keep in mind that this will have one memcached instance listen on two ports. To have two memcached instances on one machine run these two commands.

memcached -p 11211 -d

memcached -p 11212 -d

Upvotes: 48

Related Questions