How to Configure MariaDB Auto-Restart on Crash Using systemd in CentOS 7


7 views

First, a critical clarification - you were editing a symlink rather than the actual service file. The proper location for MariaDB's systemd configuration is:

/usr/lib/systemd/system/mariadb.service

This differs from the symlink you found at /etc/systemd/system/multi-user.target.wants/mariadb.service. Always modify the original service file, not the symlinks in the target directories.

For robust crash recovery, we need multiple parameters in the [Service] section:

[Service]
Restart=always
RestartSec=3s
StartLimitInterval=60s
StartLimitBurst=5

This configuration means:

  • Restart=always: Attempt restart for any exit reason
  • RestartSec: Wait 3 seconds between restart attempts
  • StartLimit: Maximum 5 restarts within 60 seconds

Here's a production-ready configuration example:

[Unit]
Description=MariaDB database server
After=syslog.target
After=network.target

[Service]
Type=notify
User=mysql
Group=mysql

# PATH modifications
EnvironmentFile=-/etc/sysconfig/mysql
ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n
ExecStart=/usr/libexec/mysqld --basedir=/usr $MYSQLD_OPTS
ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID

# Crash recovery settings
Restart=always
RestartSec=3s
StartLimitInterval=60s
StartLimitBurst=5

# Security and resource limits
PrivateTmp=true
LimitNOFILE=65535
LimitMEMLOCK=infinity

[Install]
WantedBy=multi-user.target

Follow this sequence after modifying the service file:

# Reload systemd manager configuration
sudo systemctl daemon-reload

# Verify the new settings
sudo systemctl show mariadb --property=Restart,RestartSec

# Test the configuration
sudo systemctl restart mariadb
sudo pkill -9 mysqld
sleep 5
systemctl status mariadb

For more granular control, consider these alternatives to Restart=always:

Restart=on-failure  # Only restart on non-clean exits
Restart=on-abort    # Only restart on SIGABRT

Combine with exit status checking:

SuccessExitStatus=1  # Consider exit code 1 as success
RestartForceExitStatus=255  # Force restart on code 255

When MariaDB crashes on CentOS 7 systems, the default systemd service configuration doesn't automatically restart it. This becomes problematic for production environments where database availability is critical.

Instead of modifying files in /etc/systemd/system/multi-user.target.wants/, which are symlinks, we should edit the actual service file:


# Check where the service file is actually located
systemctl status mariadb | grep "Loaded"
# Typically shows: /usr/lib/systemd/system/mariadb.service

Create an override file rather than modifying the original package file:


# Create override directory if it doesn't exist
sudo mkdir -p /etc/systemd/system/mariadb.service.d/

# Create override file
sudo nano /etc/systemd/system/mariadb.service.d/override.conf

Add these contents to the override file:


[Service]
Restart=always
RestartSec=5s
StartLimitInterval=0

After making changes, run these commands:


# Reload systemd configuration
sudo systemctl daemon-reload

# Restart MariaDB to apply changes
sudo systemctl restart mariadb

# Verify the service will restart
sudo systemctl show mariadb | grep Restart

To verify the auto-restart works:


# Simulate a crash
sudo kill -9 $(pgrep mariadbd)

# Check status after 5 seconds
sleep 5 && systemctl status mariadb

For mission-critical systems, consider adding these to your override.conf:


[Service]
RestartPreventExitStatus=255
TimeoutStartSec=300
TimeoutStopSec=300