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?

6
  • 2
    I'm working on Mac OS X Commented Mar 16, 2014 at 11:09
  • Hello, can you paste the command you are using to connect? Also have you started MySQL? Commented Mar 16, 2014 at 11:09
  • @MarkButler I'm using this command: /usr/local/mysql/bin/mysql Commented Mar 16, 2014 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? Commented Mar 16, 2014 at 11:20
  • This error is very general, and it can be for a variety of reasons. For a neat summary see here: medium.com/@7anac/… Commented May 8, 2020 at 13:48

22 Answers 22

304

Try to start the MySQL server:

mysql.server start

Sign up to request clarification or add additional context in comments.

4 Comments

ERROR! The server quit without updating PID file
@wegginho Possible solution to your error message if you installed MySQL via homebrew PID error on mysql server start
forgot start mysql services!
@bastianwegge try checking the error file in /usr/local/var/mysql/USERNAME.local.err in my case I had to delete /tmp/mysql.sock then restart using mysql.server start
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.

6 Comments

Thanks a ton @keren so simple, i wasted a good 45 minutes on changing settings in my my.cnf file when the whole time this was the only problem.
Worked for me too thanks, was not having idea in terms DMG installation.
Um, what? Why would OSX's System Preferences have a MySQL option?
After you install MySQL (or at least after I install MySQL from their website) it shows up at the bottom of the system preferences next to Java and any other 3rd party preference options.
Didn't work at first, worked after a OS restart. Thanks!!
|
49

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 

4 Comments

Awesome. Just the fix I was looking for.
I've came back to this post multiple times and it always works.
Upgraded to mysql 5.7.1 outside of MAMP and ran into the error. This fixed it for me
If you are on a mac book and you se MAMP this works
36

Following command resolved my issue:

sudo chown -R _mysql:mysql /usr/local/var/mysql sudo mysql.server start 

4 Comments

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?
Hey for me it was: sudo chown -R _mysql:mysql /usr/local/mysql/data
chown -R change the owner -R recursively
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 mariadbalso works to start mariadb at boot (not just at login). YAY! Thank you!
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 .

3 Comments

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.
This is because going through 127.0.0.1 doesn't use the socket, I think. More of a narrow workaround.
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.
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.

Comments

13

Try this it worked for me.

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

2 Comments

Why this path? while using "where is mysql" command(MAC OS) giving output /usr/local/mysql/bin/.How to identify this path in Mac?
Ya , The path changes accordingly
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

4 Comments

there's no my.cnf file in /etc directory. I think I can create that file manually, but as I said there is no mysql directory in /var/lib, how can the socket be there?
Check the related thread I have posted as well as check whether your mysql server is actually running or not.
I found the file in /etc/mysql/my.cnf
if you installed with brew you need to use /tmp/mysql.sock as the sock location
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

1 Comment

this seems to work for me and proves mysql is alive but why can I not connect to local socket?
7

First Type this-:

brew services start mysql 

Then this -:

mysql -uroot 

Comments

5

Type in the terminal as follows:

mysql.server start 

Comments

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.

Comments

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

and

>mysql.server start 

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

Comments

1

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.

1 Comment

If MySQL is installed using brew, just run mysql.server start
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

Comments

0

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

/etc/init.d/mysqld start 

Comments

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!

Comments

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 

Comments

0

I start mysql from settings,it works!

1 Comment

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.
0

Try to restart MySql server using

brew services stop [email protected]

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

brew postinstall [email protected]

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

Comments

0

I was getting the same error. I overlooked some Caveats when reinstalling mysql via homebrew as listed:

==> Caveats Upgrading from MySQL <8.4 to MySQL >9.0 requires running MySQL 8.4 first: - brew services stop mysql - brew install [email protected] - brew services start [email protected] - brew services stop [email protected] - brew services start mysql 

I followed those steps and got it working again :)

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.