• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Set Environment Variables for a systemd Service in CentOS/RHEL 7

by admin

Question: How to do set environment variables for use with a systemd service of MySQL Server?

There are a number of different methods that can be used to set environment variables for a systemd service. The method used can vary depending on the Linux distribution and the version of the distribution. The MySQL manual states the service setup is done simply as (example only):

[Service]
LimitNOFILE=max_open_files
PIDFile=/path/to/pid/file
Nice=nice_level
LimitCore=core_file_limit

Method 1 – Using ‘systemd edit’

This is the preferred method which is to create and/or edit an override file that contains the environment variables. The command to create/edit the override file is:

# systemctl edit mysqld

This will then create a directory (if not existing) and create an override.conf file which will contain the variables. e.g

[Service]
Environment="TZ=time_zone_setting"
Environment="AUTHENTICATION_PAM_LOG=1"
Environment="LD_PRELOAD=/path/to/malloc/library"

The override file and directory will usually be: /etc/systemd/system/mysqld.service.d/override.conf

Method 2 – Add multiple variables via a file

This method allows multiple environment variables to be added to the service via a simple file.

[Service]
LimitNOFILE=max_open_files
PIDFile=/path/to/pid/file
Nice=nice_level
LimitCore=core_file_limit
EnvironmentFile=-/etc/sysconf/mysqld

The ‘–‘ character at the start of the EnvironmentFile value is to let systemd ignore errors if the file does not exist. The environment file will simply have the list of different variables in the name=value format. For example:

LD_PRELOAD=/path/to/malloc/library
TZ=time_zone_setting
AUTHENTICATION_PAM_LOG=1

The environment file can be any, as defined by the EnvironmentFile attribute in the service, however, it could be specific to the version and distribution of Linux. Some older versions will expect it to be in /etc/sysconf (for Redhat-based distributions) versus /etc/default (for Debian-based distributions). This is the method currently used by the Oracle MySQL packages.

Method 3 – Add the environment variable directly

This is simply to add a name=value setting into the [Service] are of the mysql service file.

[Service]
LimitNOFILE=max_open_files
PIDFile=/path/to/pid/file
Nice=nice_level
LimitCore=core_file_limit
Environment="LD_PRELOAD=/path/to/malloc/library"

The ‘Environment‘ keyword here is used to identify what environment variable is specified. In the setting above, the environment variable is defined to use a different malloc library for the MySQL Server.

Filed Under: CentOS/RHEL 7, CentOS/RHEL 8, Linux

Some more articles you might also be interested in …

  1. getenforce: command not found
  2. How to install rsyslog7 when rsyslog5 is already installed in CentOS/RHEL
  3. lrzip: command not found
  4. CentOS / RHEL 7 : Lock User Account After N Number of Incorrect Login Attempts
  5. How to configure EPEL repository in OEL 7
  6. fprintd-enroll: command not found
  7. How To Identify User Deleting Files From A Given Directory in Linux
  8. fuser Command Examples in Linux
  9. rsync Command Examples in Linux
  10. swapoff: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • csslint: A linter for CSS code
  • csh: The shell (command interpreter) with C-like syntax (Command Examples)
  • csc: The Microsoft C# Compiler (Command Examples)
  • crystal: Tool for managing Crystal source code

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright