1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem enabling memcached.

Discussion in 'Server Configuration and Hosting' started by OldCoals, Jul 1, 2012.

  1. OldCoals

    OldCoals Active Member

    If the following is enable in config.php I receive an error when trying to access the forum.

    Code:
    $config['db']['host'] = 'xxxxx';
    $config['db']['port'] = 'xxxxx';
    $config['db']['username'] = 'xxxxx';
    $config['db']['password'] = 'xxxxx';
    $config['db']['dbname'] = 'xxxxx';
     
     
    $config['superAdmins'] = '1';
     
    $config['cache']['enabled'] = true;
    $config['cache']['frontend'] = 'Core';
    $config['cache']['frontendOptions']['cache_id_prefix'] = 'xf_';
     
    $config['cache']['backend'] = 'Memcached';
    $config['cache']['backendOptions'] = array(
     'compression' => false,
     'servers' => array(
      array(
       // your memcached server IP /address
       'host' => 'localhost',
       
       // memcached port
       'port' => 11211,
      )
     )
    );
    
    Memcached is running on the server, Memcached stats returns the following:

    Code:
    127.0.0.1:11211  Field      Value
            accepting_conns          1
                  auth_cmds          0
                auth_errors          0
                      bytes          0
                  bytes_read          40
              bytes_written        819
                  cas_badval          0
                    cas_hits          0
                  cas_misses          0
                  cmd_flush          0
                    cmd_get          0
                    cmd_set          0
                conn_yields          0
      connection_structures          6
            curr_connections          5
                  curr_items          0
                  decr_hits          0
                decr_misses          0
                delete_hits          0
              delete_misses          0
                  evictions          0
                    get_hits          0
                  get_misses          0
                  incr_hits          0
                incr_misses          0
              limit_maxbytes    67108864
        listen_disabled_num          0
                        pid        5458
                pointer_size          64
              rusage_system    0.160010
                rusage_user    0.244015
                    threads          4
                        time  1341136972
          total_connections          8
                total_items          0
                      uptime      30049
                    version      1.4.4
    
    What am I doing wrong please??
     
  2. CyclingTribe

    CyclingTribe Well-Known Member

    Yours looks different to mine, especially 'frontendOptions' - here's mine so you can compare (maybe copy the one below, adjust for your own preferences and give it a try?):

    PHP:
    $config['cache']['enabled'] = true;
    $config['cache']['frontend'] = 'Core';
    $config['cache']['frontendOptions'] = array('caching'  =>  true,
                                                
    'automatic_serialization' => true,
                                                
    'lifetime'    => 1800,
                                                
    'cache_id_prefix'  =>  'xf_'
    );
    $config['cache']['backend'] = 'Memcached';
    $config['cache']['backendOptions'] = array(
                
    'servers' =>array(
                                array(
                                    
    'host' => '127.0.0.1'// your memcached server ip /address
                                    
    'port' => 11211        // memcached port
                                
    )
                            ),
                
    'compression' => false
    );
    Cheers,
    Shaun :D
     
  3. Slavik

    Slavik XenForo Moderator Staff Member

    Stupid question, but have you installed the pecl php-memcache extention?
     
  4. OldCoals

    OldCoals Active Member

    I still have the same error I am afraid.
     
  5. OldCoals

    OldCoals Active Member

    I assume so as I am seeing Memcache stats if I query them.
     
  6. Slavik

    Slavik XenForo Moderator Staff Member

    In ssh run

    Code:
    php -m
    And see if it has memcache listed.
     
  7. OldCoals

    OldCoals Active Member

    No, it isn't running.

    i have just checked EasyApache and the Memcache option is ticked.
     
  8. MattW

    MattW Well-Known Member

    Do you have the extension loaded in your php.ini file?

    extension = "memcache.so"

    or add it to the comma separated list in WHM.

    I'm using CPANEL also:
    Code:
    php -m
    [PHP Modules]
    apc
    bcmath
    calendar
    Core
    ctype
    curl
    date
    dom
    ereg
    filter
    ftp
    gd
    hash
    iconv
    imap
    json
    libxml
    memcache
    mysql
    mysqli
    pcre
    Phar
    posix
    Reflection
    session
    SimpleXML
    sockets
    SPL
    SQLite
    standard
    timezonedb
    tokenizer
    xml
    xmlreader
    xmlwriter
    zlib
    
     
  9. Slavik

    Slavik XenForo Moderator Staff Member

    Assuming your on centos, you should be able to run

    yum install php-pecl-memcache

    Then restart apache.
     
  10. OldCoals

    OldCoals Active Member

    Thanks guys, what is generating the stats i posted earlier then if memcache isn't running?
     
  11. MattW

    MattW Well-Known Member

    memcache itself will be running, but the part required to get php to talk to the memcache server appears to be missing.
     
  12. MattW

    MattW Well-Known Member

    Also, if yum doesn't work installing php-pecl memcache, I documented my manual installation:

    Code:
    cd ~/memcache
    wget http://pecl.php.net/get/memcache-3.0.6.tgz
    tar xvfz memcache-3.0.6.tgz
    cd memcache-3.0.6
    phpize
    ./configure
    make
    make install
    vim /usr/local/lib/php.ini
    #Add this 
    extension=memcache.so
     
    JVCode, OldCoals and gordy like this.
  13. OldCoals

    OldCoals Active Member

    It is working now, I followed your instructions Matt.

    Thanks everyone for their replies.
     
    CyclingTribe and MattW like this.
  14. MattW

    MattW Well-Known Member

    Good stuff, glad we could help (y)
     
  15. craigiri

    craigiri Well-Known Member

    Help another newbie along here....

    I installed as per the yum, but could not locate or start, so I did it manually.

    That worked - I added the line to the php.ini, restarted apache and now it shows up in the php -m

    BUT, I cannot connect to it via telnet, nor with the memcache admin tool (php based) distributed by goog.

    ps aux | grep memcached
    root 13179 0.0 0.0 61216 768 pts/1 S+ 14:32 0:00 grep memcached

    It looks like it has not bound to a port/IP...????

    I have not tried to use it in XF as I want to make sure it's installed and running properly first!
     
  16. MattW

    MattW Well-Known Member

    I don't think your memcached is running. All your grep has found is the grep itself.

    Have you only installed the pecl memcache, or the full memcached package?
     
  17. MattW

    MattW Well-Known Member

    You should see something like this with ps -ef

    Code:
    [~]# ps -ef | grep memcache
    nobody    2946     1  0 Jun16 ?        00:00:00 /usr/local/bin/memcached -u root -m 32 -p 11211 -u nobody -l 127.0.0.1
    z22se    20753 20706  0 20:02 pts/0    00:00:00 grep memcache
    
     
  18. craigiri

    craigiri Well-Known Member

    Nah, I guess it's not running -
    ps -ef | grep memcache
    root 14029 7557 0 15:04 pts/1 00:00:00 grep memcache

    Funny since it first didn't show up in the php -m and then when I installed via yum it gave off an error, but when I installed via your manual commands there was no error on httpd restart and the entire directory and programs are clearly "there".

    So the current install is as per the manual commands above. I just tried to uncomment the TCP port in the memcache.ini in the startup script folder, but no difference.

    My server is managed so I can get my admin to do - just that this thread gave me the faith to give it a shot!

    PHP info shows the file was parsed and loaded and has info on it:
    memcache supportenabled
    Version 3.0.6
    Revision $Revision: 310129 $


    DirectiveLocal ValueMaster Value
    memcache.allow_failover 1 1
    memcache.chunk_size 32768 32768
    memcache.compress_threshold 20000 20000
    memcache.default_port 11211 11211
    memcache.hash_function crc32 crc32
    memcache.hash_strategy consistent consistent
    memcache.lock_timeout 15 15
    memcache.max_failover_attempts 20 20
    memcache.protocol ascii ascii
    memcache.redundancy 1 1
    memcache.session_redundancy 2 2
     
  19. MattW

    MattW Well-Known Member

    Memcached is a daemon that you need to start.

    How did you installed memcached (and not memcache which my instructions are referring to)?
     
  20. MattW

    MattW Well-Known Member

    These are my notes from installing memcached:

    Make the source folder
    Code:
    mkdir ~/memcache
    
    Compile and Install LibEvent
    Memcache requires LibEvent
    Code:
    cd ~/memcache
    wget http://www.monkey.org/~provos/libevent-1.4.9-stable.tar.gz
    tar xvfz libevent-1.4.9-stable.tar.gz
    cd libevent-1.4.9-stable
    ./configure; make; make install
    
    Compile and Install Memcache
    Code:
    cd ~/memcache
    wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
    tar xvfz memcached-1.2.6.tar.gz
    cd memcached-1.2.6
    ./configure --with-lib-event=/usr/local/; make; make install
    
    Starting Memcache
    Code:
    memcached
    
    I got the following error
    Code:
    error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
    
    So you need to register LibEvent
    Code:
    vim /etc/ld.so.conf.d/libevent-i386.conf
     
    #Add this
    /usr/local/lib/
    
    Then run:
    Code:
    # ldconfig
    
    You should then be able to start memcached
    Code:
    # memcached
    
    I got the following error
    Code:
    can't run as root without the -u switch
    
    This is because you need to run memcached as a user

    Code:
    memcached -d -u nobody -m 1024 -l 127.0.0.1 -p 11211
    
    This starts memcached as a daemon (-d) under user "nobody" (-u nobody) using 1GB of memory (-m 1024) listening on the loopback device (-l 127.0.0.1) on port 11211 (-p 11211). A quick check shows that we're up and listening for an application to store and retrieve information:

    Code:
    50183 ?? Ss 0:00.11 bin/memcached -d -u nobody -m 1024 -l 127.0.0.1 -p 11211 tcp4 0 0 localhost.11211 *.* LISTEN
    
    Installing PHP Memcache
    Code:
    cd ~/memcache
    wget http://pecl.php.net/get/memcache-3.0.3.tgz
    tar xvfz memcache-3.0.3.tgz
    cd memcache-3.0.3
    phpize
    ./configure
    make
    make install
    
    Then add the extension into the php.ini file
    Code:
    vim /usr/local/lib/php.ini
     
    #Add this
    extension=memcache.so
    
    Then restart apache

    Then start memcached as a daemon via script

    Create the configuration file
    Code:
    touch /etc/memcached.conf
    
    Then add the below
    Code:
    #Memory 1GB
    -m 1024
    # default port
    -p 11211
    # user to run daemon nobody/apache/www-data
    -u nobody
    # only listen locally
    -l 127.0.0.1
    
    Create the startups files
    Code:
    touch /etc/init.d/memcached
    chmod +x /etc/init.d/memcached
    
    Add the below:
    Code:
    #!/bin/bash
    #
    # memcached    This shell script takes care of starting and stopping
    #              standalone memcached.
    #
    # chkconfig: - 80 12
    # description: memcached is a high-performance, distributed memory
    #              object caching system, generic in nature, but
    #              intended for use in speeding up dynamic web
    #              applications by alleviating database load.
    # processname: memcached
    # config: /etc/memcached.conf
    # Source function library.
    . /etc/rc.d/init.d/functions
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/local/bin/memcached
    DAEMONBOOTSTRAP=/usr/local/bin/start-memcached
    DAEMONCONF=/etc/memcached.conf
    NAME=memcached
    DESC=memcached
    PIDFILE=/var/run/$NAME.pid
    [ -x $DAEMON ] || exit 0
    [ -x $DAEMONBOOTSTRAP ] || exit 0
    RETVAL=0
    start() {
    echo -n $"Starting $DESC: "
    daemon $DAEMONBOOTSTRAP $DAEMONCONF
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $PIDFILE
    echo
    return $RETVAL
    }
    stop() {
    echo -n $"Shutting down $DESC: "
    killproc $NAME
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $PIDFILE
    return $RETVAL
    }
    # See how we were called.
    case "$1" in
    start)
      start
      ;;
    stop)
      stop
      ;;
    restart|reload)
      stop
      start
      RETVAL=$?
      ;;
    status)
      status $prog
      RETVAL=$?
      ;;
    *)
      echo $"Usage: $0 {start|stop|restart|status}"
      exit 1
    esac
    exit $RETVAL
    
    Code:
    touch /usr/local/bin/start-memcached
    chmod +x  /usr/local/bin/start-memcached
    
    Add the below:
    Code:
    #!/usr/bin/perl -w
    # start-memcached
    # 2003/2004 - Jay Bonci <jaybonci@debian.org>
    # 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 $etcfile = shift || "/etc/memcached.conf";
    my $params = [];
    my $etchandle;
    # 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/local/bin/memcached";
    my $pidfile = "/var/run/memcached.pid";
    # 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 =~ s/\#.*//go;
      $line = join ' ', split ' ', $line;
      next unless $line;
      next if $line =~ /^\-[dh]/o;
      if ($line =~ /^[^\-]/o) {
      my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/;
      $conf_directives->{$directive}->($arg);
      next;
      }
      push @$params, $line;
    }
    }
    unshift @$params, "-u root" unless (grep $_ eq '-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);
    } elsif (open PIDHANDLE,">$pidfile") {
    print PIDHANDLE $pid;
    close PIDHANDLE;
    } else {
    print STDERR "Can't write pidfile to $pidfile.\n";
    }
    
    test the scripts
    Code:
    # /etc/init.d/memcached restart
    Shutting down memcached:                                  [  OK  ]
    Starting memcached:                                        [  OK  ]
    
    Review to make sure the config file was parsed OK
    Code:
    # ps aux  | grep memcached
    nobody    5966  0.5  0.3 18248 16444 pts/0  S    13:55  0:00 /usr/local/bin/memcached -u root -m 16 -p 11211 -u nobody -l 127.0.0.1
    
    Add memcached to autostart daemon
    Code:
    # /sbin/chkconfig memcached on
    # /sbin/chkconfig --list | grep memcached
    memcached      0:off  1:off  2:on    3:on    4:on    5:on    6:off
    
     
    Jarod, faeronsayn and JVCode like this.

Share This Page