drxkx6149 2017-04-14 07:24
浏览 607

SQLSTATE [HY000] [2002]操作超时[重复]

This question already has an answer here:

I am creating a pdo data connection. The connections works fine on localhost. But when I connect to my remote db, and use exactly the same credentials which I have used before, I get a empty page, no error messages, just a empty page.

The same credentials that are working on a previous project were done with mysqli, this one is done with pdo.

I have tried two types of pdo connection code, none of them are working.

First one:

$databaseHost = 'x.xxx.xxx.xx';
$databaseName = 'xxxxxxxxx';
$databaseUsername = 'xxxxxxx';
$databasePassword = 'xxxxxx';
$charset = 'utf8';

try {

    $dsn = "mysql:host=$databaseHost;dbname=$databaseName;charset=$charset";


} catch(PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$opt = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
        ];

$pdo = new PDO($dsn, $databaseUsername, $databasePassword, $opt);

Second one:

$databaseHost = 'x.xxx.xxx.xx';
$databaseName = 'xxxxxxxxx';
$databaseUsername = 'xxxxxxx';
$databasePassword = 'xxxxxx';

$opt = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
        ];

try {

    $pdo = new PDO("mysql:host={$databaseHost};dbname={$databaseName}", $databaseUsername, $databasePassword, $opt);

    } catch(PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
  }

Even though both connections work in localhost, I tried to just load this connection page locally in the browser, the fist option gave me this error:

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Operation timed out in /Applications/MAMP/htdocs/ijdb_pdo/config.php:24 Stack trace: #0 /Applications/MAMP/htdocs/ijdb_pdo/config.php(24): PDO->__construct('mysql:host=83.1...', 'u1164707_sohail', 'sohail123', Array) #1 {main} thrown in /Applications/MAMP/htdocs/ijdb_pdo/config.php on line 24

and the second option gave me the following error:

againtestConnection failed: SQLSTATE[HY000] [2002] Operation timed out

What am i messing up in my code?

I tried then to connect through the terminal with:

MacBook-Pro-3:/ sohail$ /Applications/MAMP/Library/bin/mysql -h "xx.xxx.xxx.xx" -u "xxxxxx" "-pxxxxxxx" "xxxxxxx";

and got the following error:

Warning: Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can't connect to MySQL server on 'xx.xxx.xxx.xx' (60) MacBook-Pro-3:/ sohail$

I don't think I have terminal access on my remote machine, so I can't do SHOW GLOBAL VARIABLES LIKE 'PORT'; cmd -thanks

</div>
  • 写回答

1条回答 默认 最新

  • doubi2228 2017-04-14 07:38
    关注

    For your second error, with reference to these two answer.

    PDOException SQLSTATE[HY000] [2002] Connection timed out on my local computer

    'PDOExcpetion' with message 'SQLSTATE[HY000] [2002] No route to host

    Either you need to enable the access to remote database or you have network IP issue which changes time to time. Please verify both things. I hope this will help you.

    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件