简体   繁体   English

来自 Java JDBC 的 Azure VM 的 Azure SQL 数据库连接问题

[英]Azure SQL database connection issue from an Azure VM by Java JDBC

We have a java project in an Azure virtual machine (VM), and need connect to Azure SQL db by JDBC connection, so we use the JDBC connection string provided by Azure SQL db as follows:我们在Azure虚拟机(VM)中有一个java项目,需要通过JDBC连接到Azure SQL db,所以我们使用Azure SQL db提供的JDBC连接字符串如下:

"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"

but we got an exception:但我们有一个例外:

"java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.
 The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net."

then we updated the JDBC connection string to:然后我们将 JDBC 连接字符串更新为:

"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cr2.eastus1-a.control.database.windows.net;loginTimeout=30;"

but we got another exception:但我们有另一个例外:

org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Cannot open server "ZZZ1" requested by the login.  The login failed.)

where "ZZZ1" is our username for the Azure VM.其中“ZZZ1”是 Azure VM 的用户名。

Any idea to solve this issue?有什么想法可以解决这个问题吗?

By default, Azure VM has not blocked the outbound connection to the Internet.默认情况下,Azure VM 未阻止到 Internet 的出站连接。 You could first verify the network connectivity from your VM to the Azure SQL database via running the command telnet ZZZdbserver.database.windows.net 1433 in CMD or Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433 in Powershell.您可以首先通过在 CMD 中运行命令telnet ZZZdbserver.database.windows.net 1433或在 Powershell 中运行Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433命令来验证从 VM 到 Azure SQL 数据库的网络连接。 If it fails, you may check if there is any firewall blocking this database connection or port or your application is listening on that port or your SQL database is online.如果失败,您可以检查是否有任何防火墙阻止了此数据库连接或端口,或者您的应用程序是否正在侦听该端口,或者您的 SQL 数据库是否在线。

Additionally, if the network connectivity has succeeded, you could verify if the username or password is correct when you are using to connect to your database.此外,如果网络连接成功,您可以在用于连接数据库时验证用户名或密码是否正确。 Make sure there is not any typo.确保没有任何错字。 Also, the username should have enough privileges to access this database.此外,用户名应该有足够的权限来访问这个数据库。 You can try to access this database via Server admin login in the properties of the SQL database.您可以尝试通过 SQL 数据库属性中的Server admin login来访问该数据库。 Moreover, you could refer to this sample code to use Java to connect to access the Azure SQL database.此外,您可以参考此示例代码使用Java 连接访问Azure SQL 数据库。

When using the Microsoft JDBC Driver for SQL Server to connect to an Azure SQL Database .使用 Microsoft JDBC Driver for SQL Server 连接到 Azure SQL 数据库时 You should note this:你应该注意这一点:

  • Appending the server name to the userId in the connection string将服务器名称附加到连接字符串中的 userId

Prior to the 4.0 version of the Microsoft JDBC Driver for SQL Server, when connecting to an Azure SQL Database, you were required to append the server name to the UserId in the connection string.在 Microsoft JDBC Driver for SQL Server 4.0 版本之前,连接到 Azure SQL 数据库时,需要将服务器名称附加到连接字符串中的 UserId。 For example, user@servername.例如,用户@服务器名。 Beginning in version 4.0 of the Microsoft JDBC Driver for SQL Server, it's no longer necessary to append @servername to the UserId in the connection string.从 Microsoft JDBC Driver for SQL Server 4.0 版开始,不再需要将 @servername 附加到连接字符串中的 UserId。

  • Using encryption requires setting hostNameInCertificate使用加密需要设置hostNameInCertificate

Prior to the 7.2 version of the Microsoft JDBC Driver for SQL Server, when connecting to an Azure SQL Database, you should specify hostNameInCertificate if you specify encrypt=true (If the server name in the connection string is shortName.domainName, set the hostNameInCertificate property to *.domainName.).在 Microsoft JDBC Driver for SQL Server 7.2 版本之前,连接 Azure SQL 数据库时,如果指定 encrypt=true,则应指定hostNameInCertificate (如果连接字符串中的服务器名称为 shortName.domainName,则设置hostNameInCertificate属性到 *.domainName。)。 This property is optional as of version 7.2 of the driver.从驱动程序的 7.2 版开始,此属性是可选的。

Hope this helps.希望这可以帮助。

If I need set encrypt=true, and hostNameInCertificate=cr2.eastus1-a.control.database.windows.net.如果我需要设置 encrypt=true 和 hostNameInCertificate=cr2.eastus1-a.control.database.windows.net。 Where do I need get the certificate for cr2.eastus1-a.control.database.windows.net from Azure SQL DB's service?我需要在哪里从 Azure SQL DB 的服务中获取 cr2.eastus1-a.control.database.windows.net 的证书?

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

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