?
Solved

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

Posted on 2014-12-30
7
Medium Priority
?
787 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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