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
  8. If there is still a permission issue, please check selinux status:
    getenforce
  9. Disable selinux if status shows Enforcing:
    setenforce 0 

    (reboot will re-enable selinux)

  10. To permanently disable selinux, edit /etc/selinux/config, change enforcing to permissiveordisabled and then reboot.
  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 USER 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
  5. If there is still a permission issue, please check selinux status:
    getenforce
  6. Disable selinux if status shows Enforcing:
    setenforce 0 

    (reboot will re-enable selinux)

  7. To permanently disable selinux, edit /etc/selinux/config, change enforcing to permissiveordisabled and then reboot.
  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/22 13:57
  • by Lisa Clarke