This guide walks you through diagnosing and fixing a MySQL server that refuses to start on CentOS. You’ll learn the most common causes—corrupted data files, permission errors, misconfigured settings, and resource limits—and the exact commands to resolve them. By the end, you’ll have your MySQL service running again, or at least know precisely what’s wrong.
What You’ll Need
- Root or sudo access to the CentOS machine
- Basic comfort with the command line
- Knowledge of your MySQL version (5.7, 8.0, or MariaDB) — most commands are the same
Step-by-Step
- Check if MySQL is actually running. Run
systemctl status mysqld(ormariadbif you use MariaDB). If it showsinactive (dead)orfailed, proceed. If it’s running but you can’t connect, skip to the troubleshooting section. - Try to start it manually and capture the error. Run
sudo systemctl start mysqld. If it fails, immediately runsudo journalctl -u mysqld --no-pager | tail -50to see the last log lines. This output is your primary clue. - Check the MySQL error log. The log is usually at
/var/log/mysqld.log(or/var/log/mysql/mariadb.logfor MariaDB). Runsudo tail -100 /var/log/mysqld.log. Look for lines containingERROR,Can't, orFailed. - Verify data directory permissions. The MySQL process runs as the
mysqluser. Ensure the data directory (usually/var/lib/mysql) is owned by that user:sudo chown -R mysql:mysql /var/lib/mysql. Also check the directory permissions:sudo ls -ld /var/lib/mysql— it should bedrwxr-xr-xor similar. - Check disk space. A full disk prevents MySQL from writing. Run
df -handdf -i(for inodes). If any filesystem is 100% full, free up space or move the data directory. - Look for corrupted tables or InnoDB issues. If the log mentions
corruptorinnodb_force_recovery, you may need to start MySQL in recovery mode. Addinnodb_force_recovery=1to/etc/my.cnfunder[mysqld], then try starting. If it starts, back up your data immediately, then remove the line. - Check for SELinux blocking. SELinux can prevent MySQL from accessing its files. Run
sudo getenforce. If it returnsEnforcing, check the audit log:sudo grep mysql /var/log/audit/audit.log | tail -20. If you see denials, you can either set the correct context or temporarily disable SELinux withsudo setenforce 0to test. If that fixes it, you’ll need to adjust the SELinux policy properly. - Verify the configuration file. A typo in
/etc/my.cnfcan cause startup failure. Look for unknown variables or wrong paths. You can test the configuration withsudo mysqld --validate-config(may not be available on older versions). - Check for port conflicts. If another process is using port 3306, MySQL can’t bind. Run
sudo netstat -tulpn | grep 3306. If something else is listening, either stop that service or change MySQL’s port in the config. - Check for insufficient memory. If the log shows
Out of memoryorCannot allocate memory, your system may not have enough RAM. Check withfree -m. If it’s low, consider reducing MySQL’s buffer pool size inmy.cnfor adding swap. - Try starting MySQL directly as the mysql user. Run
sudo -u mysql mysqld --user=mysqlto see the exact error output. This often reveals issues that systemd hides. - Re-initialize the data directory (last resort). If the data directory is beyond repair, you may need to re-initialize it. This erases all databases, so only do this if you have backups. Remove the old directory, create a new one, and run
mysqld --initialize(ormysql_install_dbfor older versions).
Common Mistakes
- Ignoring the error log. Many people guess instead of reading the log. The log almost always points to the exact problem.
- Changing permissions on the entire /var/lib/mysql without checking ownership. Sometimes only a single file is wrong, and chown -R fixes it, but it can also cause issues if you accidentally change the ownership of system files.
- Disabling SELinux permanently without understanding the risk. Setting
setenforce 0is a quick test, but you should fix the policy, not just disable it. - Forgetting to restart after editing my.cnf. Configuration changes require a restart to take effect.
Tips
- Always back up your data directory before making major changes. You can use
cp -a /var/lib/mysql /var/lib/mysql.bak. - Enable MySQL’s error logging to a file. In
my.cnf, setlog_error=/var/log/mysqld.logto ensure logs are persistent. - Use
systemctl statuswith-lto see full output.systemctl status mysqld -lshows more lines than the default. - If you recently changed the system’s hostname or IP, check the
bind-addressin my.cnf. Sometimes MySQL is configured to listen on a specific interface that no longer exists.
Troubleshooting
If the steps above don’t resolve the issue, try these additional checks:
- Run
sudo mysqld --verbose --helpto see the effective settings and any errors in the config. - Check for missing system tables. If the log mentions
mysql.userormysql.dbmissing, you may need to runmysql_upgradeafter starting in recovery mode. - Look at the systemd unit file. Sometimes the unit file has incorrect paths. Run
systemctl cat mysqldto see it. - Verify that the MySQL socket file directory exists. Usually
/var/run/mysqld. Create it withsudo mkdir -p /var/run/mysqldand set ownership to mysql. - If you’re using AppArmor (less common on CentOS), check its profiles for MySQL.
- If all else fails, uninstall and reinstall MySQL. This is drastic, but sometimes the package is corrupted. Backup your data directory first, then
sudo yum remove mysql-serverand reinstall.
Frequently Asked Questions
- Why does MySQL start but then immediately stop? This usually indicates a crash during startup. Check the error log for a stack trace or an assertion failure. Often it’s due to corrupted InnoDB files.
- What does “Can’t connect to local MySQL server through socket” mean? This means the client can’t find the socket file. Either MySQL isn’t running, or the socket path in the client config doesn’t match the server’s. Check
/var/lib/mysql/mysql.sockandmy.cnf. - How do I start MySQL in safe mode? Use
mysqld_safeinstead ofmysqld. It starts the server with extra logging and can help diagnose issues. Runsudo -u mysql mysqld_safeand watch the log. - Can I recover data if the data directory is corrupted? Possibly. Try starting with
innodb_force_recovery=1to6(increasing the number). This skips certain checks and may allow you to dump your data. Be aware that higher levels can cause data loss. - Why does MySQL fail to start after a system update? Updates can change permissions, SELinux contexts, or the MySQL version. Check the error log for version mismatch messages. You may need to run
mysql_upgrade. - Is it safe to delete the MySQL error log? Yes, you can delete
/var/log/mysqld.logand restart MySQL; it will recreate the file. This can help if the log has grown huge or is corrupted.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Delete Picture Kik? How to Remove Photos
- How to Stop WordPress Comment Spam: A Layered Guide
- What Happens to X Replies, DMs, and Followers After Deletion?
- Data Breaches That Shook the World
- Disable Google Assistant: Turn It Off on Android & Home
- Resin vs FDM Printers for Miniatures: Detail, Durability, Cost