memcached: Failed To Set rlimit For Open Files Error and Solution

I‘m using RHEL 6 / CentOS 6.x and install the memcached server. However, whey I try to start the server using service memcached start command, I get the following error:

ADVERTISEMENTS

Starting memcached: failed to set rlimit for open files. Try running as root or requesting smaller maxconns value. [FAILED]

How do I fix this problem?

Linux comes with per-process file and system-wide file system descriptor limits. Each user has per-process file descriptor limits. The default is set to 1024 including the hard limit, which is also set to 1024. Only root user can increase the hard limit. In my experience you need to to increase this when starting the memcached server.

More About Memcached

The memcached server is run as memcached user. You can verify this by visiting the /etc/passwd file, enter:
$ less /etc/passwd
$ grep -i memcached /etc/passwd

The default config file is located at /etc/sysconfig/memcached:
$ cat /etc/sysconfig/memcached
Sample outputs:

PORT="11211"
USER="memcached"
MAXCONN="4096"
CACHESIZE="256"
OPTIONS="-l 192.168.1.100"

Set Per-process File Descriptor Limits For Memcached

Edit /etc/security/limits.conf file, enter:
# vi /etc/security/limits.conf
Set max number of open files for memcached user as follows:

#"soft" for enforcing the soft limits
#"hard" for enforcing hard limits
# "nofile" max open file
# *********************************************************************
# * Note soft limit must be >= MAXCONN value (defined in /etc/sysconfig/memcached *
# *********************************************************************
# Username      type    item           value
memcached       soft    nofile          5000
memcached       hard    nofile         6144

Save and close the file. You need to logout and login again. Now type the following command to start the memcached server:
# /sbin/service memcached start
Please note that you can set global limit for all process by replacing the memcached with * as follows:

# Username      type    item           value
*       soft    nofile          5000
*       hard    nofile         6144
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • Paul M Nov 4, 2011 @ 14:18

    change the system wide ulimit for files?

    • Dan Nov 22, 2011 @ 14:51

      That would interfere with everything outside of memcached, as well.
      Vivek is showing a way to limit it only for a particular application.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.