Proper Ways to Perform a Graceful Reboot on CentOS Linux for Database Stability


3 views

Many sysadmins default to shutdown -r now for CentOS restarts, but this can cause abrupt termination of critical services like MySQL. The issue stems from how this command handles running processes:

# This gives processes only 5 seconds to terminate before force-killing
shutdown -r now

1. Using reboot with Service Management

The reboot command is actually preferable as it follows proper shutdown sequences:

# Proper way to reboot with service handling
systemctl stop mysql.service
sync
reboot

2. Systemd's Reboot Command

For modern CentOS 7+ systems:

# This initiates a proper systemd shutdown sequence
systemctl reboot

Custom Delay for Critical Services

Create a custom reboot script with service handling:

#!/bin/bash
# Graceful reboot script
echo "Stopping MySQL gracefully..."
systemctl stop mysql.service
sleep 10  # Allow time for transactions to complete
echo "Syncing filesystems..."
sync
echo "Initiating reboot..."
systemctl reboot

Checking Active Connections Before Reboot

For database servers, always verify connections:

# Check MySQL connections before reboot
mysql -e "SHOW PROCESSLIST" | grep -v "Sleep"
if [ $? -eq 0 ]; then
    echo "Active connections found - aborting reboot"
    exit 1
else
    systemctl reboot
fi

Use journalctl to check reboot logs:

journalctl -b -1 | grep "shutdown"

This helps identify if services were properly terminated before the reboot.


When managing CentOS servers, especially those running database services like MySQL/MariaDB, the standard shutdown -r now command can sometimes cause unexpected issues. The abrupt termination may lead to:

  • Database corruption
  • Uncommitted transactions
  • Replication synchronization problems

Here are the most graceful restart approaches ranked by safety:

# 1. The gold standard for database servers
systemctl reboot --message="Planned maintenance" --no-wall

# 2. Alternative systemd method (CentOS 7+)
systemctl start reboot.target

# 3. Traditional method with delay
shutdown -r +5 "Database maintenance in 5 minutes"

# 4. Emergency fallback (not recommended for production)
reboot --force

For MySQL/MariaDB servers, always run these commands first:

# Flush all operations to disk
mysql -e "FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = ON;"
# Verify no active connections
mysqladmin processlist
# For replication setups
mysql -e "STOP SLAVE;"

Create a restart script (/usr/local/bin/safe_restart.sh):

#!/bin/bash

# Put MySQL in read-only mode
mysql -e "SET GLOBAL read_only = ON;" &> /dev/null

# Stop database service properly
systemctl stop mariadb

# Wait for disk sync
sync

# Execute reboot
systemctl reboot

Make it executable:

chmod +x /usr/local/bin/safe_restart.sh

When you absolutely must force a restart but want minimal impact:

# First attempt soft reboot
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

# As last resort (may cause fsck on next boot)
echo u > /proc/sysrq-trigger  # Remount filesystems read-only
echo b > /proc/sysrq-trigger  # Reboot

After restarting, verify database integrity:

# Check MySQL error log
tail -n 50 /var/log/mysql/error.log

# Verify tables
mysqlcheck --all-databases --check-upgrade --auto-repair