简体   繁体   中英

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)

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM