Memcached is a service that allows entire database tables to be stored in memory, drastically speeding up queries to those tables and alleviating database load. In Drupal, the Memcached module allows you to store all cache tables in memory.
This article will guide you through installing memcache on your Mac OS X sandbox machine, allowing you to test memcache deployments locally. For in-depth instructions on installing memcache on a production server, read the companion article on How to install Memcache on Debian Etch. This was installed on Mac OS 10.5.1 and MAMP 1.7 with PHP 5.2.5. If you haven't already installed MAMP for your local sandbox environment, watch the Lullabot videocast on Installing a local web server on Mac OS X
Install the Memcached service on Mac OS X
- Install Xcode Tools from your Mac OS X DVD or download it from Apple Developer Connection to install the GNU Compiler Collection (gcc).
- Download the shell script from topfunky.net (mirror) to your home directory.
- Set the permissions on the shell script and run it as root.
chmod 744 install-memcached.sh sudo ./install-memcached.sh
error: C compiler cannot create executables, be sure Xcode is up-to-date and installed correctly.
- Per the instructions at the end of the script, add
EVENT_NOKQUEUE=1to your shell environment. You can do this by opening (or creating) the .bashrc or .profile file in your home directory and pasting the line at the bottom of the file.
- Check that memcache is installed properly by closing your previous Terminal window and opening a new Terminal session. Type the following command:
memcached -h. You should get a screen similar to this:
nate-macbook:~ nate$ memcached -h memcached 1.1.12 -p <num> port number to listen on -l <ip_addr> interface to listen on, default is INDRR_ANY -d run as a daemon -r maximize core file limit -u <username> assume identity of <username> (only when run as root) -m <num> max memory to use for items in megabytes, default is 64 MB -M return error on memory exhausted (rather than removing items) -c <num> max simultaneous connections, default is 1024 -k lock down all paged memory -v verbose (print errors/warnings while in event loop) -vv very verbose (also print client commands/reponses) -h print this help and exit -i print memcached and libevent license -P <file> save PID in <file>, only used with -d option
- Cleanup your installation by deleting the created src directory and the install-memcached.sh file.
Install Memcache PHP Extension
Option 1: Download the Binary Memcache Extension If you're running the (as of this writing) latest MAMP, you should have PHP 5.2.5 installed. If so, and you're on an Intel Mac, you can just download this compiled extension.
- Download PHP 5.2.5 (Intel) memcache.so
- Install into the /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-[date] directory. The [date] value will vary depending on your MAMP install.
Option 2: Compile the PECL Memcache Extension If you're running a different version of MAMP or are on a PowerPC machine, you'll need to compile a memcache.so specific to you're configuration.
- Download the complete PHP source code from php.net into /Applications/MAMP/bin/php5. You should download the version that matches your version of MAMP, which you can check at http://localhost:8888/MAMP/info.php.
- Create an include directory in the the php5 directory.
- Unzip/tar the php source archive and move it to /Applications/MAMP/bin/php5/include
- Rename the php source directory to just php (so then it will look like /Applications/MAMP/bin/php5/include/php)
- In Terminal use the following commands to compile the extension
cd /Applications/MAMP/bin/php5/include/php ./configure cd /Applications/MAMP/bin/php5/bin ./pecl i memcache
- This creates a new directory in your /Applications/MAMP/bin/php5/lib/php/extensions directory. There should be two directories, each with different dates at the end of their file names such as no-debug-non-zts-20050922. Open the directory with today's date at the end and move the memache.so file to the other directory containing many other *.so files.
- Delete the now empty no-debug-non-zts-[date] directory that contained the memcache.so file.
Enable the Memcache Extension
- Open the /Applications/MAMP/conf/php5/php.ini file and add extension=memcache.so in the
- Restart MAMP from the application or dashboard widget.
- Verify the Memcache extension is loaded by visiting http://localhost:8888/MAMP/info.php
Congratulations! You now have Memcache installed locally so you can easily test your memcache deployments. Installation and usage can then be setup on Drupal by following the instructions provided by the Memcache Project.
Bonus Section: Start and Stop Memcache with MAMP
So now that memcache is installed and available for MAMP to use, you'll need to actually start instances of the memcache daemon for your memcache-enabled site to use. Updating the scripts in /Applications/MAMP/bin with these copies will automatically start 11 instances of memcache for Drupal to use.
- Download each of the following scripts into /Applications/MAMP/bin, replacing existing startApache.sh and stopApache.sh.
- Set the permissions for the scripts to executable for the logged in user.
chmod 775 *.sh
- Stop and start MAMP.
You can now setup your Drupal site to use ports 11211 to 11221 for memcache. Such as this (in settings.php):
$conf = array( 'cache_inc' => 'sites/all/modules/contrib/memcache/memcache.db.inc', 'memcache_servers' => array( 'localhost:11211' => 'default', 'localhost:11212' => 'content', 'localhost:11213' => 'filter', 'localhost:11214' => 'menu', 'localhost:11215' => 'page', 'localhost:11216' => 'views', ), 'memcache_bins' => array( 'cache' => 'default', 'cache_content' => 'content', 'cache_filter' => 'filter', 'cache_menu' => 'menu', 'cache_page' => 'page', 'cache_views' => 'views', ), );
This is not a substitute for instructions on setting up memcache.module, be sure to follow all the instructions in INSTALLATION.txt and README.txt included with memcache.module.