1

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).

Maxime Lucas
  • 121
  • 5
  • May be this help you :- [link](http://stackoverflow.com/questions/22436028/cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock-2) [link](http://stackoverflow.com/questions/5376427/cant-connect-to-local-mysql-server-through-socket-var-mysql-mysql-sock-38) [link](https://teamtreehouse.com/community/cant-connect-to-local-mysql-server-through-socket) – Ranjeet Singh Apr 12 '16 at 08:31

2 Answers2

1

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();
}

?>
calvix
  • 173
  • 1
  • 2
  • 12
  • Thank you. I had seen the example from php.net. What bugs me is that this example specifically uses 127.0.0.1. Does host=... really works with a hostname? Or should I find mysql.sock and use something like unix-socket=... ? – Maxime Lucas Apr 12 '16 at 08:47
  • php PDO libraby should definitely work with hostnames, there has to be error somewhere, can you post your code somewhere like pastebin.com ? (clear passwords before posting) – calvix Apr 12 '16 at 08:54
  • So I am not exactly sure what caused the issue, but I think it was because I used dbname=... before host=... Anyway, it works now. Thank you! – Maxime Lucas Apr 12 '16 at 12:28
0

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.

Arjun J Gowda
  • 720
  • 10
  • 21