Solved

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

Posted on 2014-12-30
7
747 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
  • 4
  • 2
7 Comments
 
LVL 82

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 108

Accepted Solution

by:
Ray Paseur earned 250 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 82

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 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 82

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now