[mysql] 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?

This question is related to mysql sql

The answer is


Run the following on the command line :

$ mysql.server start

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.


this post helped did it for me, I'll rewrite the steps here (note: i'll be also writing the output of your commands.. just so that you know you're on track)

first stop the server if running:

[root@servert1 ~]# /etc/init.d/mysqld stop
Stopping MySQL: [ OK ]

run an sql dameon on a separate thread

[root@servert1 ~]# mysqld_safe --skip-grant-tables &    
[1] 13694    
[root@servert1 ~]# Starting mysqld daemon with databases from /var/lib/mysql

open a separate shell window and type

[root@servert1 ~]# mysql -u root

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.0.77 Source distribution



Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

start using mysql

mysql> use mysql; 

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A



Database changed

update the user table manually with your new password (note: feel free to type mysql> show tables; just to get a perspective on where you are)

NOTE: from MySQL 5.7 passwords are in the authenication_string table, so the command is update user set authentication_string=password('testpass') where user='root';

mysql> update user set password=PASSWORD("testpass") where User='root';

Query OK, 3 rows affected (0.05 sec)

Rows matched: 3 Changed: 3 Warnings: 0

flush privileges (i'm not sure what this privileges is all about.. but it works)

mysql> flush privileges; 

Query OK, 0 rows affected (0.04 sec)

quit

mysql> quit

Bye

stop the server

NOTE: on OS X or macOS, mysql.server is located at /usr/local/mysql/support-files/.

mysql.server stop

Shutting down MySQL
.130421 09:27:02 mysqld_safe mysqld from pid file /usr/local/var/mysql/mycomputername.local.pid ended
 SUCCESS! 
[2]-  Done                    mysqld_safe --skip-grant-tables

kill the other shell window that has the dameon running (just to make sure)

now you are good to go! try it:

[root@servert1 ~]#  mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.10 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

done!


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

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.


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 .


Following command resolved my issue:

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

sudo mysql.server start

It looks like you need a MySQL server installed, there is install packages on mysql's site, or you can install through macports (I assume from the darwin11 line). I installed mine via ports, and the socket lives in /opt/local/var/run/mysql5/.


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.


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.


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


Try this it worked for me.

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


Also, if you got this error and installed mysql via Homebrew, I found that this works (though you need to change "5.6.12" to your own version):

/usr/local/Cellar/mysql/5.6.12/bin/mysql.server restart

I just created a file ~/restartMysql.sh in my home directory (with only the line above in it) so that I can just use this whenever MySQL is acting up


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


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!


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.


Type in the terminal as follows:

mysql.server start

First Type this-:

brew services start mysql

Then this -:

mysql -uroot

I hope this helps somebody. I was getting the same error, but seemingly for a much different reason than other people.

I have 2 CentOS machines.

I copied my.cnf to the new machine, not realizing that I had upgraded the old machine to MySQL 5.6, and the new machine had 5.5 installed. When I commented out the 5.6-only directives, MySQL started as expected. (and now I am running the upgrade so I can apply the massively useful innodb_buffer_pool_dump_at_shutdown and innodb_buffer_pool_load_at_startup directives)

I would suggest trying a bare minimum my.cnf. If MySQL starts up, then you've found the source of your problem.


You need to follow the directions to install and start the server.

The command varies depending on how you installed MySQL. Try this first:

sudo /Library/StartupItems/MySQLCOM/MySQLCOM start

If that fails:

cd /usr/local/mysql
sudo ./bin/mysqld_safe
(Enter your password, if necessary)
(Press Control-Z)
bg


In my case, I don't got tmp folder setting up right. What I end up with these steps:

1. cd /

2. ln -s private/tmp /tmp

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.


Try to start the MySQL server:

mysql.server start


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

/etc/init.d/mysqld start

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