[Fixed]: Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38) on Mac OS X

I'm running Mac OS X and have MySQL Server and Client installed through Homebrew. After installation, I tried to connect to MySQL serve using the following command:

$ mysql -u root -p

After entering password for root user and hit Enter, the Terminal showed this error:

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

Research on the internet shows me that I should try this command:

sudo chown -R _mysql:mysql /usr/local/var/mysql

Then restart MySQL:

$ brew services restart mysql

But it's still not working.

I decided to give it another try by removing and reinstalling MySQL using Homebrew then restart MySQL but still receive the error.

What I can do to fix this error?

Asked 2 years ago ago

You should stop any MySQL running instance on your computer.

Go to Preference Pane and search for MySQL and then click Stop MySQL Server.

Then start MySQL and connect again.

Answered 2 years ago ago

Write Answer: