简体   繁体   English

Azure-尝试连接到外部MySQL数据库时权限被拒绝

[英]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. 我正在尝试使用Azure上发布的Web应用程序连接外部MySQL数据库。 Code is working perfectly fine on Tomcat hosted on Localhost, but on Azure all functions requiring connection return with error: 代码在Localhost上托管的Tomcat上运行良好,但在Azure上所有需要连接的函数均返回错误:

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 . 在这里,我试图创建一个java spring-boot项目来测试与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: 我的web.config文件:

<?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. 1.不要错过设置的SSL参数。

2.Please set white IP address of your azure web app. 2.请设置您的Azure Web应用程序的白色IP address

在此处输入图片说明

3.Don't miss -Djava.net.preferIPv4Stack=true setting in your web.config . 3.不要错过web.config -Djava.net.preferIPv4Stack=true设置。

You could find more details from this thread: JavaMail API to iMail -- java.net.SocketException: Permission denied: connect 您可以从以下线程中找到更多详细信息: JavaMail API到iMail-java.net.SocketException:权限被拒绝:connect

Hope it helps you. 希望对您有帮助。

Another solution is to add this -Djava.net.preferIPv4Stack=true to your pom.xml file. 另一个解决方案是将此-Djava.net.preferIPv4Stack = true添加到pom.xml文件中。

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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