When This Error Hits
You're running a local web app—maybe WordPress, a PHP tool, or your own script—and suddenly you see:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
I've seen this on fresh MySQL installs on Windows 10, after a power outage on a Linux server, and when someone accidentally killed the mysqld process. The worst case was a client's accounting software that hadn't been backed up for three days. The error is clear: MySQL isn't answering on port 3306.
What's Actually Going Wrong
MySQL needs to be running as a service or daemon. If it's not started, or the socket file is missing (on Unix), or the port is blocked by a firewall, you get this error. On Windows, it's usually the service being set to manual or stopped. On Linux, it's often a corrupt socket or a failed start due to a misconfiguration in my.cnf. The core fix is simple: get MySQL running.
Step-by-Step Fix
Step 1: Check if MySQL is running
Windows: Open Services (services.msc). Look for MySQL or MySQL80 (version number varies). If it's not running, right-click and Start. If it's set to Manual or Disabled, change it to Automatic and start it.
Linux (Ubuntu/Debian): Run:
sudo systemctl status mysql
If it shows inactive (dead), start it:
sudo systemctl start mysql
macOS (Homebrew):
brew services list | grep mysql
If it's not running, start it:
brew services start mysql
Step 2: Verify MySQL is listening on port 3306
Windows: Run this in Command Prompt (as admin):
netstat -ano | findstr :3306
Nothing shows? MySQL isn't listening. Restart the service.
Linux/macOS:
sudo lsof -i :3306
If empty, start MySQL. If you see mysqld in the output, it's running—skip to Step 4.
Step 3: Check the error log
MySQL writes startup failures to its error log. On Windows it's in C:\ProgramData\MySQL\MySQL Server X.Y\Data\*.err. On Linux it's /var/log/mysql/error.log. Open it and look for lines like:
[ERROR] InnoDB: Unable to lock ./ibdata1, error: 11
That means another process (like a previous MySQL instance) still has the file locked. Kill it with kill -9 (Linux) or Task Manager (Windows).
Step 4: Firewall block?
If you're connecting from a different machine (not localhost), port 3306 might be blocked. On Windows, add an inbound rule for TCP port 3306. On Linux, use ufw:
sudo ufw allow 3306/tcp
But for localhost, this is rarely the issue. Skip unless you're remote.
Step 5: Reinstall if it's still dead
Had a client whose MySQL service kept crashing on Windows after an update. The registry entries were shot. Uninstall MySQL completely (using the installer), delete C:\ProgramData\MySQL and C:\Program Files\MySQL, then reinstall. Backup your databases first with mysqldump—or copy the Data folder.
Still Failing? Check These
- Hostname mismatch: If you use
-h 127.0.0.1instead oflocalhost, MySQL might use TCP instead of the socket. Sometimes the socket file is missing. Trymysql -u root -p -h 127.0.0.1to force TCP. - my.cnf corruption: A bad bind-address or skip-networking can kill remote connections. Comment out
skip-networkingif you see it. - Disk full: MySQL can't start if the drive is full. Check with
df -h(Linux) or check Disk Cleanup on Windows. - Port conflict: Run
nmap localhostto see if something else grabbed port 3306. Change MySQL's port in my.cnf if needed.
One last tip: on Windows, try running the MySQL installer again and select Reconfigure. It'll reset the service account and often fixes weird permission issues. Saved my butt twice last year.