89

I just had to re-install mysql and I am having a problem starting it up. It cannot find the socket (mysql.sock). The problem is that neither can I. In my Mac OS X 10.4 terminal, I type: locate mysql.sock, and I get back /private/tmp/mysql.sock. It makes sense that the socket file exist in that location, but it actually does not.

  1. How can I find the socket file?

  2. If locate is returning a false location, it must have some sort of memory and probably indexes. How can I refresh that index?

iblue
  • 29,609
  • 19
  • 89
  • 128
Tony
  • 18,776
  • 31
  • 129
  • 193

13 Answers13

146

to answer the first part of your question:

run

% mysqladmin -p -u <user-name> variables

and check the 'socket' variable

hyperbole
  • 3
  • 1
  • 3
behas
  • 3,386
  • 5
  • 27
  • 27
  • 2
    I would totally change this answer to the correct one if I could. If only I had known this sooner! – Steph Rose Jan 11 '12 at 15:36
  • 52
    Ran `mysqladmin variables`. What to do when `error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'`? :) – Alexander Wallin Mar 13 '13 at 20:27
  • I'm getting the same error as @afEkenholm and haven't been able to find a solution. I ran `sudo find / -name mysql.sock` from the root directory and was unable to find mysql.sock anywhere on the system. I installed MySQL using the 64 bit dmg file for Mac OS 10.7. Running Mavericks currently. – b_dubb Apr 24 '14 at 21:31
  • @b_dubb I am the same with your situation, How did you solved your issues? – MeganZhou Oct 20 '14 at 01:20
  • @MeganZhou I cannot recall. I might have tried reinstalling from the DMG file. Sorry I can't be of more help. – b_dubb Oct 28 '14 at 22:12
  • 2
    To quickly look up the socket param without all the others do: mysqladmin variables | grep socket – BAR Oct 30 '14 at 18:25
  • Everything in this answer and comments gives the same `error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'`. Creating the file in /tmp/mysql.sock gives the same error, no matter what the permissions are. I'm using macOS Big Sur with brew mysql@5.6. – SteveExdia Jun 29 '21 at 21:04
61

This solved my problem

mysql_config --socket

UPDATE

mysql_config can tell us where the file mysql.sock should be, but in my case the file didn't exist. So, I've deleted my.cnf:

sudo rm -rf /etc/my.cnf

And then restarted mysql:

brew services restart mysql

The file was created and mysql is now running well.

Thomás Pereira
  • 9,589
  • 2
  • 31
  • 34
Eugene Ramirez
  • 3,053
  • 2
  • 23
  • 17
24

This found it for me:

netstat -ln | grep mysql

Mac OSX

zero_cool
  • 3,960
  • 5
  • 39
  • 54
20

The socket file should be created automatically when the MySQL daemon starts.

If it isn't found, most likely the directory which is supposed to contain it doesn't exist, or some other file system problem is preventing the socket from being created.

To find out where the file should be, use:

% mysqld --verbose --help | grep ^socket
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 4
    Or mysql --verbose --help | grep ^socket – Ondrej Rafaj Dec 19 '13 at 10:03
  • 1
    @Ondrej surely that tells you where the _client_ thinks it's looking? The point of running `mysqld` is that it should tell you where the _server_ actually put it! – Alnitak Dec 19 '13 at 10:17
  • on my xampp installation I couldn't find the daemon so tried without it and it worked :) ... what happened behind the scenes I have no clue of ... – Ondrej Rafaj Dec 19 '13 at 15:28
11

My problem was also the mysql.sock-file.

During the drupal installation process, i had to say which database i want to use but my database wasn't found

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

the system is searching mysql.sock but it's in the wrong directory

all you have to do is to link it ;)

it took me a lot of time to google all important informations but it took me even hours to find out how to adapt , but now i can present the result :D

ps: if you want to be exactly you have to link your /tmp/mysql.sock-file (if it is located in your system there too) to the directory given by the php.ini (or php.default.ini) where pdo_mysql.default_socket= ...

Sergio
  • 28,539
  • 11
  • 85
  • 132
Matthias D.
  • 111
  • 1
  • 2
  • After you changed in php.ini the `pdo_mysql.default_socket` you need to restart the webserver for the changes to load `sudo apachectl restart` – Szekelygobe Oct 06 '18 at 07:35
7

I got the exact path using:

netstat -ln | grep -o -m 1 -E '\S*mysqld?\.sock'

Since this only returns the path and doesn't require any input you could potentially use it in a shell script.

MySQL must be currently running on your machine for this to work. Works for MariaDB too.

mattbell87
  • 565
  • 6
  • 9
2

The original questions seems to come from confusion about a) where is the file, and b) where is it being looked for (and why can't we find it there when we do a locate or grep). I think Alnitak's point was that you want to find where it was linked to - but grep will not show you a link, right? The file doesn't live there, since it's a link it is just a pointer. You still need to know where to put the link.

my sock file is definitely in /tmp and the ERROR I am getting is looking for it in /var/lib/ (not just /var) I have linked to /var and /var/lib now, and I still am getting the error "Cannot connect to local MySQL server through socket 'var/lib/mysql.sock' (2)".

Note the (2) after the error.... I found on another thread that this means the socket might be indeed attempted to be used, but something is wrong with the socket itself - so to shut down the machine - completely - so that the socket closes. Then a restart should fix it. I tried this, but it didn't work for me (now I question if I restarted too quickly? really?) Maybe it will be a solution for someone else.

Amy D
  • 29
  • 4
1
$ mysqladmin variables | grep sock

Try this instead, this will output the demanded result, getting rid of unrelated info.

kenorb
  • 155,785
  • 88
  • 678
  • 743
soarinblue
  • 1,517
  • 3
  • 21
  • 30
1

I'm getting the same error on Mac OS X 10.11.6:

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

After a lot of agonizing and digging through advice here and in related questions, none of which seemed to fix the problem, I went back and deleted the installed folders, and just did brew install mysql.

Still getting the same error with most commands, but this works:

/usr/local/bin/mysqld

and returns:

/usr/local/bin/mysqld: ready for connections.

Version: '5.7.12' socket: '/tmp/mysql.sock' port: 3306 Homebrew

Community
  • 1
  • 1
szeitlin
  • 3,197
  • 2
  • 23
  • 19
0

I couldn't find mysql socket at all so reinstalled mysql server(all tables and phpmyadmin settings were preserved). Here are the commands:

1) Install

sudo apt-get install mysql-server

2) Follow terminal configuration steps

sudo mysql_secure_installation

3) Check status: (Should return "Active: active (running)")

systemctl status mysql.service

Nika Tsogiaidze
  • 949
  • 14
  • 18
0

Unfortunately none of the above have worked in my case. But finally I found solutions.

To find where is mysql.sock file, simply open xampp manager, select MySQL and click on Configure on the right. On the config panel click Open Conf File, and simply search for mysql.sock by pressing the CMD+F shortcut.

In my case, the owner of the mysql.sock was changed, and I had to change it back to root admin with: chmod root:admin mysql.sock

After that the database had been accessed.

szatti1489
  • 352
  • 2
  • 5
  • 16
0

To refresh the locate's database I ran

/usr/libexec/locate.updatedb
Timbinous
  • 2,863
  • 1
  • 16
  • 9
-4

I can't help with question #1, but to refresh locate's file database, run:

updatedb
Chad Birch
  • 73,098
  • 23
  • 151
  • 149