mercredi 5 août 2015

Errors connecting to MySQL database from alternate domain


I have two domains on two separate servers, foo.com and bar.com.

I have a website and MySQL database setup on foo that I want to migrate to bar, but bar doesn't have MySQL.

As a solution, I'm moving all the files across but leaving the database on foo, and connecting to it remotely.

I'm currently connecting like so:

connect.php

$hostname = 'database.foo.com';
$username = 'username';
$password = 'password';
$dbname   = 'database';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
}

catch(PDOException $e){
    echo($e->getMessage());
}

This works fine on foo.com but when I migrate this file to bar.com I get this error:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'database.foo.com' (110)

I can have the two sites, foo.com and bar.com open in two windows, and one will work while the other doesn't - despite the fact that they are both connecting (or trying to connect) to the same database.

Why is this happening and how can this be rectified?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire