How to install and configure Memcached on CentOS 7

Memcached is a special open-source memory caching distribution system for objects. It is used to render pages and accelerate dynamic web applications by storing data fragments from database callout results.

Install Memcached

First you need to upgrade your CentOS

yum update -y

Next, install the official Memcached package as well as Libmemcached. It represents several utilities for working with Memcached.

yum install -y memcached libmemcached

To start Memcached at boot, you must use the systemctl command

systemctl enable memcached

Memcached Setup

At this stage, you need to connect Memcached with the local interface and disable the UDP port. This is to avoid potential DDOS attacks. Open the /etc/sysconfig/ memcached file in your favorite editor:

nano /etc/sysconfig/memcached

Next, find the line:

OPTIONS=""

Change this to the following:

OPTIONS="-l 127.0.0.1 -U 0"

Save the file and exit the editor. Next please restart Memcached, to use this changes

systemctl restart memcached

You can verify that it works using systemctl:

systemctl status memcached

The output will look like this:

● memcached.service - Memcached
Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2019-04-04 17:01:41 UTC; 8s ago
Main PID: 31312 (memcached)
CGroup: /system.slice/memcached.service
        └─31312 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 -l 127.0.0.1 -U 0

Apr 04 17:01:41 docs systemd[1]: Started Memcached.

You can verify that Memcached is only connected to the local interface and only listens for TCP connections using the ss command:

ss -plunt | grep memcached

tcp    LISTEN     0      128    127.0.0.1:11211                 *:*                   users:(("memcached",pid=31312,fd=26))

Verify Settings

You have now completed the setup and configuration of Memcached. You can verify the configuration using the memstat command from the libmemcached package

memstat --servers="localhost"

The output will look like this:

Server: localhost (11211)
    pid: 31312
    uptime: 385
    time: 1554397684
    version: 1.4.15
    libevent: 2.0.21-stable
    pointer_size: 64
    rusage_user: 0.006269
    rusage_system: 0.014105
    curr_connections: 1

...

Was this article helpful?

Related Articles