Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

php - Confusing PDO-only problem : Can't connect through socket/Access denied/Can't connect to server (shared host)

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This was already marked as answered, but not really solved (without changing databases). So, just in case someone like me also experiences this problem...

The easiest way to fix this is to first get the socket path (either by looking in the php.ini file or by using: phpmyadmin or the console (or construct it in mysql or mysqli)

...to run the following query (anything but PDO):

show variables like 'socket';       //as mentioned by symcbean

THEN, in the PDO connection string, change it to use the socket instead of a hostname:

$dbc = new PDO("mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=$DBName", $User, $Password, array(PDO::ATTR_PERSISTENT => true)); // using persistent connections

This worked for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...