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)
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.
"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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With