0

I'm trying to connect with external MySQL database with web application posted on Azure. Code is working perfectly fine on Tomcat hosted on Localhost, but on Azure all functions requiring connection return with error:

Exception: java.net.SocketException: Permission denied: connect Message: ; nested exception is: java.net.SocketException: Permission denied: connect    

Connection function code is:

private static String hostName = "sql11.freesqldatabase.com";
private static String dbName = "xxxx";
private static String user = "xxxx";
private static String password = "xxxx";
private static String portNumber = "xxxx";
private Connection connect() {
    String url ="jdbc:mysql://"+hostName+":"+portNumber+"/"+dbName+"?user="+user+"&password="+password;
    Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    return conn;
}
obirek999
  • 3
  • 3
  • Are you using Azure Database for MySQL? – Jay Gong Oct 31 '17 at 09:27
  • I was trying both with Azure Database and Freesqldatabase - both works perfectly fine while being run on localhost tomcat server but when posted on Azure server - it shows that error – obirek999 Nov 01 '17 at 10:12

2 Answers2

0

I tired to reproduce your issue but failed.

Here , I tried to create a java spring-boot project to test connection with Azure MySQL Database.

Snippet of my code:

    private static String hostName = "<your host name>";
    private static String dbName = "sys";
    private static String user = "<user name>";
    private static String password = "password";
    private static String portNumber = "3306";

    @RequestMapping("/hello")
    public String index() throws SQLException {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            String url = "jdbc:mysql://"+hostName+":"+portNumber+"/"+dbName+"?verifyServerCertificate=true&useSSL=true&requireSSL=false&serverTimezone=UTC";
            conn = DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {
            System.out.println("error!!!!");
            System.out.println(e.getMessage());
            e.printStackTrace();
            return e.getMessage();
        }
        return conn.getCatalog();
    }

My web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
        arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\demo-0.0.1-SNAPSHOT.jar&quot;">
    </httpPlatform>
  </system.webServer>
</configuration>

You could check points as below if you can not connect to your database:

1.Don't miss set SSL parameters.

2.Please set white IP address of your azure web app.

enter image description here

3.Don't miss -Djava.net.preferIPv4Stack=true setting in your web.config.

You could find more details from this thread: JavaMail API to iMail -- java.net.SocketException: Permission denied: connect

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • I'm having a problem with doing it with application posted on Azure platform, not with connection to Azure MySQL database.. – obirek999 Nov 01 '17 at 10:11
  • @obirek999 According to your post, it seems that your web app connects to db perfectly, but not working on azure platform. So , i suggested you checking web.config or settings of your web app on azure in my answer. – Jay Gong Nov 02 '17 at 01:09
0

Another solution is to add this -Djava.net.preferIPv4Stack=true to your pom.xml file.

image

Khalid Bin Huda
  • 1,583
  • 17
  • 16