nope2023
nope2023

Reputation: 1788

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)

Upvotes: 34

Views: 39997

Answers (4)

Giuseppe
Giuseppe

Reputation: 5348

I got the same error in an Ubuntu server (with ruby 3.1.2 and rails 7.0.3) and fixed it by simply removing the line:

socket: /tmp/mysql.sock

from config/database.yml, which must have allowed the connection to be established according to defaults.

Upvotes: 2

YaEvan
YaEvan

Reputation: 720

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

Upvotes: 8

Serge
Serge

Reputation: 1

Another answer, type in terminal(zsh):

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

and then

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

should be ok.

Upvotes: -4

nope2023
nope2023

Reputation: 1788

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.

Upvotes: 55

Related Questions