macos – ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

The Question :

217 people think this question is useful

I installed MySQL on Mac OS X Mountain Lion with homebrew install mysql, but when I tried mysql -u root I got the following error:

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

What does this error mean? How can I fix it?

The Question Comments :
  • Didn’t post it as an answer but you have to install mysql-server package aswell, not just mysql
  • @HankyPanky maybe that was true when you wrote it. But in 2016 Brew has no mysql-server to install.
  • It’s important to note that you installed using Homebrew so ignore the non-homebrew tips and start (and restart on startup) with brew services start mysql or (if you don’t want a background service) mysql.server start.
  • I got it to work but with old normal mysql 5.7.19. The new mysql@5.7 version 5.7.24 couldn’t work because the some config reference was kept linked to the old mysql instead of the new brew Cellar mysql@5.7 Keg. So thanks I try this later
  • This error is also likely to happen after running brew upgrade which will install a newer version of mysql than previously installed.

The Answer 1

122 people think this answer is useful

It’s probably because MySQL is installed but not yet running.

To verify that it’s running, open up Activity Monitor and under “All Processes”, search and verify you see the process “mysqld”.

You can start it by installing “MySQL.prefPane”.

Here is the complete tutorial which helped me: http://obscuredclarity.blogspot.in/2009/08/install-mysql-on-mac-os-x.html

The Answer 2

116 people think this answer is useful

You’ll need to start MySQL before you can use the mysql command on your terminal. To do this, run brew services start mysql. By default, brew installs the MySQL database without a root password. To secure it run: mysql_secure_installation.

To connect run: mysql -uroot. root is the username name here.

The Answer 3

109 people think this answer is useful

This happened after the homebrew install and occurs due to permission issues. The following commands fixed the issue.

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

sudo mysql.server start

The Answer 4

46 people think this answer is useful

Run: brew info mysql

And follow the instructions. From the description in the formula:

Set up databases to run AS YOUR USER ACCOUNT with:
    unset TMPDIR
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

To set up base tables in another folder, or use a different user to run
mysqld, view the help for mysql_install_db:
    mysql_install_db --help

and view the MySQL documentation:
  * http://dev.mysql.com/doc/refman/5.5/en/mysql-install-db.html
  * http://dev.mysql.com/doc/refman/5.5/en/default-privileges.html

Hope this helps.

The Answer 5

38 people think this answer is useful

As others have pointed out this is because MySQL is installed but the service isn’t running. There are many ways to start the MySQL service and what worked for me is the below.

To start the service:

  1. Go to “System Preference”
  2. At the bottom pane there should be MySql icon.
  3. Double click that to launch the ‘MySQL Server Status’ and press the button ‘Start MySQL Server’

My env:

Mac Yosemite 10.10.3

Installed Package: /Volumes/mysql-advanced-5.6.24-osx10.8-x86_64

The Answer 6

25 people think this answer is useful

Solutions revolve around:

  • changing MySQL’s permissions

    sudo chown -R _mysql:mysql /usr/local/var/mysql
    
    
  • Starting a MySQL process

    sudo mysql.server start
    
    

Just to add on a lot of great and useful answers that have been provided here and from many different posts, try specifying the host if the above commands did not resolve this issue for you, i.e

mysql -u root -p h127.0.0.1

The Answer 7

16 people think this answer is useful

Warning – this method will remove all of your databases in the /usr/local/var/mysql folder

I had MySQL installed with Homebrew, and the only thing that fixed this for me was re-installing MySQL.

On my company laptop, I didn’t have permission to uninstall MySQL from my computer via Homebrew:

$ brew uninstall mysql --ignore-dependencies
Uninstalling /usr/local/Cellar/mysql/8.0.12... (255 files, 233.0MB)
Error: Permission denied @ dir_s_rmdir - /usr/local/Cellar/mysql/8.0.12

So instead, I removed and reinstalled MySQL manually:

$ sudo rm -rf /usr/local/Cellar/mysql
$ brew cleanup
$ sudo rm -rf /usr/local/var/mysql
$ brew install mysql

And that worked!

The Answer 8

13 people think this answer is useful

Below I’m including the latest instructions from brew install mysql so newer searches for this issue can benefit:

$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.6.26.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.6.26.yosemite.bottle.1.tar.gz

To connect:
    mysql -uroot

To have launchd start mysql at login:
  ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
  mysql.server start

In my case I loaded mysql now via launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist and was then able to launch $ mysql and be on my way.

I hope this helps recent troubleshooters!

The Answer 9

13 people think this answer is useful

Looks like your mysql server is not started. I usually run the stop command and then start it again:

mysqld stop
mysql.server start

Same error, and this works for me.

The Answer 10

10 people think this answer is useful

this problem related to /usr/local/var/mysql folder access, I remove this folder and reinstall mysql.

  1. uninstall mysql with brew :

    brew uninstall mysql

  2. sudo rm -r /usr/local/var/mysql

  3. brew install mysql@8.0
  4. mysql -u root

This solution works fine for me! BUT YOU LOST ALL YOUR DATABASES! WARNING!

The Answer 11

9 people think this answer is useful

This fixed my issue when I restarted the mysql service. Just run:

brew services start mysql

The Answer 12

7 people think this answer is useful

Ough, that took me a while to figure out. I saw it in a comment. After installing mysql using brew, and starting the service (perhaps using sudo brew services start mysql) then run:

$ mysqld

And MySQL should be running for your thereafter.

The Answer 13

6 people think this answer is useful

For me it was simple as running:

/usr/local/opt/mysql/bin/mysqld_safe

instead of mysqld

The Answer 14

6 people think this answer is useful

In my case it was simply a matter of deleting a lock file.

sudo rm -f /tmp/mysql.sock.lock

The Answer 15

3 people think this answer is useful

I found the solution to my problem. It was indeed because my MySQL server was not running.

It was caused by MySQL not being correctly set up on my machine, thus not being able to run.

To remedy this, I used a script which installs MySQL on Mac OSX Mountain Lion, which must have installed missing files.

Here is the link: http://code.macminivault.com/

Important Note: This script sets the root password as a randomly generated string, which it saves on the Desktop, so take care not to delete this file and to note the password. It also installs MySQL manager in your system preferences. I’m also not sure if removes any existing databases, so be careful about that.

The Answer 16

2 people think this answer is useful

After working on this for several hours what worked for me was go to /etc/mysql/ and edit the my.cnf file. Add the following

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

The Answer 17

2 people think this answer is useful

I went through this issue and I managed to run mysql server using below solution

Install mysql through .dmg(https://dev.mysql.com/downloads/mysql/5.7.html), you will get mysql service panel in system preferences then start mysql from the panel and try

mysql -u root -p

Images attached for reference

System Preferences

Mysql panel

The Answer 18

1 people think this answer is useful

I have tried every possible way to solve this issue, like ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock, uninstall and reinstall mysql, make sure mysql is running on xampp but none of it still worked.

Finally, I open up my.cnf (config file) and copy the socket path (make sure to copy the full path else it won’t work). Then I perform this command in my terminal

mysql --socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

Lo, and behold, mysql launchs.

This solution will only work if your MySQL is shown running on Xampp/Ampps, but in terminal it is still not connecting to the right socket when you have already attempted something like:

./mysql -u root

or

brew services start mysql

I hope this helps!

The Answer 19

1 people think this answer is useful

only this has made the trick for me brew services start --all (after trying all answers)

The Answer 20

1 people think this answer is useful

I would recommend you to run

  mysql.server start

before going to

  mysql -u root -p

so as to make sure that the mysql server is running before trying to login into it

This happens many times what you start/restart a machine where by no mysql server is running.

The Answer 21

1 people think this answer is useful

I keep coming back to this post, I’ve encountered this error several times. It might have to do with importing all my databases after doing a fresh install.

I’m using homebrew. The only thing that used to fix it for me:

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

This morning, the issue returned after my machine decided to shut down overnight. The only thing that fixed it now was to upgrade mysql.

brew upgrade mysql

The Answer 22

0 people think this answer is useful

In my case right after installing the MAMP accessing mysql from the terminal was giving the same socket error. At the end all it wanted was a restart and it’s working.

The Answer 23

0 people think this answer is useful

A lot of other helpful responses here, but nothing fixed this for me. Ultimately nothing I found on this site or others would get any version of MySQL from Homebrew working for me.

It was super painless to just download the DMG from https://dev.mysql.com/downloads/file/?id=479114 (find the appropriate version you need) and let the wizard install it for me. The only other manual step was adding /usr/local/mysql/bin to my PATH.

I’d recommend this option if brew is giving you trouble.

Update – if this still doesn’t fix it, try completely purging mysql before installing via DMG. Follow these instructions: https://gist.github.com/vitorbritto/0555879fe4414d18569d

The Answer 24

0 people think this answer is useful

Try this

rm -rf /usr/local/var/mysql && brew postinstall mysql@5.7 && brew services restart mysql@5.7

The Answer 25

0 people think this answer is useful

Encountered a similar problem after upgrading to Catalina OS. After running mysqld command, I found that there was some issue with the logs file. It could be different for others.

$ mysqld

Issue was

2019-10-16T04:58:59.174474Z 0 [ERROR] Could not open file '/var/log/mysql/error.log' for error logging: No such file or directory
2019-10-16T04:58:59.174508Z 0 [ERROR] Aborting

Resolved it by creating it and applying proper permissions.

sudo mkdir -p /var/log/mysql
sudo touch /var/log/mysql/error.log
sudo chown -R _mysql:mysql /var/log/mysql/

Restart MySQL

brew services restart mysql@5.7

Issue was resolved.

mysql -uroot -proot

The Answer 26

0 people think this answer is useful

After trying many solutions, seems like the one that finally did the trick was to connect by IP. No longer file sockets getting deleted randomly.

Just update your MySQL client config (e.g. /usr/local/etc/my.cnf) with:

[client]
port = 3306
host=127.0.0.1
protocol=tcp

The Answer 27

0 people think this answer is useful

Running sudo chown _mysql /usr/local/var/mysql/* helped me finally after trying multitude of options from all these SO answers. The issue with permissions probably arose from improper shutdown of the machine.

The Answer 28

0 people think this answer is useful

just hit “brew services start mysql” in terminal

The Answer 29

0 people think this answer is useful

If you are on OSX and have XAMPP,then please follow these steps:

Thanks to Jackstine’s answer, I was able to do the following:

$ cat ~/.my.cnf

[mysql]

# CLIENT #
port                           = 3306
socket                         = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

I hope this helps me again when I forget, in the future!

The Answer 30

0 people think this answer is useful

The designated data directory /usr/local/var/mysql/ is unusable. You can remove all files that the server added to it.

MacOS:
$brew services stop mysql
$brew services list
$brew uninstall  mysql

$brew install mysql
$brew postinstall mysql

If Any error found then run those cmd
$sudo rm -rf /usr/local/var/mysql
$sudo rm /usr/local/etc/my.cnf
$brew postinstall mysql


$brew services start mysql
$mysql_secure_installation
enter image description here Completed All process of secure installation then run
$mysql -u root -p

Congratulations you’ve just set up mysql!

Tags:,

Add a Comment