keepcalculator

06 Numbers and Powers

Fix MySQL Server Won’t Start on CentOS

Card No. 06·34773 · Updated · Standard formula, shown below

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

  1. Check if MySQL is actually running. Run systemctl status mysqld (or mariadb if you use MariaDB). If it shows inactive (dead) or failed, proceed. If it’s running but you can’t connect, skip to the troubleshooting section.
  2. Try to start it manually and capture the error. Run sudo systemctl start mysqld. If it fails, immediately run sudo journalctl -u mysqld --no-pager | tail -50 to see the last log lines. This output is your primary clue.
  3. Check the MySQL error log. The log is usually at /var/log/mysqld.log (or /var/log/mysql/mariadb.log for MariaDB). Run sudo tail -100 /var/log/mysqld.log. Look for lines containing ERROR, Can't, or Failed.
  4. Verify data directory permissions. The MySQL process runs as the mysql user. 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 be drwxr-xr-x or similar.
  5. Check disk space. A full disk prevents MySQL from writing. Run df -h and df -i (for inodes). If any filesystem is 100% full, free up space or move the data directory.
  6. Look for corrupted tables or InnoDB issues. If the log mentions corrupt or innodb_force_recovery, you may need to start MySQL in recovery mode. Add innodb_force_recovery=1 to /etc/my.cnf under [mysqld], then try starting. If it starts, back up your data immediately, then remove the line.
  7. Check for SELinux blocking. SELinux can prevent MySQL from accessing its files. Run sudo getenforce. If it returns Enforcing, 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 with sudo setenforce 0 to test. If that fixes it, you’ll need to adjust the SELinux policy properly.
  8. Verify the configuration file. A typo in /etc/my.cnf can cause startup failure. Look for unknown variables or wrong paths. You can test the configuration with sudo mysqld --validate-config (may not be available on older versions).
  9. 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.
  10. Check for insufficient memory. If the log shows Out of memory or Cannot allocate memory, your system may not have enough RAM. Check with free -m. If it’s low, consider reducing MySQL’s buffer pool size in my.cnf or adding swap.
  11. Try starting MySQL directly as the mysql user. Run sudo -u mysql mysqld --user=mysql to see the exact error output. This often reveals issues that systemd hides.
  12. 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 (or mysql_install_db for 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 0 is 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, set log_error=/var/log/mysqld.log to ensure logs are persistent.
  • Use systemctl status with -l to see full output. systemctl status mysqld -l shows more lines than the default.
  • If you recently changed the system’s hostname or IP, check the bind-address in 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 --help to see the effective settings and any errors in the config.
  • Check for missing system tables. If the log mentions mysql.user or mysql.db missing, you may need to run mysql_upgrade after starting in recovery mode.
  • Look at the systemd unit file. Sometimes the unit file has incorrect paths. Run systemctl cat mysqld to see it.
  • Verify that the MySQL socket file directory exists. Usually /var/run/mysqld. Create it with sudo mkdir -p /var/run/mysqld and 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-server and reinstall.

Frequently Asked Questions

  1. 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.
  2. 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.sock and my.cnf.
  3. How do I start MySQL in safe mode? Use mysqld_safe instead of mysqld. It starts the server with extra logging and can help diagnose issues. Run sudo -u mysql mysqld_safe and watch the log.
  4. Can I recover data if the data directory is corrupted? Possibly. Try starting with innodb_force_recovery=1 to 6 (increasing the number). This skips certain checks and may allow you to dump your data. Be aware that higher levels can cause data loss.
  5. 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.
  6. Is it safe to delete the MySQL error log? Yes, you can delete /var/log/mysqld.log and 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

The whole Numbers and Powers drawer →