Solved

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

Posted on 2014-12-30
7
769 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 83

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 109

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 83

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 83

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 83

Expert Comment

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

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

776 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