首页 > 解决方案 > Azure Runbook to connect to SQL server running on Azure VM

问题描述

I am trying to connect to a SQL server running on an Azure VM using the below sample code (having defined the SQL user name and password):

$SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset 

# Get the username and password from the SQL Credential 
$SqlUsername = $SqlCredential.UserName 
$SqlPass = $SqlCredential.GetNetworkCredential().Password 

# Define the connection to the SQL Database 
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;")

this code works when I am connecting to Azure SQL Database , but not when connecting to SQL running off Azure VM.

the error thrown is:

Error occured: Exception calling "Open" with "0" argument(s): 
"A network-related or instance-specific error occurred while establishing a 
connection to SQL Server. The server was not found or was not accessible.
 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 
(provider: TCP Provider, error: 0 - No such host is known.)"

I have verified on the VM SQL remote connections are enabled and including TCP port 1433 defined.

标签: azureazure-automationazure-runbook

解决方案


应该是网络相关的问题。如果您已在 VM 上验证了 SQL 远程连接已启用并包括定义的 TCP 端口 1433。此外,1433 端口在连接到 VM 的 NSG 的入站端口规则和 VM 内的 windows 防火墙中打开。您可以在本地使用telnet验证此端口。

此外,您可以$SqlServer= "sqlvmlabel.eastus.cloudapp.azure.com"指定哪个是 Azure VM 的公共 DNS 名称。下面是使用 SQL 身份验证的连接字符串示例。

Server=sqlvmlabel.eastus.cloudapp.azure.com;Integrated Security=false;User ID=<login_name>;Password=<your_password>

阅读更多信息:连接到 Azure 上的 SQL Server 虚拟机


推荐阅读