Powershell Invoke-Sqlcmd 命中错误的数据库 - 系统,为什么?
Powershell Invoke-Sqlcmd hits wrong database - system, why?
我正在通过 Terraform 部署 MSsql 服务器、防火墙、数据库和 sql 登录(以及更多)。
服务器、防火墙和数据库已正确部署,但通过 powershell 登录失败并出现以下错误:
': exit status 1. Output: Invoke-Sqlcmd : Database 'System' does not
exist. Make sure that the name is entered correctly.
我的 terraform 代码如下所示:
resource "azurerm_mssql_server" "itan-mssql-server" {
administrator_login = "itanadmin"
administrator_login_password = random_password.itan-mssql-admin-password.result
location = var.location
name = "itan-mssql-server"
resource_group_name = var.resource-group-name
version = "12.0"
depends_on = [
azurerm_resource_group.itan-west-europe-resource-group]
}
resource "azurerm_sql_firewall_rule" "itan-mssql-server-firewall-rule" {
start_ip_address = "${chomp(data.http.myip.body)}"
end_ip_address = "${chomp(data.http.myip.body)}"
name = "itan-tf-client-executer-ip"
resource_group_name = var.resource-group-name
server_name = azurerm_mssql_server.itan-mssql-server.name
depends_on = [azurerm_mssql_server.itan-mssql-server]
}
resource "azurerm_mssql_database" "itan-mssql-database" {
name = "itan"
server_id = azurerm_mssql_server.itan-mssql-server.id
sku_name = "basic"
max_size_gb = "2"
depends_on = [
azurerm_mssql_server.itan-mssql-server]
}
resource "null_resource" "itan-mssql-logins" {
provisioner "local-exec" {
command =<<EOT
$server = Get-AzSqlServer
$createReaderLogin = @{
'ServerInstance' = "${azurerm_mssql_server.itan-mssql-server.fully_qualified_domain_name}"
'Database' = "itan"
'Username' = "${azurerm_mssql_server.itan-mssql-server.administrator_login}"
'Password' = "${random_password.itan-mssql-admin-password.result}"
'Query' = "SELECT * FROM Students";
}
Invoke-Sqlcmd $createReaderLogin
EOT
interpreter = ["PowerShell", "-Command"]
}
depends_on = [azurerm_sql_firewall_rule.itan-mssql-server-firewall-rule]
}
最后一部分,null_resource
被解析成这样:
$server = Get-AzSqlServer
$createReaderLogin = @{
'ServerInstance' = "server.database.windows.net"
'Database' = "database"
'Username' = "admin"
'Password' = "***********"
'Query' = "SELECT * FROM Students";
}
- 已经销毁,密码不用担心
- 我知道我需要在主数据库上创建登录,在正确的数据库上创建下一个用户,这个 select * 只是查询失败的一个例子
我已经测试过,在创建服务器、防火墙、数据库之后,我能够将 azure mssql 与 mssql 连接起来。然而 运行 Invoke-SqlCmd
失败并显示 “数据库 'System' 不存在”
为什么?已经为此苦苦挣扎了 2 天。
基本上,我正在尝试创建具有专用 sql 权限的 logins/users (db_read/db_write)。
我用单行而不是多行解决了这个问题:
null_resource
的新代码
resource "null_resource" "itan-mssql-logins" {
provisioner "local-exec" {
command = "Invoke-Sqlcmd -ServerInstance ${azurerm_mssql_server.itan-mssql-server.fully_qualified_domain_name} -Database master -Username itanadmin -Password '${random_password.itan-mssql-admin-password.result}' -Query \"CREATE LOGIN [itanreader] WITH PASSWORD=N'${random_password.itan-mssql-reader-password.result}'\""
interpreter = ["PowerShell", "-Command"]
}
depends_on = [azurerm_sql_firewall_rule.itan-mssql-server-firewall-rule, azurerm_mssql_database.itan-mssql-database]
}
所有参数看起来都一样,但这个有效。不知道为什么。
我正在通过 Terraform 部署 MSsql 服务器、防火墙、数据库和 sql 登录(以及更多)。
服务器、防火墙和数据库已正确部署,但通过 powershell 登录失败并出现以下错误:
': exit status 1. Output: Invoke-Sqlcmd : Database 'System' does not exist. Make sure that the name is entered correctly.
我的 terraform 代码如下所示:
resource "azurerm_mssql_server" "itan-mssql-server" {
administrator_login = "itanadmin"
administrator_login_password = random_password.itan-mssql-admin-password.result
location = var.location
name = "itan-mssql-server"
resource_group_name = var.resource-group-name
version = "12.0"
depends_on = [
azurerm_resource_group.itan-west-europe-resource-group]
}
resource "azurerm_sql_firewall_rule" "itan-mssql-server-firewall-rule" {
start_ip_address = "${chomp(data.http.myip.body)}"
end_ip_address = "${chomp(data.http.myip.body)}"
name = "itan-tf-client-executer-ip"
resource_group_name = var.resource-group-name
server_name = azurerm_mssql_server.itan-mssql-server.name
depends_on = [azurerm_mssql_server.itan-mssql-server]
}
resource "azurerm_mssql_database" "itan-mssql-database" {
name = "itan"
server_id = azurerm_mssql_server.itan-mssql-server.id
sku_name = "basic"
max_size_gb = "2"
depends_on = [
azurerm_mssql_server.itan-mssql-server]
}
resource "null_resource" "itan-mssql-logins" {
provisioner "local-exec" {
command =<<EOT
$server = Get-AzSqlServer
$createReaderLogin = @{
'ServerInstance' = "${azurerm_mssql_server.itan-mssql-server.fully_qualified_domain_name}"
'Database' = "itan"
'Username' = "${azurerm_mssql_server.itan-mssql-server.administrator_login}"
'Password' = "${random_password.itan-mssql-admin-password.result}"
'Query' = "SELECT * FROM Students";
}
Invoke-Sqlcmd $createReaderLogin
EOT
interpreter = ["PowerShell", "-Command"]
}
depends_on = [azurerm_sql_firewall_rule.itan-mssql-server-firewall-rule]
}
最后一部分,null_resource
被解析成这样:
$server = Get-AzSqlServer
$createReaderLogin = @{
'ServerInstance' = "server.database.windows.net"
'Database' = "database"
'Username' = "admin"
'Password' = "***********"
'Query' = "SELECT * FROM Students";
}
- 已经销毁,密码不用担心
- 我知道我需要在主数据库上创建登录,在正确的数据库上创建下一个用户,这个 select * 只是查询失败的一个例子
我已经测试过,在创建服务器、防火墙、数据库之后,我能够将 azure mssql 与 mssql 连接起来。然而 运行 Invoke-SqlCmd
失败并显示 “数据库 'System' 不存在”
为什么?已经为此苦苦挣扎了 2 天。
基本上,我正在尝试创建具有专用 sql 权限的 logins/users (db_read/db_write)。
我用单行而不是多行解决了这个问题:
null_resource
resource "null_resource" "itan-mssql-logins" {
provisioner "local-exec" {
command = "Invoke-Sqlcmd -ServerInstance ${azurerm_mssql_server.itan-mssql-server.fully_qualified_domain_name} -Database master -Username itanadmin -Password '${random_password.itan-mssql-admin-password.result}' -Query \"CREATE LOGIN [itanreader] WITH PASSWORD=N'${random_password.itan-mssql-reader-password.result}'\""
interpreter = ["PowerShell", "-Command"]
}
depends_on = [azurerm_sql_firewall_rule.itan-mssql-server-firewall-rule, azurerm_mssql_database.itan-mssql-database]
}
所有参数看起来都一样,但这个有效。不知道为什么。