user3344382
user3344382

Reputation: 1991

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

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?

Upvotes: 170

Views: 291214

Answers (22)

Bloem
Bloem

Reputation: 1

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 :)

Upvotes: 0

Shekhar Patil
Shekhar Patil

Reputation: 337

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

Upvotes: 0

Plants Water
Plants Water

Reputation: 1

I start mysql from settings,it works!

Upvotes: 0

Sana
Sana

Reputation: 11

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

Upvotes: 1

vasanth vasu
vasanth vasu

Reputation: 401

Try this it worked for me.

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

Upvotes: 13

amitshree
amitshree

Reputation: 2298

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

Upvotes: 0

Micah Lotemo
Micah Lotemo

Reputation: 1

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.

Upvotes: -2

Lakshmi Narayanan
Lakshmi Narayanan

Reputation: 67

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.

Upvotes: 1

Frank Cheng
Frank Cheng

Reputation: 77

Type in the terminal as follows:

mysql.server start

Upvotes: 5

Rudra shah
Rudra shah

Reputation: 851

First Type this-:

brew services start mysql

Then this -:

mysql -uroot

Upvotes: 7

adrian
adrian

Reputation: 317

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.

Upvotes: 3

LittleCabbage
LittleCabbage

Reputation: 71

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!

Upvotes: 0

Krishnadas PC
Krishnadas PC

Reputation: 6539

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.

Upvotes: 14

Casey Robinson
Casey Robinson

Reputation: 3268

Try to start the MySQL server:

mysql.server start

Upvotes: 304

Bryan
Bryan

Reputation: 17601

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

Upvotes: 49

mercury
mercury

Reputation: 2765

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 .

Upvotes: 24

Cairo
Cairo

Reputation: 616

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.

Upvotes: 4

Nikesh Suwal
Nikesh Suwal

Reputation: 492

Following command resolved my issue:

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

sudo mysql.server start

Upvotes: 36

etusm
etusm

Reputation: 4220

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

/etc/init.d/mysqld start

Upvotes: 0

keren
keren

Reputation: 641

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.

Upvotes: 64

Umesh
Umesh

Reputation: 81

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

Upvotes: 8

Rahul
Rahul

Reputation: 77934

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

Upvotes: 10

Related Questions