Azure-尝试连接到外部MySQL数据库时权限被拒绝 [英] Azure - permission denied when trying to connect to external MySQL database

查看:101
本文介绍了Azure-尝试连接到外部MySQL数据库时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Azure上发布的Web应用程序与外部MySQL数据库进行连接.代码在Localhost上托管的Tomcat上运行良好,但是在Azure上所有需要连接的函数均返回错误:

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    

连接功能代码为:

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.

在这里,我试图创建一个Java spring-boot 项目来测试与 Azure MySQL数据库的连接.

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

我的web.config文件:

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

1.Don't miss set SSL parameters.

2.请为您的azure网络应用设置白色的 IP地址.

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

3.不要错过 web.config 中的 -Djava.net.preferIPv4Stack = true 设置.

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

您可以从此线程中找到更多详细信息:JavaMail到iMail的API-java.net.SocketException:权限被拒绝:connect

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

希望它对您有帮助.

这篇关于Azure-尝试连接到外部MySQL数据库时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆