2

I am developing a simple JAVA Time triggered Azure Function.

I am trying to connect to a Azure MySQL instance but I keep getting this error:

[Information] com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Here is the source code:

String url="jdbc:mysql://XXXX.mysql.database.azure.com:3306/DBNAME?useSSL=true&requireSSL=false";
connect = DriverManager.getConnection(url, "user@DBNAME", "passwd"); 
statement = connect.createStatement(); 
resultSet statement.executeQuery("select * from table1");

I am using the following library:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>

Notes:

  • The program works fine on my local machine (i.e., after adding my IP address to the MySQL firewall, I can reach the DB from my machine);

  • I've already tried different mysql libraries: nothing changes;

  • Another Azure function implemented with NodeJS is able to reach the DB;

  • I tried to add the Function "ADDITIONAL OUTBOUND IP ADDRESSES" to the MySQL firewall: nothing changes.

Any ideas?

Thank you in advance.

Hulk
  • 6,399
  • 1
  • 30
  • 52
steccami
  • 21
  • 1
  • 4
  • did you try a different version of the same connector package? Looks like it has an history of such connectivity issues with azure mysql https://stackoverflow.com/questions/44620692/unable-to-connect-to-azure-mysql-database-through-azure-function-c-sharp https://stackoverflow.com/questions/53095542/connecting-mysql-azure-with-java – Aravind Nov 12 '18 at 10:13
  • Thank you. I've already tried different package versions but the error is still there. The point is that the same JAVA code works fine on my local machine (i.e., I can connect to the remote Azure DB). It seems something related to specific Azure/networking configurations. – steccami Nov 12 '18 at 11:35
  • Update: I developed a simple C# Azure function, it works well. It seems to be a problem strictly related to the JAVA-based Azure Function env. – steccami Nov 12 '18 at 16:25
  • Did you find the solution for this problem ? – Kumar Feb 05 '19 at 22:40
  • I contacted MS support but unfortunately they did not manage to provide any solution. They told me that JAVA is still in preview so they closed the ticket. This problem is very weird. I tried many different configurations/jdbc libs but nothing changed. The weird thing is that I can reach the Azure MySQL DB when running the same code on my local machine. – steccami Feb 06 '19 at 13:58
  • I am facing the exact same problem. – Kumar Feb 06 '19 at 14:05
  • I am using 8.0.14 version of mysql connector – Kumar Feb 06 '19 at 14:07
  • Check the answer and let me know if it works for you. I am curious – Kumar Feb 06 '19 at 16:13

1 Answers1

0

5.1.31 MySQL-connector-java version Worked for me.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.31</version>
</dependency>

Connecting MySQL Azure with Java

Kumar
  • 182
  • 13
  • It does not work for me :( I tried to re-create the App but nothing changes. My DB has SSL status: ENABLED. Maybe something related to the SSL connection? – steccami Feb 07 '19 at 08:46
  • My Url : jdbc:mysql://xxxxxxx:3306/{mydb}?serverTimezone={zone}&trustServerCertificate=true&encrypt=true&hostNameInCertificate=*.database.windows.net – Kumar Feb 07 '19 at 14:25
  • Thank you for sharing your connection string. Here is what I did: I tried your suggestion and obtained the following error `jdbc:mysql://xxx.mysql.database.azure.com:3306/xxx?serverTimezone=UTC&trustServerCertificate=true&encrypt=true&hostNameInCertificate=*.database.windows.net Error: java.sql.SQLException: SSL connection is required. Please specify SSL options and retry.`. – steccami Feb 08 '19 at 08:08
  • The previous problem was probably due to the SSL setup so I changed your string in this way: `jdbc:mysql://xxx.mysql.database.azure.com:3306/xxx?useSSL=true&serverTimezone=UTC&trustServerCertificate=true&encrypt=true&hostNameInCertificate=*.database.windows.net Error: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure` – steccami Feb 08 '19 at 08:08
  • Is it possible for you to share the java project? Of course I am not asking you for the entire code ;-) but a "simplified" version of it where I can see the maven setup anche code used for the DB connection? Many thanks. – steccami Mar 01 '19 at 07:33