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
For your second error, with reference to these two answers.
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.
就我而言,不知何故数据库连接凭据已更改。
I had this issue with a Laravel App in Vagrant. I cleared the config cache and then it worked fine.
php artisan config:cache
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.