MySql can't connect on live server

Couldn't connect to serverSQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2 "No such file or directory")Please try again later.

That is the message I’m getting. I have checked the dbname, username and password. Can anybody share any ideas?

Open PHP.ini.
Find:
mysql.default_socket
Does the location listed there A: Exist. and B: Contain a file called mysql.sock?

If no, your ini is pointing to the wrong place. Find mysql.sock, and point PHP to it.

Thanks but how do you open php.in on a live server?

php.ini is a file just like any other. If you arnt in control of the box in question, you should contact your host to find out why their box is not configured for mysql.sock.

Thanks again. I have about 2 accounts on the same server. Php is running fine on one of them but not this one. Do you think it could be a minor code error?

It might be. You haven’t shown us the code that error message is refering to so it is hard to say.

Here is index.php:

<?php 

$page = "home";

include 'inc/db.php';

// recent properties list

try {	
	$property_results = $pdo->query("SELECT * from properties order by id DESC LIMIT 9");
	$properties = $property_results->fetchAll(PDO::FETCH_OBJ);
	}
	
catch (PDOException $e) {
	echo 'Sorry, cannot fetch data at this point. Contact system admin at 123456789.';
	}

try {	
	$slider_results = $pdo->query("SELECT * from properties where keywords LIKE 'yes' order by id DESC LIMIT 4");
	$sliders = $slider_results->fetchAll(PDO::FETCH_OBJ);
	}
	
catch (PDOException $e) {
	echo 'Sorry, cannot fetch data at this point. Contact system admin at 123456789.' . $e->getMessage();
	}


include 'inc/header.php';

include 'inc/slider.php';

?>

<div class="grid-container">
<div class="wrap85">
<h2> Recent Listings <span class="view-all"><a href=""> [view all listings] </a></span></h2><br />
<br />

<?php foreach ($properties as $property): ?>
<div class="box">

<header>
<h3> <?php echo $property->category;?> </h3>
<p> <?php echo $property->location;?> </p>
</header>
<img src="get.php?id=<?php echo $property->id; ?>"/>

<p class="price"> <?php echo $property->price;?> </p>

<p> <a href="single.php?id=<?php echo $property->id; ?>" target="_blank"> View More </a> </p>
<div class="clear"></div>
</div><!-- end box-->
<?php endforeach; ?>


<div class="clear"></div>
</div><!-- end wrap-->
</div><!-- end grid container-->

<?php include 'inc/footer.php'; ?>

The db.php file:

<?php

try {
	$pdo = new PDO ('mysql:hostname=localhost; dbname=realdb','username','password');
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$pdo->exec('SET NAMES "utf8"');
	}

catch (PDOException $e) {
	echo 'Couldn\'t connect to server' . $e->getMessage() . 'Please try again later.';
	}
	
?>

This is what I got as response from the host, not sure what to do with it. I’ve been asked by one person here to change stuff in the PHP.ini file but my host doesn’t allow for it.

Hi,

The default location for the Unix socket file that the server uses for communication with local clients is /tmp/mysql.sock.

We look forward to being of service.

Thank you.

$pdo = new PDO ('mysql:unix_socket=/tmp/mysql.sock; dbname=realdb','username','password');

Cautionary warning that your local environment will probably not work with that. So you will need to change out the connection credentials per environment.

On changing that I get the same error with the updated socket name.

Couldn’t connect to serverSQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2 “No such file or directory”)

I’ve uploaded the exact same files to another domain I have registered with those guys and it’s working. Just not on this domain. I ask them that and they say they do not know and that it is something I should figure.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.