Azure Runbook连接到在Azure VM上运行的SQL Server

时间:2019-01-17 07:13:32

标签: azure azure-automation azure-runbook

我正在尝试使用以下示例代码(已定义SQL用户名和密码)连接到在Azure VM上运行的SQL Server:

$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;")

此代码在我连接到Azure SQL数据库时有效,但是在连接到运行Azure VM的SQL时无效。

抛出的错误是:

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.)"

我已经在VM上验证了SQL远程连接已启用,并且包括已定义的TCP端口1433。

1 个答案:

答案 0 :(得分:0)

可能是与网络相关的问题。如果您已在VM上进行验证,则SQL远程连接已启用,并且已定义TCP端口1433。此外,在连接到VM的NSG的入站端口规则和VM内部的Windows防火墙中,打开了端口1433。您可以使用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>

阅读更多信息:connect to a SQL Server Virtual Machine on Azure