Call us Toll-Free:
1-800-218-1525
Email us

 Sponsors

How to install Memcached

Dawn Rossi, 03-08-2007
Memcached is a is a great program that allows you to store data that was previously retrieved from another source (usually a database) in your system memory. We recently decided to install it on a server that has some extra memory to try and lighten the load on our database.

Here is the relevant software that I'm currently using on this machine:

OS: FreeBSD 4.7-release-p28
Compiler: gcc version 2.95.4 20020320 [FreeBSD]


And here's what I did to get memcached installed properly:

First I created a directory to work in while installing our new software and downloaded the source packages that we need to get started. Here are the files I used:

http://www.danga.com/memcached/dist/...d-1.2.1.tar.gz
http://monkey.org/~provos/libevent-1.3b.tar.gz
http://pecl.php.net/get/memcache-2.1.0.tgz

First we need to unpack everything and install libevent before memcached:

[root@softwareprojects /home/chris/memcached]# for i in `ls`; do tar -zxf $i; done;

I decided not to install libevent with the default binary and library locations due to some problems I've had in the past with using shared libraries with this kernel. So to make life easier I chose to install it to /usr/local by adding the --prefix flag to configure:

[root@softwareprojects /home/chris/memcached/libevent-1.3b]# ./configure --prefix=/usr/local/libevent
[root@softwareprojects /home/chris/memcached/libevent-1.3b]# make

Ok, here's our first hiccup:

regress.c: In function `test_evbuffer':
regress.c:504: syntax error before `struct'
regress.c:506: `evb' undeclared (first use in this function)
regress.c:506: (Each undeclared identifier is reported only once
regress.c:506: for each function it appears in.)
*** Error code 1

Guess our compiler doesn't like this function. This should do the trick:

[root@softwareprojects /home/chris/memcached/libevent-1.3b]# diff -u test/regress-orig.c test/regress.c
--- test/regress-orig.c Tue Mar 6 04:35:27 2007
+++ test/regress.c Tue Mar 6 04:38:03 2007
@@ -499,9 +499,10 @@

void
test_evbuffer(void) {
+ struct evbuffer *evb;
setup_test("Evbuffer: ");

+ evb = evbuffer_new();
- struct evbuffer *evb = evbuffer_new();

evbuffer_add_printf(evb, "%s/%d", "hello", 1);

Looks good. I tried to compile again and got some more compiler errors:

regress_http.c: In function `http_readcb':
regress_http.c:134: syntax error before `int'
regress_http.c:136: `done' undeclared (first use in this function)
regress_http.c:136: (Each undeclared identifier is reported only once
regress_http.c:136: for each function it appears in.)
regress_http.c: In function `http_basic_cb':
regress_http.c:168: syntax error before `struct'
regress_http.c:169: `evb' undeclared (first use in this function)
regress_http.c: In function `http_post_cb':
regress_http.c:405: syntax error before `struct'
regress_http.c:406: `evb' undeclared (first use in this function)
*** Error code 1

Heres the fix:

[root@softwareprojects /home/chris/memcached/libevent-1.3b]# diff -u test/regress_http-orig.c test/regress_http.c
--- test/regress_http-orig.c Tue Mar 6 04:44:50 2007
+++ test/regress_http.c Tue Mar 6 04:46:59 2007
@@ -125,13 +125,14 @@
http_readcb(struct bufferevent *bev, void *arg)
{
const char *what = "This is funny";
+ int done;

event_debug(("%s: %sn", __func__, EVBUFFER_DATA(bev->input)));

if (evbuffer_find(bev->input, (const unsigned char*) what, strlen(what)) != NULL) {
struct evhttp_request *req = evhttp_request_new(NULL, NULL);
req->kind = EVHTTP_RESPONSE;
+ done = evhttp_parse_lines(req, bev->input);
- int done = evhttp_parse_lines(req, bev->input);

if (done == 1 &&
evhttp_find_header(req->input_headers,
@@ -163,9 +164,10 @@
void
http_basic_cb(struct evhttp_request *req, void *arg)
{
+ struct evbuffer *evb;
event_debug(("%s: calledn", __func__));

+ evb = evbuffer_new();
- struct evbuffer *evb = evbuffer_new();
evbuffer_add_printf(evb, "This is funny");

evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
@@ -380,6 +382,7 @@
void
http_post_cb(struct evhttp_request *req, void *arg)
{
+ struct evbuffer *evb;
event_debug(("%s: calledn", __func__));

/* Yes, we are expecting a post request */
@@ -402,7 +405,7 @@
exit(1);
}

+ evb = evbuffer_new();
- struct evbuffer *evb = evbuffer_new();
evbuffer_add_printf(evb, "This is funny");

evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);

It worked! Next I installed libevent by running "make install". It installed everything to /usr/local/libevent like I asked. Now lets move on to memcached.

Again, to keep things more organized I'm installing memcached to /usr/local. We also have to tell configure where we installed libevent:

[root@softwareprojects /home/chris/memcached/memcached-1.2.1]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/

This time I didn't have to patch any of the code. "make" and "make install" went off without any problems.
Next I tried to run memcached but ran into another error:

[root@softwareprojects /usr/local/memcached]# bin/memcached
/usr/libexec/ld-elf.so.1: Shared object "libevent-1.3b.so.1" not found

I created a symlink to this object in our library directory to circumvent this problem:

[root@softwareprojects /usr/local/memcached]# ln -s /usr/local/libevent/lib/libevent-1.3b.so.1 /usr/local/lib

Now we're in business:

[root@softwareprojects /usr/local/memcached]# bin/memcached
can't run as root without the -u switch

Here are the correct flags to pass to memcached:

[root@softwareprojects /usr/local/memcached]# bin/memcached -d -u www -m 1024 -l 127.0.0.1 -p 11211

This starts memcached as a daemon (-d) under user "www" (-u www) 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:

50183 ?? Ss 0:00.11 bin/memcached -d -u www -m 1024 -l 127.0.0.1 -p 11211
tcp4 0 0 localhost.11211 *.* LISTEN


Next, we need to install the memcache PHP extension (memcache-2.1.9.tgz).

[root@softwareprojects /home/chris/memcached/memcache-2.1.0]# phpize
Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20050606

On compile I got a bunch of openssl errors. It appears its a bug with this release. I found the solution and implemented the suggested fix and it worked great:

[root@softwareprojects /home/chris/memcached/memcache-2.1.0]# diff -u memcache-orig.c memcache.c
--- memcache-orig.c Mon Oct 9 16:11:59 2006
+++ memcache.c Tue Mar 6 06:08:12 2007
@@ -32,8 +32,10 @@
#endif

#if HAVE_MEMCACHE
+#define free_func zlib_free_func
+#include
+#undef free_func

-#include
#include
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"

If you are interested in why this had to be done, you can find the bug details here:
http://pecl.php.net/bugs/bug.php?id=3592

With this patch "make" runs without a problem. I copied the module to our existing extensions directory:

[root@softwareprojects /home/chris/memcached/memcache-2.1.0]# cp modules/memcache.so /usr/local/lib/php/extensions/current/

Next, I had to add the extension to php.ini by adding "extension=memcache.so" near the rest of the extensions. A quick restart of apache, and we're ready to test this thing!


// init
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die("Could not connect to memcache");

// print ver
echo "Memcache Version: ".$memcache->getVersion()."rn";
ob_flush(); flush();

// create a key and some data to store in memcache
$key = "mykey";
$data = new stdClass;
$data->mydata = "testing";

// save to memcache
$memcache->set($key, $data, false, 10) or die("Could not save data to memcache");
echo "stored data in cache!rn";
ob_flush(); flush();

// get from memcache
$result = $memcache->get($key);
echo "data dump from cache:rn";
var_dump($result);

?>

Here's the result:

Memcache Version: 1.2.1
stored data in cache!
data dump from cache:
object(stdClass)(1) {
["mydata"]=>
string(7) "testing"
}

Looks like we're in business!

I'll be adding more posts about memcached in the near future - check back soon!

mosh, 10-18-2008
nice artice
i just install it on my dedicted server
now server load its very nice :)

thx and greetings

reyzer, 12-02-2009
Thanks for install manual
memcached - cool :)

Viren Ajmera, 03-23-2011
Excellent Article..
Saved my loads of time :)

Mike, 07-25-2011
Hi,

Please help me, How to migrate memcache from one machine to another. I mean I need the data in memcached migrated. I am using slackware13.1 linux

By

MIKE
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions