How to use Memcached in a UNIX Socket

Memcached can run in a UNIX socket, which provides better performance than a TCP connection.

Note: If Memcached fails to start, it is usually due to permission and user problems. Please use root privilege to execute the following instructions, and verify that the socket path is writable to the designated user.

  1. Stop Memcached
    systemctl stop memcached
  2. Copy the service file
    cp /usr/lib/systemd/system/memcached.service /etc/systemd/system/memcached.service
  3. Add the following content to /etc/systemd/system/memcached.service. After [Service], please change username to the same user that runs PHP:
    User=username
    Group=username

    The contents of the file should look like this:

  4. Edit /etc/sysconfig/memcached, changing the path to your desired location, and the username to the same one used in Step 3:
    OPTIONS=""
    USER="memcached"

    becomes

    OPTIONS="-s /path/to/memcached.sock -a 0770"
    USER="username"
  5. Start Memcached again:
    systemctl start memcached
  6. Verify it started successfully:
    systemctl status memcached
  7. Check if everything is working well:
    nc -U /path/to/memcached.sock
    stats
  1. Stop Memcached
    systemctl stop memcached
  2. Edit /etc/sysconfig/memcached and change
    OPTIONS=""
    USER=""

    to

    OPTIONS="-s /path/to/memcached.sock -a 0770"
    USER="username"

    where userid is the same user that runs PHP.

  3. Start Memcached
    service memcached start
  4. Check if everything is working well:
    nc -U /path/to/memcached.sock
    stats
  1. Stop Memcached
    systemctl stop memcached
  2. Edit /etc/memcached.conf, comment out host and port, add socket path and permission
    -s /path/to/memcached.sock
    -a 0770

    and change -u memcache to -u username where username is the same user that runs PHP.

  3. Start Memcached again
    systemctl start memcached
  4. Check if everything is working well:
    nc -U /path/to/memcached.sock
    stats
  1. Stop Memcached
    service memcached stop
  2. Edit /etc/memcached.conf, comment out host and port, add socket path and permission
    -s /path/to/memcached.sock
    -a 0770

    and change -u memcache to -u username where username is the same user that runs PHP.

  3. Start Memcached again
    service memcached start
  4. Check if everything is working well:
    nc -U /path/to/memcached.sock
    stats
  • Admin
  • Last modified: 2018/01/18 15:56
  • by Lisa Clarke