catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

SQLSTATE[HY000] [2002] Connection timed out #556

Closed KongYian closed 7 years ago

KongYian commented 7 years ago

yeasterday,l connected my mysql server.This problem showed. After my debug,l confirm that this problem is caused by a wrong $dsn . u can see this blog http://blog.csdn.net/gusgao/article/details/52435812 . add a space before host and exchange params named host & dbname . l modified source code at column 213 $dsn = $driver . ': ' . implode($stack, ';'); and add $attr = array_reverse($attr,true); at column 200 in Medoo.php. At last , l figured out it. u can check it. Thx.

catfan commented 7 years ago

I tested this issue. The reason is because you are using wrong configuration for the server option.

For example:

// This works fine
mysql:host=localhost;dbname=foo;

// Invalid host name, throw error
mysql:host=localhostttt;dbname=foo;

// Invalid host name also, but connect successfully
mysql: host=localhostttt;dbname=foo;
mysql:xhost=localhost;dbname=foo;

Adding whitespace before host is just making the host value as invalid, and the PDO engine will connect the localhost by default.

So, check out the server option is correct or not again.