简体   繁体   中英

How to resolve mysql connection error?

I have an application designed in mysql and php. Earlier this app was working fine. But now it is not. Its giving me an error :

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

How can I resolve this error ?

The application is running on a redhat machine.

Does the directory "/var/lib/mysql" exist? also please check your mysql user (or better, mysql group) have writer permission. If so, please try re-starting mysql and see what happens

You must follow the below steps to debug the error:-

  1. start mysql engine.

  2. check below database connecting variable

    a. host name

    b. username

    c. password

    d. database name

  3. now check query for connecting with mysql.

  4. now check whether it's able to connect with database or not.

I think this will help You to resolve the problem. If it persists then let me know.

Reference link you may find the solution.According to the link it says:

Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.

i think your mysql server is not running. Make sure your mysql server is running

use

# mysqladmin -u root -p status

to check status

if it is running , try to connect from command line. And execute some simple select statement from command line. If your are able to do that, make sure that username and password (used in command line) are used in your application too.

If your are application working sometime and sometime not, in that case make sure you are closing connections in your program after use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM