Categories
Tech Notes

Fixing “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock'” on macOS

If you’re using Local WP, Homebrew, or other tools for local development on macOS, you may have encountered MySQL issues after a macOS update. Recently, after upgrading to macOS Sequoia, I ran into errors when trying to start or connect to MySQL. Here’s how I resolved them.

Errors Encountered

When I tried to connect to MySQL using mysql -u root, I received this error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

And when trying to start MySQL directly:

ERROR! The server quit without updating PID file (/usr/local/var/mysql/myHostName.pid).

Troubleshooting Attempts

I attempted a few common fixes:

  • Uninstalling and reinstalling MySQL via Homebrew.
  • Following suggestions from Stack Overflow and other forums.

Unfortunately, none of these worked.

Solution: Understanding the Upgrade Caveats

Running mysqld stop in the terminal, I noticed a key error message in the logs:

2024-10-27T03:31:42.571563Z 1 [ERROR] [MY-014060] [Server] Invalid MySQL server upgrade: Cannot upgrade from 80300 to 90001. Upgrade to next major version is only allowed from the last LTS release, which version 80300 is not.

This log reminded me of an important caveat I’d seen during the initial MySQL installation with Homebrew. Here’s what I ignored:

==> Caveats
Upgrading from MySQL <8.4 to MySQL >9.0 requires running MySQL 8.4 first:
  - brew services stop mysql
  - brew install [email protected]
  - brew services start [email protected]
  - brew services stop [email protected]
  - brew services start mysql

I mistakenly assumed this didn’t apply since mysql -V confirmed MySQL 9.0 was installed. However, the version mismatch did matter!

Fixing the Issue

Following these steps resolved the problem:

  1. Stop the MySQL service:
   brew services stop mysql
  1. Install MySQL 8.4:
   brew install [email protected]
  1. Start and stop MySQL 8.4 to allow it to complete the upgrade process:
   brew services start [email protected]
   brew services stop [email protected]
  1. Finally, start MySQL again:
   brew services start mysql

After running these commands, everything worked as expected!

Final Check

Running mysql -u root returned the following:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 9.0.1 Homebrew

Problem solved!


Hope this helps others struggling with the same MySQL issue on macOS.