170

I have installed MySQL server and trying to connect to it, but getting the error:

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

I have checked my /tmp directory and there is no mysql.sock. I can't find mysql.sock anywhere. I read that it might be in

    /var/lib/mysql/mysql.sock

But I checked there as well and there is even no mysql directory, only some postfix thing inside /lib. Could anyone help me with this problem?

Oleksandr Makarenko
  • 779
  • 1
  • 6
  • 18
user3344382
  • 1,941
  • 4
  • 18
  • 17
  • 2
    I'm working on Mac OS X – user3344382 Mar 16 '14 at 11:09
  • Hello, can you paste the command you are using to connect? Also have you started MySQL? – Mark Butler Mar 16 '14 at 11:09
  • @MarkButler I'm using this command: /usr/local/mysql/bin/mysql – user3344382 Mar 16 '14 at 11:11
  • right, it's just the server wasn't running. However, I can not still understand where is mysql.sock, might there be any other hidden places where it may reside? – user3344382 Mar 16 '14 at 11:20
  • This error is very general, and it can be for a variety of reasons. For a neat summary see here: https://medium.com/@7anac/mysql-error-the-server-quit-without-updating-pid-file-ce320ff75828 – Janac Meena May 08 '20 at 13:48
  • For me, instead of connecting it using `mysql`, connecting via host configuration like noted here - https://stackoverflow.com/a/11658142/1612469 I was able to connect to the database – Aleks Jun 13 '21 at 23:10

21 Answers21

304

Try to start the MySQL server:

mysql.server start

user664833
  • 18,397
  • 19
  • 91
  • 140
Casey Robinson
  • 3,268
  • 2
  • 14
  • 21
64

I got the same question after updating OS X Yosemite, well the solution is quite simple, check system preference -> mysql, the status was STOP. Just restart it and it works fine on my mac now.

keren
  • 641
  • 5
  • 3
48

For MAMP

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

From https://coderwall.com/p/w5kwzw/solved-can-t-connect-to-local-mysql-server-through-socket-tmp-mysql-sock

UPDATE: Every time my computer restarts I have to enter this command, so I created a shortcut.

Do the following in terminal type:

~: vi ~/.profile

Add

alias ...='source ~/.profile'
alias sockit='sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock'

Save.

In terminal type:

~: ... to source the .profile config.

Now in terminal you can just type

~: sockit
Bryan
  • 17,201
  • 24
  • 97
  • 123
35

Following command resolved my issue:

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

sudo mysql.server start
Nikesh Suwal
  • 482
  • 4
  • 5
  • 1
    After searching for 3 hours this solved my problem. I just installed mysql through homebrew with `brew install mysql` but nothing of the commands homebrew told me to run worked properly. This made every problem go away... Thank you!! What exactly is that first line that you wrote? What does it mean? – mesqueeb Jun 30 '16 at 05:26
  • Thanks, Works fine ! – Nirojan Selvanathan Nov 22 '16 at 08:22
  • 1
    Hey for me it was: sudo chown -R _mysql:mysql /usr/local/mysql/data – Junaid Bhura Jan 06 '17 at 12:38
  • chown -R change the owner -R recursively – mercury Mar 09 '17 at 02:23
  • A pristine 10.13.1 and homebrew gave this error when installing mariadb (10.2.10). This owner change fixed it, so now `sudo brew services start mariadb`also works to start mariadb at boot (not just at login). YAY! Thank you! – iggie Nov 22 '17 at 06:06
24

After trying all solutions it worked only for me after specifying the host

mysql -u root -p -h127.0.0.1

when asking for password

Enter password:

press enter

and it will work , if everything is ok as above .

mercury
  • 2,390
  • 3
  • 30
  • 39
  • 3
    I setup mysql with a docker container by using Docker for Mac, and map its 3306 port to Mac. Without adding the `-h 127.0.0.1`, it always prompt `Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)`; after adding the argument, it works well. – Evi Song Jun 03 '17 at 07:24
  • 1
    This is because going through 127.0.0.1 doesn't use the socket, I think. More of a narrow workaround. – Lucas Feb 13 '19 at 18:33
  • 1
    Running `mysql -u root -h127.0.0.1` without the `-p` flag worked for me, since when i installed MySQL I never ran the `mysql_secure_installation` command. My understanding is that in this case the installation defaults to no required password. – Richie Thomas Mar 15 '19 at 23:31
  • 2
    Only solution that worked for me – Samia Ashraf Jul 25 '23 at 11:36
14

After struggling for hours the only thing which worked was

sudo mysql.server start

Then do a secure installation with

mysql_secure_installation 

Then connect to the db via

mysql -uroot -p

Mysql is installed via homebrew and the version is

Server version: 5.7.21 Homebrew

Specifying the version might be helpful as the solution may be different based upon the version.

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
13

Try this it worked for me.

sudo /usr/local/mysql/support-files/mysql.server start 
ah bon
  • 9,293
  • 12
  • 65
  • 148
vasanth vasu
  • 401
  • 5
  • 13
10

In your mysql config file, which is present in /etc/my.cnf make the below changes and then restart mysqld dameon process

[client]
socket=/var/lib/mysql/mysql.sock

As well check this related thread

Can't connect to local MySQL server through socket '/tmp/mysql.sock

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125
8

Following resolved my issue:

Check where is your MySQL server is listning to: netstat -nlp If it is listning to TCP then use 127.0.0.1 while connecting to DB instead of "localhost"

Check MySQL doc here

Umesh
  • 81
  • 2
6

First Type this-:

brew services start mysql

Then this -:

mysql -uroot
Rudra shah
  • 804
  • 8
  • 10
5

Type in the terminal as follows:

mysql.server start
Frank Cheng
  • 77
  • 2
  • 3
4

If you are using XAMPP in Mac OS X and have installed MySQL with Homebrew you may have this problem. In XAMPP manager window go to Manage Servers and select MySQL, then click configure and open the configuration file, there you have the socket file path, put the path in your MySQL host config and it should work.

It's something like this:

...
[client]
#password   = your_password
port        = 3306
socket      = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
...

then, for instance in Django:

...    
DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.mysql",
            "NAME": "database_name",
            "USER": "user",
            "PASSWORD": "password",
            "HOST": "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock",
            "PORT": "",
        }
    }
...

Hope this helps.

Cairo
  • 495
  • 9
  • 19
3

First, knowing where the data directory was for me was the key. /usr/local/var/mysql In here, there was at least one file with extension .err preceded with my local machine name. It had all info i needed to diagnose.

I think i screwed up by installing mysql 8 first. My app isn't compatible with it so i had to downgrade back to 5.7

My solution that worked for me was going to /usr/local/etc/my.cnf

Find this line if its there. I think its mysql 8 related:

mysqlx-bind-address = 127.0.0.1 

Remove it because in the mysql 5.7 says it doesnt like it in the error log

Also add this line in there if its not there under the bind-address.

socket=/tmp/mysql.sock

Go to the /tmp directory and delete any mysql.sock files in there. On server start, it will recreate the sock files

Trash out the data directory with mySQL in the stopped state. Mine was /usr/local/var/mysql . This is the same place where the logs are at

From there i ran

>mysqld --initialize

Then everything started working...this command will give you a random password at the end. Save that password for the next step

Running this to assign my own password.

>mysql_secure_installation 

Both

>brew services stop mysql@5.7

and

>mysql.server start

are now working. Hope this helps. It's about 3 hours of trial and error.

adrian
  • 147
  • 1
  • 1
  • 5
2

Stoping and starting the mysql server from terminal resolved my issue. Below are the cmds to stop and start the mysql server in MacOs.

sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start

Note: Restarting the services from Mac System preference didn't resolve the issue in my mac. So try to restart from terminal.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
1

Faced the same issue while taking mysql dump: Can't connect to local MySQL server through socket '/tmp/mysql.sock

Try to give the path for mysql.sock explicitly.

  1. type ps -ef|grep -mysql

    Get the path for mysql.sock from this command, e.g /var/lib/mysql/mysql.sock

  2. mysqldump --socket=/var/lib/mysql/mysql.dump -u username -pPassword

    you can try this with any mysql command

Sana
  • 11
  • 1
0

For CentOS, the file to init mysql is located here:

/etc/init.d/mysqld start
etusm
  • 4,082
  • 1
  • 30
  • 26
0

I have spent lots of time doing this I want to put my django app on my server and when I run python manage.py migrate I met this questions

And!! I set this ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock It worked finally!

LittleCabbage
  • 71
  • 1
  • 3
0

I have faced the same issue. Here is how I have fixed it.

Step 1: Remove mysql using command:

brew uninstall --force mysql

Step 2: Run command brew doctor which will give you some hint related to your brew packages.

Step 3: Cleanup brew packages using command:

brew cleanup

Step 4: Move/delete previously installed mysql data using command:

mv /usr/local/var/mysql/ /usr/local/var/old_mysql

Step 5: Finally install mysql again using command:

brew install mysql
amitshree
  • 2,048
  • 2
  • 23
  • 41
0

I start mysql from settings,it works!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 02 '23 at 09:49
0

Try to restart MySql server using

brew services stop mysql@5.7

if even after this issue is not resolved then try following command

brew postinstall mysql@5.7

if you gets following error -

2023-06-18T09:23:53.239569Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-06-18T09:23:53.241698Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2023-06-18T09:23:53.241717Z 0 [ERROR] Aborting

then please delete mySql directory. Search location of your file on Google. In my case it was /usr/var/mysql

Shekhar Patil
  • 337
  • 4
  • 14
-2

If you're running on a macOS it's just easier to first check go to 'System Preferences' and see if MySQL is running or not.