How to solve the error “connect to local MySQL server through socket”

Asked 1 month ago (Jan 24, 2021) in the field of Programming by Marquez

Hi, I am having a problem with MySQL. I faced an error and did a search through the internet but is not solved yet. How to solve the error of client when is connecting to the database when says: can’t connect to local MySQL server through socket ‘/var/run/mysql/mysqld sock’ (2)


Answered 1 month ago (Jan 27, 2021) by Eldernode staff

Dear Marquez,


While you are receiving this error, it means that you have no MySQL service running or maybe you have detected a wrong Linux file socket or TCP/IP port.


1- First, you need to ensure your MySQL database services. To do this, run the command below:

ps xa | grep mysqld | grep -v mysqld


2- If you see no output, it means MySQL is not running. So, the clients cannot connect it. To start MySQL service, type:

sudo systemctl start mysql #Debian/Ubuntu  sudo systemctl start mysqld #RHEL/CentOS/Fedora  


3- Then, to check the status of service, run:

sudo systemctl status mysql #Debian/Ubuntu  sudo systemctl status mysqld #RHEL/CentOS/Fedora  


4- If you see MySQL is failed in the above output, you must restart and check it again:

sudo systemctl restart mysql  sudo systemctl status mysql  


2-1- But if MySQL is running and you still view the error, you should check for TCP/IP port.

ps xa | grep mysqld | grep -v mysqld


2-2- To check which port is listening on the server, run:

sudo netstat -tlpn | grep "mysql"


Feel free to ask any further questions or find some related articles on:





We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way


7 days a week, 24 hours a day