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

MysqlSql

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

Try to start the MySQL server:

mysql.server start

Solution 2 - Mysql

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.

Solution 3 - Mysql

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

Solution 4 - Mysql

Following command resolved my issue:

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

sudo mysql.server start

Solution 5 - Mysql

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 .

Solution 6 - Mysql

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.

Solution 7 - Mysql

Try this it worked for me.

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

Solution 8 - Mysql

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

https://stackoverflow.com/questions/16325607/cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock

Solution 9 - Mysql

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

Solution 10 - Mysql

First Type this-:

brew services start mysql

Then this -:

mysql -uroot

Solution 11 - Mysql

Type in the terminal as follows:

mysql.server start

Solution 12 - Mysql

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.

Solution 13 - Mysql

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.

Solution 14 - Mysql

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.

Solution 15 - Mysql

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

Solution 16 - Mysql

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

/etc/init.d/mysqld start

Solution 17 - Mysql

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!

Solution 18 - Mysql

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

Solution 19 - Mysql

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.

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
Questionuser3344382View Question on Stackoverflow
Solution 1 - MysqlCasey RobinsonView Answer on Stackoverflow
Solution 2 - MysqlkerenView Answer on Stackoverflow
Solution 3 - MysqlBryanView Answer on Stackoverflow
Solution 4 - MysqlNikesh SuwalView Answer on Stackoverflow
Solution 5 - MysqlmercuryView Answer on Stackoverflow
Solution 6 - MysqlKrishnadas PCView Answer on Stackoverflow
Solution 7 - Mysqlvasanth vasuView Answer on Stackoverflow
Solution 8 - MysqlRahulView Answer on Stackoverflow
Solution 9 - MysqlUmeshView Answer on Stackoverflow
Solution 10 - MysqlRudra shahView Answer on Stackoverflow
Solution 11 - MysqlFrank ChengView Answer on Stackoverflow
Solution 12 - MysqlCairoView Answer on Stackoverflow
Solution 13 - MysqladrianView Answer on Stackoverflow
Solution 14 - MysqlLakshmi NarayananView Answer on Stackoverflow
Solution 15 - MysqlSanaView Answer on Stackoverflow
Solution 16 - MysqletusmView Answer on Stackoverflow
Solution 17 - MysqlLittleCabbageView Answer on Stackoverflow
Solution 18 - MysqlamitshreeView Answer on Stackoverflow
Solution 19 - MysqlMicah LotemoView Answer on Stackoverflow