Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_connect returning "Can't connect to local MySQL server through socket" on remote host connection?

Tags:

php

mysql

I have a server that's returning an unexpected, intermittent error and wondering if anyone has either experienced it before or can guess what might be going on. My search has so far has been unfruitful.

Im connecting to a remote mysql server using mysql_connect() in the usual way, but for the last 2 weeks (couple of times a day) the connection to the database fails and I get an unable to connect to socket error?

Error: 2002 - Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Obviously this is unexpected as Im trying to connect to a remote host. Does mysql_connect default to localhost if the remote host is unavailable? I've not known it to do that, and nothing in the docs that say it does.

There doesn't seem to be any pattern to the failed connections, various files and it happens at all sorts of time of day. Im thinking it might be network traffic related, because if you refresh the page straight away it connects fine, but am confused as to why Im getting an unable to connect to socket error.

the database provider has checked and are unable to find an issue with the server itself. anyone have any ideas?

like image 535
gavtaylor Avatar asked Dec 28 '22 18:12

gavtaylor


2 Answers

Im sure it doesnt default to localhost, and im also pretty sure that it is the remote server sending you the error message. So it's "Local" on your mysql server, not local on "your" server.

In this case its probably due to downtime on the remote server.

Gr.

like image 129
Hans Wassink Avatar answered Dec 30 '22 10:12

Hans Wassink


ok, facepalm moment... after this hitting me for ages I finally found why mysql_error() was reporting the socket error. in my code there was a mysql_select_db() that I didnt notice and it was that, thats reporting the socket error as there is no open connection for it to use.

actual error the mysql_connect is reporting is (2003) Can't connect to MySQL server on 'mysql.hostname.tld'. Im still leaning towards some sort of DNS lookup/network traffic issue, Im currently using the IP address to connect to see if that errors.

But as far as this question is concerned, the answer is "Im a numpty".

like image 30
gavtaylor Avatar answered Dec 30 '22 10:12

gavtaylor