Ryan
Ryan

Reputation: 2886

Can't connect to local MySQL server through socket (From time to time)

I have a LAMP stack setup. Occasionally, I get the following error message when I open some page from the browser:

Error creating the connection!: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I think the server was configured correctly. The problem happens about every two months. Every time when I reboot the Linux server, or restart mysql, the problem was gone. I want to fix this problem permanently. Can anyone give me some idea?

Much appreciated.

EDIT

The problem occurs again and I checked the mysqld.sock file, it was not there. Do you have any idea how to fix the problem? – Ryan Jul 23 at 16:24

Upvotes: 7

Views: 10831

Answers (4)

Drawn Yang
Drawn Yang

Reputation: 11

If the mysqld.sock file doesn't exist, that is to say your config file is not correct.Check your mysql config file in /etc/mysql/my.conf, find the socket config just as Vanya Avchyan says.

I think the socket config is /var/run/mysqld/mysqld.sock, but in fact your mysql process runs in other place sock file. I used to met that problem, the real socket file exists in /tmp/mysqld.sock. So run

sudo find / -name 'mysqld.sock'

to find the real sock file and change my.conf to this real place, restart your mysql. May have work.

Upvotes: -2

Vanya Avchyan
Vanya Avchyan

Reputation: 880

If your file my.cnf (usually in the /etc/mysql/ folder) is correctly configured with

socket=/var/run/mysqld/mysqld.sock

modified

#bind-address = 127.0.0.1

to

bind-address = localhost

you can check if mysql is running with the following command:

mysqladmin -u root -p status

try changing your permission to mysql folder. If you are working locally, you can try:

sudo chmod -R 755 /var/run/mysqld/

And then restart the mysql.

Good luck.

Upvotes: 2

Roberto Gonçalves
Roberto Gonçalves

Reputation: 3434

You could try change the permission of your MySQL sock file like this:

chmod 777 '/var/run/mysqld/mysqld.sock'

It is a test to see if whatever user mysqld is using, it will acess your mysqld.sock file. So, reboot your MySQL and change the permission of mysqld.sock. And you need to check that if your sock folder can be accessed through any mysqld process.

Upvotes: 1

Lew  Perren
Lew Perren

Reputation: 1239

Could it be the log file getting too large and rebooting flushes it. See this in docs on server maintenance and logfiles. Also see discussion at digital ocean. Appears to be confirmed by discussion at serverfault

Upvotes: 1

Related Questions