To fix MySQL on a Linux server, start by checking the service status and error logs, then resolve common failures like disk space, corrupted tables, permission issues, or port conflicts. Restart the service after each change.
Use systemctl, journalctl, mysqlcheck, and configuration tweaks to restore availability and performance safely.
If you’re wondering how to fix MySQL on Linux server environments (Ubuntu, Debian, CentOS, Rocky, AlmaLinux), this guide walks you through a proven, step-by-step troubleshooting workflow.
As a Senior Technical SEO Content Writer at YouStable, I’ll share practical sysadmin methods that work in real production hosting—beginner-friendly, technically accurate, and optimized to rank.
Before You Start: Key Facts and Safety
- Always take a backup before major changes: config edits, upgrades, repair operations.
- Know your engine: MySQL vs MariaDB. Commands are similar, service names differ.
- Have root or sudo access. Use a staging server when possible.
Quick Diagnosis: Identify the Exact Problem
First, confirm whether MySQL is installed and which flavor you run (Oracle MySQL or MariaDB). Then get immediate status, logs, and basic resource checks.
# Identify service name (varies by distro)
systemctl status mysql # Debian/Ubuntu (MySQL)
systemctl status mariadb # RHEL/CentOS/Rocky/Alma (MariaDB)
systemctl status mysqld # Some installations
# View recent logs
journalctl -u mysql -n 200 --no-pager
journalctl -u mariadb -n 200 --no-pager
# Classic log files
sudo tail -n 200 /var/log/mysql/error.log
sudo tail -n 200 /var/log/mysqld.log
# Check resources and port usage
df -h # Disk space
free -m # Memory
ss -ltnp | grep 3306 # Is port 3306 taken?
lsof -i :3306 # Which process uses 3306?
Step-by-Step Fixes for Common MySQL Issues
Step 1: Service Won’t Start: Status, Logs, and Fast Fixes
Most “MySQL won’t start” issues trace back to disk, permissions, corrupted tables, or port conflicts. Work through these in order.
# Try a clean restart and check status
sudo systemctl daemon-reload
sudo systemctl restart mysql || sudo systemctl restart mariadb
sudo systemctl status mysql -l --no-pager
- Disk Full: If df shows 100% usage, free space by removing old logs, rotating logs, or moving archives.
- PID/Socket Leftovers: Remove stale files, then restart.
# Example cleanup (paths vary by distro)
sudo rm -f /var/run/mysqld/mysqld.pid /var/run/mysqld/mysqld.sock
sudo mkdir -p /var/run/mysqld && sudo chown mysql:mysql /var/run/mysqld
- Permission Issues: Ensure MySQL owns its data directory.
sudo chown -R mysql:mysql /var/lib/mysql
sudo find /var/lib/mysql -type d -exec chmod 750 {} \;
sudo find /var/lib/mysql -type f -exec chmod 640 {} \;
- Port Conflict: If another process uses 3306, stop it or change MySQL’s port in my.cnf, then restart.
# Example config paths
sudo nano /etc/mysql/my.cnf # Debian/Ubuntu
sudo nano /etc/my.cnf # RHEL/CentOS/Rocky/Alma
# In [mysqld] section:
port=3307
Step 2: Fix InnoDB Corruption and Crash Loops
InnoDB is resilient, but abrupt power loss or full disks can corrupt tablespaces. The error log often mentions “InnoDB: corruption” or “page checksum mismatch.”
- Try a safe start: remove redo logs (InnoDB will rebuild them).
sudo systemctl stop mysql
sudo mv /var/lib/mysql/ib_logfile0 /var/lib/mysql/ib_logfile0.bak 2>/dev/null
sudo mv /var/lib/mysql/ib_logfile1 /var/lib/mysql/ib_logfile1.bak 2>/dev/null
sudo systemctl start mysql
If MySQL still fails, use InnoDB force recovery to bring it up long enough to dump data. Use the lowest level that works and remove it afterward.
# In my.cnf under [mysqld]
innodb_force_recovery=1 # Try 1..6 cautiously
# Then:
sudo systemctl restart mysql
# Dump critical databases
mysqldump --single-transaction --hex-blob --routines --triggers -u root -p mydb > mydb.sql
# Remove force_recovery and restore from backup or reimport dumps
# Finally:
sudo systemctl restart mysql
Step 3: Repair MyISAM Tables (Older or Niche Use)
MyISAM tables can be repaired using mysqlcheck or myisamchk. Stop the service before filesystem-level repairs.
# Online repair attempt (server running)
mysqlcheck -u root -p --repair --optimize --all-databases
# Offline repair (server stopped)
sudo systemctl stop mysql
sudo myisamchk -r /var/lib/mysql/DB_NAME/*.MYI
sudo systemctl start mysql
Step 4: Reset a Lost MySQL Root Password
Use skip-grant-tables to bypass authentication temporarily. Restrict server access while doing this.
sudo systemctl stop mysql
# Start manually without grants (one-time)
sudo mysqld_safe --skip-grant-tables --skip-networking &
# In another shell, set a new password
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword!';
-- or (older versions)
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewStrongPassword!');
# Kill safe server and start normally
sudo pkill -f mysqld_safe
sudo systemctl start mysql
Step 5: Authentication, Bind, Firewall, and SELinux/AppArmor
- Bind Address: For local-only access, keep 127.0.0.1. For remote access, use 0.0.0.0 and secure with users, SSL, and firewall.
# my.cnf
[mysqld]
bind-address=0.0.0.0
# Firewall example (UFW)
sudo ufw allow 3306/tcp
- SELinux (RHEL-based):
# Allow MySQL to listen remotely
sudo setsebool -P mysql_connect_any 1
# If using a custom data dir
sudo semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
sudo restorecon -Rv /data/mysql
- AppArmor (Debian/Ubuntu): Adjust profiles if using a non-default data directory.
sudo nano /etc/apparmor.d/usr.sbin.mysqld
# Add new data dir paths, then:
sudo systemctl reload apparmor
Step 6: Fix Performance: Slow Queries and Misconfiguration
- Enable and inspect slow query log:
# my.cnf
[mysqld]
slow_query_log=1
long_query_time=1
slow_query_log_file=/var/log/mysql/slow.log
sudo systemctl restart mysql
pt-query-digest /var/log/mysql/slow.log # Percona tool (optional)
- Tune key buffers (examples, adjust to RAM):
[mysqld]
innodb_buffer_pool_size=1G
innodb_log_file_size=256M
innodb_flush_log_at_trx_commit=1
max_connections=200
query_cache_type=0 # Disable on modern versions
- Flush and optimize:
mysqlcheck -u root -p --optimize --all-databases
mysql -e "SHOW PROCESSLIST;" -uroot -p
mysql -e "SHOW ENGINE INNODB STATUS\G" -uroot -p
Hardware limits matter. If memory is tight, reduce innodb_buffer_pool_size or add swap as a temporary measure, then scale the server.
Step 7: Upgrade/Downgrade Pitfalls and Version Mismatch
- After upgrading major versions, run mysql_upgrade (or the built-in upgrade routine on newer releases).
- Check plugin and authentication compatibility (e.g., caching_sha2_password vs mysql_native_password).
mysql_upgrade -u root -p
# Newer versions may auto-run equivalent tasks at startup
Step 8: Backups and Restores (Logical vs Physical)
- Logical (mysqldump): Portable, slower on large datasets.
- Physical (Percona XtraBackup/LVM): Fast, great for large datasets and PITR.
# Full logical backup
mysqldump --single-transaction --routines --triggers --events --all-databases -u root -p | gzip > full-$(date +%F).sql.gz
# Restore
gunzip -c full-2025-01-01.sql.gz | mysql -u root -p
Common Fix Scenarios and What to Do
- Disk is 100% full: Purge logs, move binary logs, increase volume size, then restart MySQL.
- Can’t connect remotely: Update bind-address, create proper users with host patterns, open firewall, verify cloud security groups.
- High CPU or RAM: Investigate slow queries, missing indexes, high concurrency; tune buffer pool and connection limits.
- Tables marked crashed: Use mysqlcheck or myisamchk; consider migrating to InnoDB for durability.
- Random restarts: Check kernel logs (dmesg), OOM kills, hardware, and power. Enable systemd restart policies.
Security, Stability, and Prevention Best Practices
- Automate backups and test restores monthly.
- Enable monitoring for disk, memory, CPU, I/O, and MySQL metrics.
- Keep MySQL packages updated after testing in staging.
- Use least-privilege database users and SSL for remote access.
- Document your my.cnf, keep it in version control, and annotate changes.
Essential Commands Cheat Sheet
# Service control
sudo systemctl status mysql
sudo systemctl restart mysql
sudo systemctl status mariadb
sudo systemctl restart mariadb
# Logs
journalctl -u mysql -f
tail -f /var/log/mysql/error.log
# Health and repair
mysqladmin ping -uroot -p
mysqlcheck -uroot -p --all-databases
mysql -e "SHOW VARIABLES LIKE 'version';" -uroot -p
# Connections and ports
ss -ltnp | grep 3306
ufw allow 3306/tcp # or firewall-cmd --add-service=mysql --permanent && firewall-cmd --reload
When to Ask for Help (And How YouStable Can Assist)
If your data is valuable and logs hint at deep InnoDB damage, or production downtime costs are mounting, engage a professional. YouStable’s managed hosting team routinely handles emergency MySQL recovery, performance tuning, and high availability setups—without guesswork—so your stack returns to a healthy, secure state fast.
FAQ’s – Fixing MySQL on Linux Servers
How do I fix “MySQL service failed to start” on Ubuntu or Debian?
Run systemctl status mysql and check journalctl -u mysql for the exact error. Common fixes include freeing disk space, correcting ownership of /var/lib/mysql, removing stale PID/socket files, and resolving port conflicts. After changes, restart MySQL and recheck logs.
What should I do if MySQL port 3306 is already in use?
Identify the process with ss -ltnp or lsof -i :3306. Stop the conflicting service or change MySQL’s port in my.cnf (e.g., 3307), then update firewalls and application configs accordingly. Restart MySQL and verify connectivity.
How can I recover from InnoDB corruption without data loss?
Try restarting after clearing ib_logfile files. If that fails, enable innodb_force_recovery at the lowest value that allows startup, dump your databases with mysqldump, remove the force setting, rebuild the instance, and reimport. Always maintain verified backups for safety.
Why can’t I connect to MySQL remotely from another server?
Check bind-address (0.0.0.0 for remote), ensure the user is created with the correct host (e.g., ‘app’@’10.%’), allow port 3306 on the firewall and cloud security groups, and confirm no SELinux/AppArmor restrictions. Test with mysql -h SERVER_IP -u USER -p.
Is MariaDB different from MySQL when troubleshooting?
Core steps are nearly identical. The main differences are package and service names (mariadb vs mysql/mysqld), version-specific features, and configuration defaults. Log analysis, systemctl, journalctl, and mysqlcheck apply to both.