Last updated February 25, 2013. Created by catch on April 19, 2011.
Edited by a.ross, jacobson, nerdcore, HongPong. Log in to edit this page.
Installation
These are the broad steps you need to take in order to use this software on Drupal 6.x.Information pertaining to the Drupal 7.x version is also found in the README.txt file from the module distribution. For 6.x (order is important):
- Install the memcached binaries on your server. See
- Install the PECL memcache extension for PHP.
- In php.ini set memcache.hash_strategy="consistent".
- Put your site into offline mode.
- Download and install the memcache module.
- If you have previously been running the memcache module, run update.php.
- Start at least one instance of memcached on your server.
- Edit settings.php to configure the servers, clusters and bins that memcache is supposed to use. (see code snippet below)
- Edit settings.php to include memcache.inc. For example, $conf['cache_inc'] ='sites/all/modules/memcache/memcache.inc';
- Bring your site back online.
For 7.x (order is important):
-
Install the memcached binaries on your server. See
- How to install Memcache on Debian Etch
- How to install Memcache on Ubuntu (this link also provides instructions for installing the PHP memcache extension and configuring the Drupal settings.php file). Alternative instructions for installing & configuring memcached on Ubuntu, including package php5-memcache
- How to install Memcache on openSUSE
- How to install Memcache on OSX
- How to install Memcache Fedora / CentOS / RHEL
- Install the PECL memcache extension for PHP. This must be version 2.2.1 or higher or you will experience errors.
- Put your site into offline mode.
- Download and install the memcache module.
- If you have previously been running the memcache module, run update.php.
- Start at least one instance of memcached on your server.
-
Edit settings.php to make memcache the default cache class, for example:
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['memcache_key_prefix'] = 'something_unique'; - Bring your site back online.
For instructions on 1 and 2 above, please see the INSTALLATION.txt file that comes with the memcache module download.
Servers
NOTE: As of 6.15, it is possible to efficiently run memcache in one daemon, shared by all bins. You can safely ignore advice found elsewhere on the internet that advises one memcached daemon per bin. In terms of reporting, there are still some advantages to having more daemons. If you want the simple version, you can start one default memcache instance on your web server like this: memcached -m 24 -p 11211 -d If that is enough to meet your needs, there is no more configuration needed. If you want to utilize this module's sophisticated clustering feature and spread your cache over several machines, or if your cache is found on a machine other than your web server, read on. You should probably lock down the memcache server so that it only listens for connections from the hosts that need to be served, as the default is that memcache listens to connections from all addresses. So, to close that hole, modify your memcached configuration as follows:
On RHEL/CentOS, edit /etc/sysconfig/memcached:
OPTIONS="-l ${HOSTIP}"
For example:
OPTIONS="-l 127.0.0.1"-l ${HOSTIP}
For example:
-l 127.0.0.1$conf;memcache_servers and memcache_bins. The arrays follow this pattern: <?php
$conf['memcache_servers'] = array(
host1:port => cluster,
host2:port => cluster,
hostN:port => cluster
);
$conf['memcache_bins'] = array(
bin1 => cluster,
bin2 => cluster,
binN => cluster
);
?>- Servers are memcached instances identified by host:port.
- Bins are groups of data that get cached together and map 1:1 to the $table param in cache_set(). Examples from Drupal core are cache_filter, cache_menu. The default is 'cache'.
- Clusters are groups of servers that act as a memory pool.
- Many bins can be assigned to a cluster.
- The default cluster is 'default'.
Here is a simple setup that has two memcached instances, both running on 10.1.1.1. The 11212 instance belongs to the 'pages' cluster and the table cache_page is mapped to the 'pages' cluster. Thus everything that gets cached, with the exception of the page cache (cache_page), will be put into 'default', or the 11211 instance. The page cache will be in 11212.
<?php
$conf['memcache_servers'] = array(
'10.1.1.1:11211' => 'default',
'10.1.1.1:11212' => 'pages'
);
$conf['memcache_bins'] = array(
'cache_page' => 'pages',
);
?><?php
$conf['cache_inc'] = './sites/all/modules/memcache/memcache.inc';
$conf['memcache_servers'] = array(
'10.1.1.1:11211' => 'default',
'10.1.1.1:11212' => 'default',
'10.1.1.2:11211' => 'default',
'10.1.1.3:11211' => 'cluster2',
'10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
'cache' => 'default',
'cache_filter' => 'cluster2',
'cache_menu' => 'cluster2'
);
?><?php
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['memcache_servers'] = array(
'10.1.1.1:11211' => 'default',
'10.1.1.1:11212' => 'default',
'10.1.1.2:11211' => 'default',
'10.1.1.3:11211' => 'cluster2',
'10.1.1.4:11211' => 'cluster2'
);
$conf['memcache_bins'] = array(
'cache' => 'default',
'cache_filter' => 'cluster2',
'cache_menu' => 'cluster2'
);
?>Prefixing
If you want to have multiple Drupal installations share memcached instances, you need to include a unique prefix for each Drupal installation in the $confarray of settings.php:
<?php
$conf['memcache_key_prefix'] = 'something_unique';
?>Sessions
NOTE: Session.inc is not yet ported to Drupal 7. Here is a sample config that uses memcache for sessions. Note you MUST have a session and a users server set up for memcached sessions to work.
<?php
include_once('./includes/cache.inc');
include_once('./sites/all/modules/memcache/memcache.inc');
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['session_inc'] = './sites/all/modules/memcache/memcache-session.inc';
$conf['memcache_servers'] = array(
'localhost:11211' => 'default',
'localhost:11212' => 'filter',
'localhost:11213' => 'menu',
'localhost:11214' => 'page',
'localhost:11215' => 'session',
'localhost:11216' => 'users',
);
$conf['memcache_bins'] = array(
'cache' => 'default',
'cache_filter' => 'filter',
'cache_menu' => 'menu',
'cache_page' => 'page',
'session' => 'session',
'users' => 'users',
);
?>Troubleshooting
PROBLEM: Error: Failed to set key: Failed to set key: cache_page-......
SOLUTION: Upgrade your PECL library to PECL package (2.2.1) (or higher).
PROBLEM: WARNING: Zlib compression at the php.ini level and Memcache conflict.
SOLUTION: See http://drupal.org/node/273824
Memcache Admin
A module offering a UI for memcache is included. It provides stats, a way to clear the cache, and an interface to organize servers, bins, and clusters.
Memcached PECL Extension Support
We also now support the Memcached PECL extension. If you install this extension, it will be used by default. This new extension backends to libmemcached and allows you to use some of the newer advanced features in memcached 1.4. NOTE: It is important to realize that the memcache php.ini options do not impact the memcached extension, this new extension doesn't read in options that way. Instead, it takes options directly from Drupal. Because of this, you must configure memcached in settings.php. Please look here for possible options: http://www.php.net/manual/en/memcached.constants.php An example configuration block is below, this block also illustrates our default options. These will be set unless overridden in settings.php.
<?php
$conf['memcache_options'] = array(
Memcached::OPT_COMPRESSION => FALSE,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
);
?>- Turn off compression, as this takes more CPU cycles than its worth for most users
- Turn on consistent distribution, which allows you to add/remove servers easily
If you are using memcached 1.4 or above, you should enable the binary protocol, which is more advanced and faster, by adding the following to settings.php:
<?php
$conf['memcache_options'] = array(
Memcached::OPT_BINARY_PROTOCOL => TRUE,
);
?>
Comments
Drupal 6
Note that this page is specific to Drupal 6. The way you hook up memcache in Drupal 7 is a bit different, specifically the way you tie into the cache system. See the README.txt file in the module for the updated installation instructions until this page is updated. (Please delete this comment when this page is updated.)
--
Larry Garfield
http://www.garfieldtech.com/blog
http://www.palantir.net/
One important security hole
One important security hole is present in default memcached daemon configuration. That's it listen to connections from all IPs.
So, to close that hole, edit /etc/sysconfig/memcached with
OPTIONS="-l 127.0.0.1" or change IP to whatever suits your case.
A tip when trying to get this
A tip when trying to get this going on Ubuntu 11.04 is that
sudo apt-get install libmemcached(needed for building the PECL extension) does not work, as you'd think it would. Usesudo apt-get install libmemcached-devinstead. This depends on other packages that satisfy the dependency.WizOne Solutions - http://www.wizonesolutions.com - Drupal module development, theme implementation, and more
Fill PDF Service - http://fillpdf-service.com - Hosted solution for Fill PDF
Drupal 7 Prefixing
the above code for Prefixing doesn't work on my D7 setup. it doesn't gave an error message but there's no data passed to memcached. here's what worked for my D7 setup:
$conf['memcache_key_prefix'] = 'something_unique';Gerold Mercadero
Systems Administrator - Promet Solutions Inc.
Drupal Web Development - Hosting - Online Marketing
How to install memcached on openSUSE for use with Drupal
Here is a tutorial on How to install memcached on openSUSE for use with Drupal
Christiaan
Clarification in docs
Do you mean you need to just need to have a session and user table in Drupal for this to work? Or do you you mean you need to have a separate memcache instance just for sessions and users?
Kim
Kim Pepper
~~~~~~~
http://www.previousnext.com.au