Mysql – Cannot connect to thesql database remotely using jdbc

connectiondatabasejdbcMySQL

I am trying to connect to my database which is hosted on 000webhost. I am connecting from SmartFoxServer, and they allow database connections. All you have to do is change some of the settings in the smartfoxserver xml file.

I have successfully managed to connect to my localhost mysql database using these settings.

 <databaseManager active="true">
    <connectionString>jdbc:mysql://localhost:3306/db_name</connectionString>
    <userName>root</userName>
    <password></password>
    <testSql>SELECT id FROM users LIMIT 1</testSql>
  </databaseManager>

But when I tried to connect to my external database which is hosted on 000webhost and looks like this:

enter image description here

Using these settings

<connectionString>jdbc:mysql://mysql2.000webhost.com/a5939459_data</connectionString>
<userName>a5939459_user</userName>
<password>censored</password>
<testSql>SELECT id FROM users LIMIT 1</testSql>

I get the following error…

Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
Message: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driv
er has not received any packets from the server.
Description: The DBManager Test SQL failed
Please double check your SQL code and make sure that Database server is running.

EDIT:

But when I run php files on my localhost and ask for database connection like this, it works

<?php
$db_username = "a5939459_user";
$db_name = "a5939459_data";
$db_password = "censored";
$db_host = "mysql2.000webhost.com";
mysql_connect($db_host,$db_username, $db_password, $db_name);
mysql_select_db($db_name) or die (mysql_error());
?>

Best Answer

It is a big security issue to open up a database/mysql port for external/Internet connections. So when they created your database, they will most likely assign a user to connect only from limited source IPs, mostly Internal IPs. You need to communicate with their support for information from which source IPs you are allowed to connect. As John mentioned, the mysql2.000webhost.com resolves to a private IP address, and that implies you are most likely allowed to connect to it from some other private IP, which you might connect to through VPN or other means.

mysql2.000webhost.com.  14189   IN      A       10.1.1.102