34

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)
6
  • Check if mysql is running and the socket file. If MySQL is running but the socket file is not there then check my.cnf if mysql is configured to listen on that socket. Chances are there that it is listening on port 3306 which is default for MySQL. Commented Dec 17, 2016 at 15:49
  • When I try to check that it's running, I get this error: mysqladmin: connect to server at 'localhost' failed 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! Commented Dec 17, 2016 at 15:58
  • Try ps aux|grep mysql Commented Dec 17, 2016 at 16:00
  • You can also try top and type -o then vsize which will sort by memory consumption and MySQL if running will be near top as it consumes quite a bit of memory compared to other system processes. Commented Dec 17, 2016 at 16:01
  • Mysql isn't there, so it mustn't be running. How can I get it to start? Commented Dec 17, 2016 at 16:04

4 Answers 4

55

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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

3 Comments

what if there is no mysqld.sock file?
@tagaism I think this may be another problem:'mysqld.sock does not exist?', the solution I found is actually to restart the mysql service to re-establish the socket. serverfault.com/a/305056/420636
Cannot login. Says: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
2

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.

Comments

-4

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.

1 Comment

there's not much to learn in your solution since you didn't explain much about it :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.