I have a java application, that can connect to an online MySQL database, like this:
private String hostName = "db4free.net:3306";
private String dbName = "mydbname";
private String username = "name";
private String password = "pw";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + hostName + "/" + dbName, username, password);
System.out.println("Connection established!");
} catch (Exception ex) {
ex.printStackTrace();
}
It works perfectly. However, I try the same with PHP (ignore hostname difference, this should be it):
$host = "85.10.205.173";
$dbname = "dbname";
$username = "name";
$password = "pw";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
// Open connection with db
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
// Just an error message, if connection fails
die("Failed to connect to the database: " . $ex->getMessage());
}
And after a while, I get this: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '85.10.205.173' (4)
So why do the two work differently? I think that remote access is enabled (because I could connect with java).
Disable the DNS hostname lookups in mysql
To disable DNS host name lookups, start the server with the --skip-name-resolve option. In this case, the server uses only IP addresses and not host names to match connecting hosts to rows in the MySQL grant tables. Only accounts specified in those tables using IP addresses can be used.
Don't forget to restart MySQL
to take effect.
Not sure this will solve your problem.