Configure MySQL to start on boot with systemd

  1. Add a systemd service unit configuration file with details about the MySQL service

    $> cd /usr/lib/systemd/system
    $> touch mysqld.service
    $> chmod 644 mysqld.service

    Add this configuration information to the mysqld.service file

    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(7)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    Type=forking
    PIDFile=/usr/local/mysql/data/mysqld.pid

    # Disable service start and stop timeout logic of systemd for mysqld service.
    TimeoutSec=0

    # Start main service
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS

    # Use this to switch malloc implementation
    EnvironmentFile=-/etc/sysconfig/mysql

    # Sets open_files_limit
    LimitNOFILE = 5000
    Restart=on-failure
    RestartPreventExitStatus=1
    PrivateTmp=false
  2. Add a configuration file for the systemd tmpfiles feature.

    $> cd /usr/lib/tmpfiles.d
    $> touch mysql.conf
    $> chmod 644 mysql.conf

    Add this configuration information to the mysql.conf file.

    d /usr/local/mysql/data 0750 mysql mysql -
  3. Enable the mysqld service to automatically start at reboot.

    $> systemctl enable mysqld.service --now
    $> systemctl status mysqld