【问题标题】:golang SQL Server query via go-mssqldb通过 go-mssqldb 查询 golang SQL Server
【发布时间】:2017-03-13 06:06:52
【问题描述】:

我正在尝试使用 go 查询 SQL Server 2008 R2

https://github.com/denisenkom/go-mssqldb

SQL Server 2008 R2 实例位于 Windows Server 2008 R2 下的 VM 上;我正在 Win 7 VMWare 主机下进行开发并从那里运行程序以查询 VM 上的数据库。数据库已启动并运行托管在服务器 VM 上的应用程序。代码如下。

我得到的错误是:

[编辑 2017-03-14:我指定端口时出现新错误]

登录错误:读取 tcp 192.168.91.1:15222->192.168.91.135:1433: wsarecv: 现有连接被远程主机强行关闭。

指定 SQL Server 端口 (1433) 时返回此错误。包含或不包含实例都不会改变它。

SQL Server 配置为允许远程连接、SQL Server 身份验证、连接未加密、启用 TCP/IP、IPALL 端口=1433。防火墙在 80、443、1433、1434 上为 TCP 开放; UDP 在 1433、1434 上。在将 db 实例添加到连接字符串之前,我遇到了另一个错误。

SQL 服务器记录 似乎 表明机器正在通话。 IP 地址用于 VMWare 主机和 VM。 SQL Server Browser 服务正在运行(记为“本地服务”)。 SQL Server 代理运行。我试过使用 ODBC 和 ADO 连接字符串,似乎得到了同样的错误。任何帮助将不胜感激。

package main
import (
   // Import go-mssqldb strictly for side-effects
   _ "github.com/denisenkom/go-mssqldb"
   "database/sql"
   "log"
)

func main() {
   var n_tables int

   println (sql.Drivers())

   // URL connection string formats
   //    sqlserver://sa:mypass@localhost?database=master&connection+timeout=30         // username=sa, password=mypass.
   //    sqlserver://sa:my%7Bpass@somehost?connection+timeout=30                       // password is "my{pass"
   // note: pwd is "myP@55w0rd"
   connectString := "sqlserver://SBM:myP%4055w0rd@VM17:1433?database=AE&connection+timeout=30"
   println("Connection string=" , connectString )

   println("open connection")
   db, err := sql.Open("mssql", connectString)
   defer db.Close()
   println ("Open Error:" , err)
   if err != nil {
      log.Fatal(err)
   }

   println("count records in TS_TABLES & scan")
   err = db.QueryRow("Select count(*) from ts_tables").Scan(&n_tables)
   if err != nil {
      log.Fatal(err)
   }
   println ("count of tables" , n_tables)

   println("closing connection")
   db.Close()
}

输出:

[2/2]0xc042002c20
Connection string= sqlserver://VM17_SBM:P%4055word@VM17:1433?database=VM17_SBM_AE_OE_REPO_CL&connection+timeout=30
open connection
Open Error: (0x0,0x0)
count records in TS_TABLES & scan
2017/03/14 19:48:01 Login error: read tcp 192.168.91.1:15222->192.168.91.135:1433: wsarecv: An existing connection was forcibly closed by the remote host.
exit status 1

【问题讨论】:

    标签: sql-server go sql-server-2008-r2


    【解决方案1】:

    我在a comment by the library author on Github找到了答案。

    将“encrypt=disable”添加到连接字符串就可以了。我正在下载 SQL Server 2008 R2 x64 as suggested here 的 SP3 更新,并会在我有时间时安装它。至于现在,虽然查询有效。

    【讨论】:

      【解决方案2】:

      github repo 如下所示

      确保 SQL Server Browser windows 服务正在运行,并且没有防火墙阻止 UDP 端口 1434。驱动程序使用此服务来获取 SQL Server 实例的 TCP 端口

      确保您的 SQLbrowser 服务正在您的主机上运行..

      你也可以指定你的连接字符串和端口号(如果你的端口是静态的,这就不需要启动 SQLBrowser 服务)

      【讨论】:

      • 我用额外的服务器配置信息编辑了帖子。 SQL 浏览器正在运行。指定端口似乎无关紧要。我不知道为什么 NMAP 说 1434 被过滤:` PORT STATE SERVICE VERSION 1433/tcp open ms-sql -s Microsoft SQL Server 2008 R2 10.50.1600; RTM 1434/tcp 过滤 ms-sql-m `
      • read udp 192.168.91.1:49155->192.168.91.135:1434为什么连接是udp
      • 你启用tcp了吗,你能从外面ping服务器吗
      • 两个端口都启用了 TCP 和 UDP。 npcap 在我的系统上不起作用,所以我无法进行 nmap UDP 扫描。我不允许从我的笔记本电脑“外部”访问虚拟机。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 2021-01-07
      • 1970-01-01
      相关资源
      最近更新 更多