Maxime Lucas
Maxime Lucas

Reputation: 121

PDO can't connect to database

Our school let us sue a small database that I am trying to access through the school network. Connecting using

mysql -D ddddd -h hhhh -u uuuu

works fine (Note that contrary to multiple questions on this site, hhhh is not localhost). However, using

new PDO('mysql:host=hhhh;dbname=dddd, uuuu, pppp)

does not, and brings up an error:

error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''

How do I locate this socket? (NB: In case it is relevant, the schools computers run on FreeBSD and I only have a very limited permissions).

Upvotes: 1

Views: 1232

Answers (2)

Arjun J Gowda
Arjun J Gowda

Reputation: 730

Use correct IP address to connect.

If u r using localhost then by default socket will be used on Linux so Use 127.0.0.1 as host instead of localhost.

Upvotes: 0

calvix
calvix

Reputation: 193

the error:

error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''

means that the PDO is trying to connect to MySQL via local socket (linux socket is file).

But my guess is that you want to connect via hostname/ip. Your code Has to be wrong. From the example it looks like you are using php. Try double check everything.

Here is an example from php.net:

<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

Upvotes: 1

Related Questions