Can't connect to local MySQL server through socket homebrew
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error Can't connect to local MySQL server through socket '/tmp/mysql.sock' on macOS with Homebrew means the MySQL server is not running, or the client is looking for the socket file in the wrong location. This is one of the most common MySQL issues on macOS and is usually resolved by starting the server, fixing the socket path, or resetting permissions.
The Error
The (2) means "No such file or directory" — the socket file does not exist because MySQL is not running.
Fix 1: Start MySQL
The most common cause — MySQL is simply not running:
Fix 2: Restart MySQL
If MySQL is stuck or crashed:
Fix 3: Socket Path Mismatch
Homebrew MySQL may create the socket in a different location than the client expects:
Option A: Create a Symlink
Option B: Specify the Socket Path
Option C: Configure my.cnf
Create or edit the MySQL config file:
Then restart MySQL:
Fix 4: Permission Issues
The MySQL data directory or socket file may have wrong permissions:
Fix 5: Corrupted Data Directory
If MySQL fails to start due to a corrupted data directory:
Fix 6: After macOS Upgrade
macOS upgrades can break Homebrew MySQL:
Fix 7: Port Conflict
If another MySQL instance is running on the same port:
Connecting After Fix
Diagnostic Commands
Common Pitfalls
- Apple Silicon vs Intel paths: Homebrew on Apple Silicon (M1/M2/M3) uses
/opt/homebrew/, while Intel Macs use/usr/local/. The socket and config file paths differ accordingly. - Multiple MySQL installations: Having both Homebrew MySQL and the official MySQL DMG installer causes conflicts. Remove one:
brew uninstall mysqlor uninstall the DMG version. MYSQL_UNIX_PORTenvironment variable: This overrides the socket path. Check withecho $MYSQL_UNIX_PORT. If set incorrectly, unset it:unset MYSQL_UNIX_PORT.- Using
localhostvs127.0.0.1:mysql -h localhostuses the Unix socket.mysql -h 127.0.0.1uses TCP. If the socket is broken, use127.0.0.1as a workaround. - Homebrew services vs launchctl:
brew services start mysqlregisters a LaunchAgent. If you also have a system-wide LaunchDaemon from a DMG install, they conflict. Remove the daemon:sudo launchctl unload /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist.
Summary
- Start MySQL first:
brew services start mysql - The socket error means MySQL is not running or the socket path is wrong
- Homebrew socket location:
/opt/homebrew/var/mysql/mysql.sock(Apple Silicon) or/usr/local/var/mysql/mysql.sock(Intel) - Use
mysql -h 127.0.0.1(TCP) as a quick workaround that bypasses the socket - Check the error log at
/opt/homebrew/var/mysql/$(hostname).errfor startup failures - Create a symlink or configure
my.cnfto fix the socket path permanently

