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

MysqlMacos

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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.

Solution 2 - Mysql

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

Solution 3 - Mysql

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

Solution 4 - Mysql

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.

Solution 5 - 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

Solution 6 - Mysql

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

Solution 7 - 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!

Solution 8 - Mysql

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.

Solution 9 - Mysql

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!

Solution 10 - Mysql

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!

Solution 11 - Mysql

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!

Solution 12 - Mysql

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

brew services start mysql

Solution 13 - Mysql

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.

Solution 14 - Mysql

For me it was simple as running:

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

instead of mysqld

Solution 15 - Mysql

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

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

Solution 16 - Mysql

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.

Solution 17 - Mysql

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

Solution 18 - Mysql

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

Solution 19 - Mysql

just hit "brew services start mysql" in terminal

Solution 20 - Mysql

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!

Solution 21 - Mysql

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

Solution 22 - Mysql

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.

Solution 23 - Mysql

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

Solution 24 - Mysql

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

Solution 25 - Mysql

I managed to solve this issue by changing localhost to 127.0.0.1

Solution 26 - Mysql

AFTER USING ALL THE ABOVE SOLUTIONS, NOTHING WORKED FOR ME BUT THIS WORKED.

I you have installed MySQL using HomeBrew then check System Preferences > MySQL in your mac that MySQL Server is stopped if it is running stop itenter image description here by clicking on Stop MySQL Server and start MySQL from terminal writing the command mysqlserver.start.

If it doesn't work you can also try:-

If you have also installed MySQL workbench then just uninstall MySQL workbench and MySQL and after that install MySQL first and then MySQL workbench.

Solution 27 - Mysql

For my case, just specify host as 127.0.0.1, instead of localhost:

$ bin/mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Solution 28 - Mysql

Probably you might have faced some issues during the homebrew mysql installation and mysql services might not be running. If that is the case, then it might be worth trying below steps to reinstall mysql properly and then try to connect.

sudo chown -R $(whoami) /usr/local/var

sudo chown -R $(whoami) /Library/Caches/Homebrew

brew uninstall mysql

brew install mysql

mysql.server start   

mysql -uroot  

Solution 29 - Mysql

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.

Solution 30 - Mysql

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

Solution 31 - Mysql

Try this

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

Solution 32 - Mysql

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

Solution 33 - Mysql

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.

Solution 34 - Mysql

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!

Solution 35 - Mysql

Homebrew will absolutely never repair the permissions using a standard reinstall, and chasing down which folder/file is corrupted will take longer than it is worth.

In this case - blow away the mysql install manually and reinstall via homebrew. Takes about 2 minutes.

cd /usr/local/var

sudo rm -rf mysql

brew install mysql

Homebrew installs mysql insecure by default, so if you want a password:

mysql_secure_installation

Then fire it up.

mysql -uroot

Solution 36 - Mysql

After installing MySQL on my mac m1 macOS Monterey, using brew install mysql I got this info:

[System] [MY-013169] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld (mysqld 8.0.27) initializing of server in progress as process 3624

[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.

[ERROR] [MY-013236] [Server] The designated data directory /opt/homebrew/var/mysql/ is unusable. You can remove all files that the server added to it.

[ERROR] [MY-010119] [Server] Aborting

[System] [MY-010910] [Server] /opt/homebrew/Cellar/mysql/8.0.27_1/bin/mysqld: Shutdown complete (mysqld 8.0.27)  Homebrew.

and this warning:

Warning: The post-install step did not complete successfully You can
try again using:   brew postinstall mysql

After that I try to start MySQL using brew services start mysql, and I got this error:

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

I managed to fix it using:

> rm -rf ~/opt/homebrew/var/mysql/
> brew postinstall mysql

Now you can connect with mysql -uroot or mysql -uroot -p.

Solution 37 - Mysql

if you are using Kali linux you might want to specify the host explicitly

do that with mysql -h 127.0.0.1 -u root where 127.0.0.1 is your localhost

Solution 38 - Mysql

Best Alternative to all these issue on local machine -> DOCKER

Follow this steps and you will be as good as a local mysql setup :

  1. Install Docker
  2. Make sure docker is running (Just Open the Docker desktop app)
  3. run this command on terminal -> docker pull mysql
  4. Run the below command and your mysql is up and running on port 3306 with password as root and username also root

-> docker run --name docker-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql

Solution 39 - Mysql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshrewdbeansView Question on Stackoverflow
Solution 1 - MysqlMd Mazedul Islam KhanView Answer on Stackoverflow
Solution 2 - MysqlNirojan SelvanathanView Answer on Stackoverflow
Solution 3 - Mysqlshammerw0wView Answer on Stackoverflow
Solution 4 - MysqlGaurav AgarwalView Answer on Stackoverflow
Solution 5 - MysqlItharView Answer on Stackoverflow
Solution 6 - MysqlmondiekiView Answer on Stackoverflow
Solution 7 - MysqlyndolokView Answer on Stackoverflow
Solution 8 - MysqlPrakhar AgarwalView Answer on Stackoverflow
Solution 9 - MysqlggentzkeView Answer on Stackoverflow
Solution 10 - MysqlIsrat Jahan SimuView Answer on Stackoverflow
Solution 11 - MysqlbarsmagaView Answer on Stackoverflow
Solution 12 - MysqlpmventuraView Answer on Stackoverflow
Solution 13 - MysqlFooBarView Answer on Stackoverflow
Solution 14 - MysqlKenny CasonView Answer on Stackoverflow
Solution 15 - MysqlBrad JohnsonView Answer on Stackoverflow
Solution 16 - MysqlshrewdbeansView Answer on Stackoverflow
Solution 17 - MysqlJackstineView Answer on Stackoverflow
Solution 18 - MysqlAravind VemulaView Answer on Stackoverflow
Solution 19 - MysqlAbdulrahman MasoudView Answer on Stackoverflow
Solution 20 - MysqlKate LeeView Answer on Stackoverflow
Solution 21 - Mysqluser10604324View Answer on Stackoverflow
Solution 22 - MysqlmwangabenView Answer on Stackoverflow
Solution 23 - MysqlJKLView Answer on Stackoverflow
Solution 24 - MysqlNicodemuzView Answer on Stackoverflow
Solution 25 - MysqlMkhululi Cooper Ncube KamzilanView Answer on Stackoverflow
Solution 26 - MysqlMohit ChachraView Answer on Stackoverflow
Solution 27 - MysqlLiu YanView Answer on Stackoverflow
Solution 28 - MysqljlawrenceView Answer on Stackoverflow
Solution 29 - MysqlDiwaker TripathiView Answer on Stackoverflow
Solution 30 - MysqlPatrickView Answer on Stackoverflow
Solution 31 - MysqlGolam SorwarView Answer on Stackoverflow
Solution 32 - MysqlSaif KhanView Answer on Stackoverflow
Solution 33 - MysqlNasif Imtiaz OhiView Answer on Stackoverflow
Solution 34 - MysqlFelipe ValdesView Answer on Stackoverflow
Solution 35 - MysqletusmView Answer on Stackoverflow
Solution 36 - MysqlIvanView Answer on Stackoverflow
Solution 37 - Mysqlnfon andrewView Answer on Stackoverflow
Solution 38 - MysqlTarun PatelView Answer on Stackoverflow
Solution 39 - MysqlAmod KulkarniView Answer on Stackoverflow