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

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?

This question is related to mysql macos

The answer is


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

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!


just hit "brew services start mysql" in terminal


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


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.


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.


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.


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 [email protected]
  4. mysql -u root

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


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 [email protected]

Issue was resolved.

mysql -uroot -proot

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.


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

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

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


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


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

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!


  1. Uninstall MySQL.
  2. Remove /usr/local/var/mysql/.
  3. Install MySQL.

For me it was simple as running:

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

instead of mysqld


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

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.


I managed to solve this issue by changing localhost to 127.0.0.1


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

_x000D_
_x000D_
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
_x000D_
_x000D_
_x000D_ enter image description here Completed All process of secure installation then run

$mysql -u root -p

Congratulations you’ve just set up mysql!


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!


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.


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!


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

Try this

rm -rf /usr/local/var/mysql && brew postinstall [email protected] && brew services restart [email protected]

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.


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

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.


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

brew services start mysql

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


Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"