Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock'

When running rake db:migrate, I get this error:

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

I've looked at other people's questions on here and none of their solutions have helped me, for example:

Solution One

mysql.server start

returns:

Starting MySQL

. ERROR! The server quit without updating PID file (/usr/local/var/mysql/something.pid).

Solution Two

mysqladmin variables | grep socket

returns:

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

Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

Further notes: I tried reinstalling mysql using homebrew, which was successful, and I'm still receiving the same errors:

Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
like image 378
rubyandcoffee Avatar asked Dec 17 '16 15:12

rubyandcoffee


People also ask

Can not connect to MySQL through socket?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can't connect to local MySQL server through socket Ubuntu?

"Try" to run mysql via /etc/init. d/mysql start if it gives you the exact same error from above then you need to copy the mysql. server file from the mysql you downloaded which can be found in the support-files folder inside the mysql folder you downloaded or in the /usr/local/mysql folder and copy it to /etc/init.


2 Answers

I solved it!

First, go to database.yml

Change host: localhost to host: 127.0.0.1

That's it!

Edit: This works temporarily, but when I restarted my computer today it began throwing up the same errors. The fix was to just install mysql from the website, then my application could successfully connect to mysql again.

like image 121
rubyandcoffee Avatar answered Oct 19 '22 16:10

rubyandcoffee


env:rails5 mysql5.7.32

As a supplement, I also encountered the problem 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)', but the reason for the error was caused by the socket files

database.yml

default: &default
  socket: /tmp/mysql.sock

login mysql mysql -uroot -p then show variables like 'socket';

+---------------+-----------------------------+
| Variable_name | Value                       |
+---------------+-----------------------------+
| socket        | /var/run/mysqld/mysqld.sock |
+---------------+-----------------------------+

so change database.yml

default: &default
  socket: /var/run/mysqld/mysqld.sock # The line can also be deleted
like image 21
YaEvan Avatar answered Oct 19 '22 16:10

YaEvan