首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过PowerShell连接到Azure SQL DB会导致现有连接被远程主机强制关闭

问通过PowerShell连接到Azure SQL DB会导致现有连接被远程主机强制关闭
EN

Stack Overflow用户
提问于 2021-09-01 15:34:44
回答 1查看 283关注 0票数 0

通过Powershell连接到Azure (PaaS)

代码语言:javascript
复制
$sqlServer = "x.database.windows.net"
$sqlDB = "master"
$conn = new-object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server=tcp:$($sqlServer),1433;Initial Catalog=$($sqlDB);Persist Security Info=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" 
$conn.AccessToken = $env:ACCESSTOKEN
$conn.Open()

然后,我将看到一个错误:

代码语言:javascript
复制
Exception calling "Open" with "0" argument(s): "A connection was successfully established with the server, but then an 
error occurred during the login process. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed 
by the remote host.)"
At C:\Users\x\testcon2.ps1:6 char:1
+ $conn.Open()
+ ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException
  • 我能够直接通过SSMS连接到服务器,没有任何问题。
  • 防火墙规则已经到位。
  • 最低TLS 1.2
  • 资源利用率为零
  • 除了我的数据库,没有其他连接。
  • 我查看了side中的RingBuffer和eventlog,没有看到任何错误。

有人知道我怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2021-09-07 12:25:47

我们需要通过运行命令来安装模块。

尝试通过运行Azure验证

连接-AzAccount

我们可以通过运行下面的命令找到数据库,如果返回要连接的数据库,则可以运行脚本。

获取-AzSqlServer -ResourceGroupName .

下面的PS脚本将建立连接并执行查询,并给出输出。

代码语言:javascript
复制
Import-Module Az.Sql -Force
 
$rgName = '<Resource Group Name>'
$sqlServer = Get-AzSqlServer -ResourceGroupName $rgName
$sqlDatabase = Get-AzSqlDatabase -ServerName $sqlServer.ServerName -ResourceGroupName $rgName
 
$Params = @{
   'ServerInstance' = $sqlServer.FullyQualifiedDomainName;
   'Database' = $sqlDatabase.DatabaseName[0];
   'Username' = $sqlServer.SqlAdministratorLogin;
   'Password' = ‘<Password>’;
   'Query' = 'SQL Statement to access table'
}
 
Invoke-Sqlcmd @Params

还请参阅微软文章有关使用Powershell的Azure SQL数据库身份验证。

此连接问题通常发生在在少数地点提供服务中,也是在AccessToken过期时。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69016773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档