After a recent server reboot, I encountered a frustrating scenario where MySQL failed to start automatically despite having the init script in place. This is a common pain point for sysadmins deploying database servers. Let me walk through the proper configuration methods for both modern and legacy CentOS/RHEL systems.
For systems using systemd (CentOS 7 and later), follow these steps:
# Check current service status
systemctl status mysqld
# Enable auto-start at boot
systemctl enable mysqld
# Verify the setting
systemctl is-enabled mysqld
# Optional: Start immediately without reboot
systemctl start mysqld
For older systems still using SysV init:
# View current runlevel configuration
chkconfig --list mysqld
# Enable for runlevels 2-5
chkconfig --level 2345 mysqld on
# Alternative syntax
chkconfig mysqld on
# Verify the change
chkconfig --list mysqld
If MySQL still won't autostart, check these potential issues:
# Verify init script permissions
ls -l /etc/init.d/mysqld
# Check for error logs
journalctl -u mysqld -b
tail -n 50 /var/log/mysqld.log
# Validate the service file exists
ls /usr/lib/systemd/system/mysqld.service
For complex environments where MySQL needs to wait for storage or networking:
# Create systemd override
mkdir -p /etc/systemd/system/mysqld.service.d
cat > /etc/systemd/system/mysqld.service.d/override.conf <
In modern CentOS/RHEL systems (version 7 and above), service management has transitioned from traditional SysV init to systemd. The presence of both /etc/rc.d/init.d
and systemd unit files can cause confusion about the proper way to enable automatic startup.
First, check if MySQL is currently configured to start on boot:
systemctl is-enabled mysqld
# or for MariaDB
systemctl is-enabled mariadb
If this returns "disabled", you'll need to enable it. For older systems using chkconfig:
chkconfig --list mysqld
For CentOS 7+ systems, use the following commands:
# For MySQL
sudo systemctl enable mysqld
# For MariaDB
sudo systemctl enable mariadb
Verify the change took effect:
systemctl is-enabled mysqld
For CentOS 6 or systems using SysV init:
sudo chkconfig mysqld on
sudo chkconfig --list mysqld
You should see runlevels 2-5 marked as "on":
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
If MySQL still doesn't start automatically:
# Check for failed services
systemctl --failed
# View MySQL service logs
journalctl -u mysqld
Common problems include:
- Incorrect permissions on data directory
- Missing configuration files
- Resource constraints preventing startup
As a fallback method, you can add this to root's crontab:
@reboot /usr/bin/systemctl start mysqld
Edit the crontab with:
sudo crontab -e
After making changes, test by rebooting or simulating with:
sudo systemctl reboot
Then check MySQL status:
systemctl status mysqld