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

7

35

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

Worser answered 8/5, 2011 at 15:31 Comment(1)
For the simulation of clustering environment I am running two tomcats in one server, but both the tomcat servers are sharing one mysql server. Please correct me if I am wrong. For the sticky session sharing purpose I need to run memcahed as daemon.Worser
H
47

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
Handknit answered 13/5, 2011 at 22:43 Comment(4)
Wow thanks this is really going to help me out with my cluster.Stater
none of these work for me on 1.4.2 and the referenced documentation no longer seems to exist.Gunnery
None is this is working for me also.Posting below the error I am getting.Perlman
D:\sw\memcache\memcached-1.2.5-win32-bin>memcached -l 0.0.0.0:12213,0.0.0.0:12212 getaddrinfo(): No error failed to listenPerlman
D
73

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

Dealer answered 13/2, 2014 at 14:9 Comment(4)
This should be the chosen solutionDendriform
I think your init script must be different. On CenOS 6 I read the file /etc/init.d/memcached and I don't see how the commands you listed would work without modifying it. Maybe I am slow :)Selfish
Please be aware, this does not work for Debain Jessie because of this bug: bugs.debian.org/cgi-bin/bugreport.cgi?bug=784357. There are workarounds in the ticket.Pinion
Worked well after doing this little change: cp /lib/systemd/system/memcached.service /lib/systemd/system/[email protected] and editing that new file like on this link: Editing serviceCanard
H
47

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
Handknit answered 13/5, 2011 at 22:43 Comment(4)
Wow thanks this is really going to help me out with my cluster.Stater
none of these work for me on 1.4.2 and the referenced documentation no longer seems to exist.Gunnery
None is this is working for me also.Posting below the error I am getting.Perlman
D:\sw\memcache\memcached-1.2.5-win32-bin>memcached -l 0.0.0.0:12213,0.0.0.0:12212 getaddrinfo(): No error failed to listenPerlman
S
3

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
Sparker answered 29/4, 2015 at 4:24 Comment(0)
S
2

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

Squeteague answered 6/3, 2018 at 8:59 Comment(3)
Thank you! Only reload doesn't exist, I got Usage: /etc/init.d/memcached_server1 {start|stop|restart|force-reload|status}, I used restart insteadSlabber
Ah! Edited my original comment for clarity :)Squeteague
Thanks @narasi, combined yours and Davyd Dzhahaiev answered and it finally worked on Ubuntu. It also auto opens with reboot!Sap
I
1

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";
    }
}
Incorrigible answered 14/10, 2015 at 22:1 Comment(0)
C
1

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.

Consult answered 21/3, 2016 at 10:1 Comment(0)
V
0

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

Verniavernice answered 18/4, 2015 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.