Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL/Php - Can't connect to local MySQL server through socket '/tmp/mysql.sock' (96)

Posted on 2014-12-30
7
Medium Priority
?
796 Views
Last Modified: 2015-01-09
Dear Experts,

I have found this small Php code on a great site (http://www.plus2net.com/php_tutorial/php_paging-download.php), basically targeting of the pagination of an MySQL table:

<?Php
$dbhost_name = "localhost";
$database = "xx";// database name
$username = "xx"; // user name
$password = "xx"; // password 

//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?> 

Open in new window


I am sure that it works, however not at my internet service provider (Yahoo Small Business account), because at running this code bringing an error message:

Error!: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (96)

I would like to understand what does that mean and eventually what to change for having it working. With other methods with other php codes I am able to login to the database and running queries, but with this one not

Thanks,
0
Comment
Question by:csehz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40524284
That means that you probably have not used the correct credentials.  Many hosting companies did not put the MySQL server on the same machine as the web site.  That would use 'localhost'.  There is probably a different server name or IP address for the MySQL server for your account.  You need to look it up and use that.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 40524816
Agree with Dave -- you're headed in the right direction, but you need to get some information from your web hosting provider.  Here is the standard PDO "startup scenario" -- there are four pieces of data you need.  These pieces of data are assigned to the variables that start with $db_...

// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // THIS MAY BE OK, BUT ASK ABOUT IT
$db_name = "??";
$db_user = "??";
$db_word = "??";

// WARNING: AN UNCAUGHT CONNECT ERROR WILL BARK OUT THE DB CREDENTIALS!
// $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, 'foo');

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$dsn = "mysql:host=$db_host;dbname=$db_name";
try
{
    $pdo = new PDO($dsn, $db_user, $db_word);
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error($exc->getMessage(), E_USER_ERROR);
}

// SHOW THE PDO CONNECTION OBJECT
var_dump($pdo);
echo PHP_EOL;

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40524847
I noticed (after posting) that he said that he could connect "with other php codes".  He probably just needs to copy the credentials from those files to this one.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 1

Author Comment

by:csehz
ID: 40541411
Thanks very much the answers, sorry for the late during the holidays period was not able to test it.

Yes as you also mentioned with other codes I can login without any issue, for example with this:

<?Php
$connect = mysql_connect("mysql", "xx", "xx") or
			die ("Check your server connection.");
		$k = "SET NAMES latin2"; mysql_query($k);
		mysql_select_db("lang") or die(mysql_error());
?> 

Open in new window


Comparing this code to that original one in the question, which is maybe not clear the $db_host = "localhost"; but in my understanding that should be left as unchanged?
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 total points
ID: 40541468
Not necessarily.  If the working code has something else for the 'host', use that.  'localhost' does not work for every instance.  I never use it on my own systems.  I have to use it on some web hosts because they have it set up to only work that way.
0
 
LVL 1

Author Closing Comment

by:csehz
ID: 40541511
Thanks for your assist in the question
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40541516
You're welcome.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this article, we’ll look at how to deploy ProxySQL.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question