Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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

like image 817
Ryan Avatar asked Jun 12 '17 21:06

Ryan


People also ask

How do you fix MySQL can't connect to localhost?

Go to start- MySQL Installer-community and run again the installer as a re-configuration(you will be asked so). Once asked if you want make MySQL instance to run as a Windows service, check the box. In case would do not work, try unistalling and installing again, and check the box to run MySQL as Windows service.

How do I access MySQL server on localhost?

Connecting via a standard connectionEnter 127.0. 0.1 for the host. The default username for a new MySQL installation is root, with a blank password. You can leave the port field blank unless your server uses a different port than 3306.

Can't connect to local MySQL server through socket TMP MySQL sock?

It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ .


2 Answers

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.

like image 173
Vanya Avchyan Avatar answered Sep 23 '22 06:09

Vanya Avchyan


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

like image 29
Lew Perren Avatar answered Sep 25 '22 06:09

Lew Perren