MySQL
Homebrew
Connection Issues
Troubleshooting
Database Configuration

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

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

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:

bash
1# Start MySQL via Homebrew services
2brew services start mysql
3
4# Or start manually (foreground)
5mysql.server start
6
7# Check if it's running
8brew services list | grep mysql
9# mysql started markqian ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
10
11# Or check the process
12ps aux | grep mysqld

Fix 2: Restart MySQL

If MySQL is stuck or crashed:

bash
1# Restart
2brew services restart mysql
3
4# Or force stop and start
5brew services stop mysql
6brew services start mysql
7
8# Nuclear option — kill the process
9killall mysqld
10brew services start mysql

Fix 3: Socket Path Mismatch

Homebrew MySQL may create the socket in a different location than the client expects:

bash
1# Find where MySQL creates the socket
2mysql_config --socket
3# /opt/homebrew/var/mysql/mysql.sock  (Apple Silicon)
4# /usr/local/var/mysql/mysql.sock     (Intel Mac)
5
6# But the client looks in /tmp/mysql.sock by default
bash
# Link the actual socket to where the client expects it
sudo ln -s /opt/homebrew/var/mysql/mysql.sock /tmp/mysql.sock

Option B: Specify the Socket Path

bash
1# Connect with explicit socket path
2mysql -u root --socket=/opt/homebrew/var/mysql/mysql.sock
3
4# Or use TCP instead of socket
5mysql -u root --protocol=tcp -h 127.0.0.1

Option C: Configure my.cnf

Create or edit the MySQL config file:

ini
1# /opt/homebrew/etc/my.cnf (Apple Silicon)
2# /usr/local/etc/my.cnf (Intel Mac)
3
4[mysqld]
5socket=/tmp/mysql.sock
6
7[client]
8socket=/tmp/mysql.sock

Then restart MySQL:

bash
brew services restart mysql

Fix 4: Permission Issues

The MySQL data directory or socket file may have wrong permissions:

bash
1# Fix data directory ownership
2sudo chown -R $(whoami) /opt/homebrew/var/mysql
3
4# Fix /tmp permissions if socket creation fails
5ls -la /tmp/mysql.sock
6# If owned by a different user:
7sudo rm /tmp/mysql.sock
8brew services restart mysql

Fix 5: Corrupted Data Directory

If MySQL fails to start due to a corrupted data directory:

bash
1# Check MySQL error log
2cat /opt/homebrew/var/mysql/$(hostname).err | tail -50
3
4# If the log shows InnoDB corruption, reinitialize
5brew services stop mysql
6rm -rf /opt/homebrew/var/mysql   # WARNING: deletes all databases
7mysqld --initialize-insecure
8brew services start mysql

Fix 6: After macOS Upgrade

macOS upgrades can break Homebrew MySQL:

bash
1# Reinstall MySQL
2brew reinstall mysql
3
4# Or upgrade
5brew upgrade mysql
6
7# Re-initialize if needed
8mysql_install_db
9brew services start mysql

Fix 7: Port Conflict

If another MySQL instance is running on the same port:

bash
1# Check what's using port 3306
2lsof -i :3306
3
4# Stop any conflicting instance
5sudo launchctl unload /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist  # Official MySQL
6brew services stop [email protected]  # Older Homebrew formula

Connecting After Fix

bash
1# Basic connection
2mysql -u root
3
4# If root has a password
5mysql -u root -p
6
7# TCP connection (bypasses socket entirely)
8mysql -u root -h 127.0.0.1 -P 3306
9
10# Set root password if needed
11mysql -u root
12ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Diagnostic Commands

bash
1# Check MySQL status
2brew services info mysql
3mysqladmin -u root status
4
5# Find the socket file
6find / -name "mysql.sock" 2>/dev/null
7
8# Check MySQL error log
9tail -20 /opt/homebrew/var/mysql/$(hostname).err
10
11# Check MySQL config locations
12mysql --help | grep my.cnf
13# Default options: /etc/my.cnf /etc/mysql/my.cnf /opt/homebrew/etc/my.cnf ~/.my.cnf

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 mysql or uninstall the DMG version.
  • MYSQL_UNIX_PORT environment variable: This overrides the socket path. Check with echo $MYSQL_UNIX_PORT. If set incorrectly, unset it: unset MYSQL_UNIX_PORT.
  • Using localhost vs 127.0.0.1: mysql -h localhost uses the Unix socket. mysql -h 127.0.0.1 uses TCP. If the socket is broken, use 127.0.0.1 as a workaround.
  • Homebrew services vs launchctl: brew services start mysql registers 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).err for startup failures
  • Create a symlink or configure my.cnf to fix the socket path permanently

Course illustration
Course illustration

All Rights Reserved.