简体   繁体   中英

Azure - permission denied when trying to connect to external MySQL database

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;
}

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.

在此处输入图片说明

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.

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

图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM