views:

1233

answers:

4

Hi, folks !

So the problem changed from what it was, i'll leave the original question below to prevent bad reviews on answers like I had after someone editing his question I answered :

So I am working on a (really lame) shared hosting which has PDO installed, but it doesn't work. With default parameters

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
    echo 'Connected to database';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

it throws this message :

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

With a simple mysql_connect, it works.

And the socket path seems correct (both phpinfo and this query :

show variables like 'socket';

confirm.

Localhost redirects to 10.103.0.14 (this data comes from mysql_get_host_info() and in phpMyAdmin)

In the PDO, if i replace localhost by 127.0.0.1 i will get

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111) 

And if i replace localhost by 10.103.0.14 :

Access denied for user 'USER_NAME'@'10.103.0.14' (using password: YES

Both IP adress (127.0.0.1 and 10.103.0.14) work with mysql_connect.

So apparently the problem comes from the PDO connection.

Does somebody knows where this could come from, or/and any way to fix it ?

Some server datas :

The PHP Version : 5.2.10 You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5 No command line possible. (i know it should be the tech suport's job, but they're reaaaaaly slow)

Thanks

Previous question :

How to find the mysql.sock on a shared host (tricky way needed...)

So today's problem is : The PDO connection doesn't work on a shared host, and it's supposed to (it's installed on the server). Just a basic PDO connection :

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
    echo 'Connected to database';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

throws this message :

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

A regular mysql connection :

mysql_connect("localhost", "THE_USER", "THE_PWD") or die(mysql_error()); 
mysql_select_db("24DLJLRR1") or die(mysql_error());;
echo 'Connected to database <br/>';

works fine.

So apparently it cannot find the .sock. I think specifying the correct address should work, i tried some "classic" mysql path that I found on internet, without success. The phpinfo says it is at this adress (/var/lib/mysql/mysql.sock) (The PHP Version is 5.2.10) You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5

So i am trying to figure out where the hell it is !!! I tried to look in the phpMyAdmin interface, but i couldn't find the info, plus it seems that phpMyAdmin connects to a different server (it has a different IP adress, and trying to connect to it with php gives a "Wrong password" error). The mysql_connect also connects to this adress, i think it redirects to a different server with some internal password/login.

Well if you have any idea of how to obtain this info (the provider's technical support is "fixing the problem"... it's been 1 month...). Also maybe the problem comes from somewhere else, but the same stuff works on other shared hosts...

The need of PDO is because I use the Symfony framework with Doctrine for this website, and the Doctrine plugin needs PDO... I don't want to redo the website from scratch !

Thanks for your help !

+1  A: 

Can you try 127.0.0.1 as the server name instead of localhost?

IIRC, with some mySQL drivers / adapters, this decides whether the socket is used for establishing the connection or not.

Pekka
I get a different error :SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)
Julien
*Is* there a mySQL server running on localhost? Do you know for sure?
Pekka
Ah, sorry, you say above that there is.
Pekka
Well it's a bit confusing, because mysql_get_host_info() function gives me an ip address (10.103.0.14), so not 10.0.0.127.If i try this IP address from the PDO i get :Access denied for user 'USER_NAME'@'10.103.0.14' (using password: YES)
Julien
I was already beginning to have the feeling that your provider is mapping `localhost` to a different IP (not sure yet, though). Can you try my comment from symcbean's answer?
Pekka
+2  A: 

Using the connection which works, run the query:

show variables like 'socket';

(this behaves just like a select statement)...and you'll get the path of the running socket.

Then check the file permissions.

symcbean
+1 the socket path quoted above does definitely not exist (Error 2 = No such file or directory). This sounds like the best way to go.
Pekka
Thank's a lot ! The problem now is I get : Array ( [0] => socket [1] => /var/lib/mysql/mysql.sock ), so the socket IS at the right place...I think i'm going to kill myself.How can i check the file permission ? (which i think should have, if it works with mysql_connect)Maybe it's just a problem with their PDO installation...
Julien
I think the `mysql_connect` connection is not using the socket, which is why it works. Just for laughs, what happens if you replace `localhost` by `127.0.0.1` in the working mysql connection?
Pekka
yeah, it works with 127.0.0.1, plus the socket seems to be at his place...
Julien
This is *strange*. I think PDO uses different client libraries than classic mysql_connect() but there is something different wrong here... I have no idea what this could be.
Pekka
Yeah, i feel more and more confused, i changed the topic because the socket seems at the right place... hope someone will know.I didn't want to create a new question, because lots of elements are in the comments, but if nobody answers i'll mark this one as the correct answer (cause it made me find out the socket was at the right place...)
Julien
Yup. You might really have to change providers or to increase pressure on the support staff :(
Pekka
To find out the permissions, depending on how the server is configured you could just `ls -l $socket` or print_r(stat($socket)) (and decode the numbers)
symcbean
+2  A: 

try:

exec('`which mysql_config` --socket');

this should show you the configured socket.

Rufinus
If he can run external programs, good idea. It also may be an idea to `find` for any mysql.sock files.
Pekka
Well seems i can'tWarning: exec() has been disabled for security reasonsThanks thought :)
Julien
A: 

I have tried a couple of toturials about PDO and it did not work, all this time I thought I was doing something wrong, I had not idea that this could be problem with connection, I am having second thought about PHP. I might switch to ASP.NET

Kie
actually this is a problem on the dedicated server. They say PDO is not supported.
Julien