Initial Problem Indicators
If you see this error when trying to connect to MySQL:
bashCopy
1
2 mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Step 1: Check MySQL Installation
Check if MySQL is installed via Homebrew:
bashCopy
1 brew list | grep mysql
Step 2: Clean Up Existing Services
Stop all running MySQL services:
1 brew services stop mysql<br>brew services stop mysql@8.0
sudo pkill mysqld
Step 3: Fix Permissions
Reset ownership and permissions of MySQL directory:
1 sudo chown -R $(whoami) /opt/homebrew/var/mysql<br>sudo chmod -R 755 /opt/homebrew/var/mysql
sudo chown -R $(whoami):admin /opt/homebrew/var/mysql
sudo chown -R $(whoami):admin /opt/homebrew/Cellar/mysql@8.0
Step 4: Remove Launch Agent Files
Clean up old launch agent files:
1 rm ~/Library/LaunchAgents/homebrew.mxcl.mysql@8.0.plist<br>sudo rm /Library/LaunchDaemons/homebrew.mxcl.mysql@8.0.plist
Step 5: Reset MySQL Links
Unlink and relink MySQL:
1 brew unlink mysql<br>brew unlink mysql@8.0<br>brew link mysql@8.0 --force
# The last step will gave an export to add, add that as well => echo 'export PATH="/opt/homebrew/opt/mysql@8.0/bin:$PATH"' >> ~/.zshrc
Step 6: Start MySQL Service
Start MySQL as a regular user (not root):
1 brew services start mysql@8.0
Verification
After completing these steps, verify the connection:
1 mysql -u root -p
Note: Keep track of your MySQL root password. If you’ve just installed MySQL, the initial root password might be empty.