azure - 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.
解决方案
应该是网络相关的问题。如果您已在 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 虚拟机
推荐阅读
- html - 如何创建一个检查输入但不提交数据的 HTML 按钮?
- javascript - 看不到所有 javascript 日志消息
- apache - 在 Apache 中安装 SSL 时出错
- amazon-web-services - 在 user_data 提供者模板文件中访问 Terraform 变量
- css - Bootstrap 4轮播链接覆盖所有其他链接
- loops - Netlogo 中的刻度循环
- r - LDA 中的主题建模之后的下一步是什么
- c# - Websocket 服务器发送自发消息(没有来自客户端的请求)
- oracle - 启用闪回会减慢 oracle 中的更新/插入速度吗?
- json - vue.js - 发送所选对象的另一个字段