Memcached unix_socket, how to?

Hi,
I’m wondering if someone can write how to use memcached with unix_socket?
I know how to configure it in Seafile, but how do I install it in Ubuntu server?

here my own config and google again for fine tunning :slight_smile:

root@hiperborea ~ # vi /etc/memcached.conf
# 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 64

# 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 (original jobenvil)
-u root

# 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.1i (original)
-s /var/run/memcached.sock
-a 0766

# 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

and here the extract regarding memcached inside seahub_settings.py:

# define memcached:
# dependences ...
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'unix:/var/run/memcached.sock'
    }
}
# remplaced 'LOCATION': '127.0.0.1:11211' by 'LOCATION': 'unix:/media/usbfestplatte/memcached.sock'
3 Likes

Greate thanks, it’s makes it easier to understand.

When I’m looking at my config file I can see this:
# 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

I have not change anything but still it’s running as a user named memcache and not root as it says it should do as default.
Why’s that?

When I’m looking at the /var/log/ I can’t find any memcache.log file.

I have only done the standard setup when you use the HTTP_socket but still, I’m wondering why it uses user memcache insted of root?

maybe wrong interpretation, the service itself should be run like as root, but after the escalation it takes the memcached user

Ok Thanks,
I did take a look at the default configuration and your correct it should be memcache there as default.

Regarding this, do I need to create the memcached.sock or does memcache create it by it self?
I’m asking because I’m not home right now and can try it out by my self.

it will be created by memcache after starting service

When I did have -u memcache it did not work but when I did change it to -u root then everything seem to work perfectly fine.

When I use it with -u memcache I did get this:

seafile@Seafile:~/nohatech$ sudo service memcached status
● memcached.service - memcached daemon
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2017-11-09 09:51:18 CET; 1s ago
Process: 2517 ExecStart=/usr/share/memcached/scripts/systemd-memcached-wrapper /etc/memcached.conf (code=exited, status=71)
Main PID: 2517 (code=exited, status=71)

Nov 09 09:51:18 Seafile systemd[1]: Started memcached daemon.
Nov 09 09:51:18 Seafile systemd-memcached-wrapper[2517]: bind(): Address already in use
Nov 09 09:51:18 Seafile systemd-memcached-wrapper[2517]: failed to listen on UNIX socket: /var/run/memcached.sock: Address already in use
Nov 09 09:51:18 Seafile systemd[1]: memcached.service: Main process exited, code=exited, status=71/n/a
Nov 09 09:51:18 Seafile systemd[1]: memcached.service: Unit entered failed state.
Nov 09 09:51:18 Seafile systemd[1]: memcached.service: Failed with result ‘exit-code’.

And when I’m changing the sock file I get premission denied.

Nov 09 09:53:17 Seafile systemd[1]: Started memcached daemon.
Nov 09 09:53:17 Seafile systemd-memcached-wrapper[2540]: bind(): Permission denied
Nov 09 09:53:17 Seafile systemd-memcached-wrapper[2540]: failed to listen on UNIX socket: /var/run/memcachedtest.sock: Permission denied

But when I’m changeing the location on the sock file to /tmp//tmp/memcached.sock everything seem to work fine.
Is that a security issue to have it in /tmp instead?
As far as I can see it’s only memchace that can do anything with the socket file correct?

seafile@Seafile:~/nohatech/logs$ ls -lh /tmp
total 0
srwxrw-rw- 1 memcache memcache 0 Nov 9 09:58 memcached.sock

Because if I’m running it the other way I need to run memcached as root all the time, that maybe are not a issue?

What you describe is totally right. First, you have to consider which rights has the directory where memcache will create the socket. The second one is that after you have created the socket with “root”, this could not be “destroyed” by “normal” memcache user. Since could not be destroyed, to be recreated in same place, memcache cannot start.

For this reason I have the memcached configuration file with user “root”, since I don’t wanna to put it on /tmp directory and I like to have it under /var/run. Thats’s easy instead of starting to re-adjust the parent directory /run or the rights of memcached (0766).

Thanks for all of your help, I have it up and running now and after some googeling I have also decided to put it in /var/run/ folder.