How to Install Memcached

LawrenceLawrence Moderator
edited May 12 in Linux Applications

Introduction

Memcached is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Nearly every popular CMS has a plugin or module to take advantage of memcached, and many programming languages have a memcached library, including PHP, Perl, Ruby, and Python. Memcached runs in memory and is thus quite speedy, since it does not need to write data to disk.


Installation

We can install Memcached by running this command:

apt-get install memcached -y


Configuration

Use the following command to edit the default configuration file /etc/memcached.conf:

nano /etc/memcached.conf

You will see default configurations of memcached, such as default port (11211), cap of memory for memcached in megabytes (64) and so on.

Most common variables to edit:

p - port in which memcached is running
m - cap of memory for memcached
c - value of maximum connections

An example how should look configuration file if we want memcached to run on port 12345, to use 2 GB of RAM and have 1024 allowed maximum connections:

# memcached default config file
# 2003 - Jay Bonci
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d

# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log

# Be verbose
# -v

# Be even more verbose (print client commands as well)
# -vv

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 2048

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache

# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1

# Limit the number of simultaneous incoming connections. The daemon default is 1024
-c 1024

# Lock down all paged memory. Consult with the README and homepage before you do this
# -k

# Return error when memory is exhausted (rather than removing items)
# -M

# Maximize core file limit
# -r



Finalizing

After we had configured mamached to work with parameters we want we can start it:

service memcached start

We can test is it working with commands

service memcached status

or

pgrep memcached
netstat -tulpn | grep :11211

Conclusion

That's it! You had installed and configured basics of memcached. So, now you have very useful program which can do a lot to increase the server efficiency.
Tagged:
Sign In or Register to comment.