简体   繁体   中英

How to solve “Can't connect to local MySQL server through socket”

This is my config file:

$mysql_hostname = "localhost";
$mysql_user = "xyz";
$mysql_password = "abc";
$mysql_database = "mno";

$IT = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $IT) or die("Opps some thing went wrong");

Here is the warning that I get from it:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/03/10531303/html/iqamah.org/config.php on line 13

How can I fix this?

Please specify the system you're running on. If credentials are ok, this is probably a problem with mysql server. If you're on linux, check if this line returns something on the console. If not, your server is not running:

# netstat -apn | grep mysql

You can also try to connect to mysql here too, so that you check your credentials are ok:

# mysql -h localhost -u user database

If you're on windows, just check if the service is running:

  1. WindowsKey + R
  2. Type services.msc
  3. Check msyql service is running.

I had exactly the same issue

  1. I verified the mysql.sock existed
  2. Update the localhost to 127.0.0.1 in the database.php config file. (user166390 comment)
  3. I also created a folder for the logs in admin/expressionengine/ folder
  4. Configured the error log levels in the config.php file
  5. And also the path to the log folder in this same config.php file

Hope that helps anyone having the same issue. Especially when the log folder didn't exist to troubleshoot.

The error comes only when connection parameters are wrong, Make sure following credentials are correct

$mysql_hostname = "localhost";
$mysql_user = "xyz";
$mysql_password = "abc";

If you can't connect the database it means that somewhere in the execution process is a problem. Revise your hostname, user, password and database name. Then ensure if the database is really on your server with user data you have written to your code - log in with terminal or something like this...

But on line 13 means, that you have to search problem on line 13. Check this line.

First check if your database is really listening; do this by calling

netstat -a 

You should see something like this

TCP    0.0.0.0:3306           MyComputer:0               LISTEN

If you don't seen a listening process on port 3306 your database is not (properly) started or uses another port. Check your environment carefully. If it's a WAMP/LAMP system you can check your admin pages for the phpinfo() section ( mysqli section)

If your port differs from the standard port try adding the appropriate port, eg

$link = mysql_connect('example.com:3331', 'mysql_user', 'mysql_password');

Hope this helps.

< Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

After reading some good advice from here. mine worked out for me. i tried removing the "localhost" and add 127.0.0.1 and it worked like magic vouch everyone contributed here

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