Increase number of filedescriptors for services launched by systemd

Before systemd, the system administrator was able to increase the limit of file descriptors (open files) for a given user by editing /etc/security/limits.conf and values to the nofile limits.

We'll take MySQL (or mariadb, as it uses the same username) as an example:

mysql           soft    nofile  32768
mysql           hard    nofile  32768

After that, the mysql user was allowed to use up to 32768 open files. Now, the sysadmin can edit /etc/my.cnf and add:

open_files_limit = 32768
open-files-limit = 32768

After restarting MySQL, you could check that the change was effective:

# mysql -u root -p -e "show global variables like 'open%';"
+------------------+-------+
| open_files_limit | 32768 |
+------------------+-------+

But now, with systemd, this is not enough, and you will get the default:

# mysql -u root -p -e "show global variables like 'open%';"
+------------------+-------+
| open_files_limit |  1024 |
+------------------+-------+

Because now, systemd has it's own limit enforced in the service configuration file.


Increase nofile limits inside systemd service file

In our example, we need now to edit MySQL's systemd service file /usr/lib/systemd/system/mysqld.service, to add the following configuration parameters inside the [Service] section:

## Note: You can also configure "LimitNOFILE=infinity"
LimitNOFILE=32768
LimitMEMLOCK=infinity

But, as @bigonbe pointed out in Twitter, we should not edit the "service" configuration file shipped with the package (as it can be overwritten with package updates). What we need to do is create an configuration override file that allows us to redefine any configuration parameter.

Let's see how to do this for mysqld with systemctl edit:

# systemctl edit mysqld

A text editor will open on the "overrides" file for the mysqld service. In that file, add the following and save:

[Service]
LimitNOFILE=32768
LimitMEMLOCK=infinity

(Don't forget the [Service] tag).

Now, run (although edit will automatically do a daemon-reload for us, doing it again won't hurt systemd):

# systemctl daemon-reload
# systemctl restart mysqld

(Replace mysqld with mariadb if you use MariaDB instead).

You can check now that the change was effective:

# mysql -u root -p -e "show global variables like 'open%';"
+------------------+-------+
| open_files_limit | 32768 |
+------------------+-------+


Other services

Other possible services to "configure" are:

/usr/lib/systemd/system/httpd.service
/usr/lib/systemd/system/nginx.service
/usr/lib/systemd/system/memcached.service
/usr/lib/systemd/system/another_service.service

With:

# systemctl edit httpd
# systemctl edit nginx
# systemctl edit memcached
# systemctl edit another_service

Some of them, like in the case of MySQL's my.cnf, may require additional configuration steps (i.e. configuration directives in the service file) or even executing ulimit -n 32768 in their user .bashrc profile or in the start script.



<Volver a la sección de GNU/Linux>