无法使用Go连接到MS SQL Server

huangapple go评论79阅读模式
英文:

Unable to connect to MS SQL Server using Go

问题

我正在尝试使用Go语言和这个SQL驱动程序"github.com/denisenkom/go-mssqldb"连接到SQL Server Express。

目前它调用了open函数,然后在ping命令上停顿,它既不会ping也不会返回错误。

我正在使用Windows身份验证,它在SQL Management Studio中可以正常工作。

我已经验证了数据库名称,如果我输入一个错误的名称,会在open步骤中生成错误并记录。

我尝试设置了一个非常短的连接超时时间,但是仍然发生相同的情况。

我尝试连接到远程SQL Server,它可以正常工作。
远程服务器是SQL Server的开发者版本,名称类似于xyz.abc.123,而我的本地SQL Express被称为machine-name\sqlexpress。

这是一个示例程序:

package main

import (
	"database/sql"
	_ "github.com/denisenkom/go-mssqldb"
	"log"
)

func main() {
	
    log.Println("Main:")

    log.Println("Opening")
    db, err := sql.Open("mssql", "server=Machine-Name\\SQLEXPRESS; database=MyDatabaseName;")
    
    if err != nil {
        log.Println("Open Failed: ", err.Error())
    }
    log.Println("Opened")

    log.Println("Pinging")
    err = db.Ping();
    if err != nil {
        log.Println("Failed to ping: ", err.Error())
    }

    log.Println("Pinged")
}

希望对你有帮助!

英文:

I am trying to connect to sql server express using go and this sql driver "github.com/denisenkom/go-mssqldb"

Currently it calls open then stalls on the ping command, it never pings or returns an error.

I am using winauth and it works with sql management studio.

I have verified the database name, if I put a false one in, an error is generated and logged at the open step.

I have tried setting a really short connection time out but the same thing happens.

I have tried connecting to a remote sql server and it works correctly.
The remote server is a developer version of sql server and had a name like xyz.abc.123 where my local sql express is called machine-name\sqlexpress.

Here is a sample program.

package main

import (
	"database/sql"
	_ "github.com/denisenkom/go-mssqldb"
	"log"
)

func main() {
	
    log.Println("Main:")
    
    log.Println("Opening")
    db, err := sql.Open("mssql", "server=Machine-Name\\SQLEXPRESS; database=MyDatabaseName;")
    
    if err != nil {
        log.Println("Open Failed: ", err.Error())
    }
    log.Println("Opened")

    log.Println("Pinging")
    err = db.Ping();
    if err != nil {
        log.Println("Failed to ping: ", err.Error())
    }

    log.Println("Pinged")
}

答案1

得分: 5

我不是一个翻译程序,但我可以帮你翻译这段内容。以下是翻译的结果:

我不久前也遇到了同样的问题,在经过两天的研究后,我成功解决了这个问题。

令人困惑的是,go-mssqldb需要传递一个名为"server"的参数,在SQL Management Studio中连接属性中有一个明确的参考:

连接属性

然而,这不是go-mssqldb正在寻找的值。它正在寻找安装了数据库的实际服务器。也就是说,如果它安装在你的本地PC上,这个值应该是"localhost"。如果它安装在你网络中的另一台机器上,那么这个值应该是相应的IP地址或服务器名称。

还要记住的一件事是,go-mssqldb需要一个端口来连接数据库,如果你没有提供端口,它会默认使用端口1433。然而,我的端口不是这个值。为了查找你的端口号:

  1. 打开SQL Server Configuration Manager。
  2. 在左侧的树上展开"SQL Server Network Configuration"。
  3. 点击"Protocols for SQLEXPRESS(或你的数据库名称)"。
  4. 在主菜单中,确保"TCP/IP"已启用。如果没有启用,右键点击并选择"Enable"。
  5. 再次右键点击"TCP/IP",选择属性。
  6. 选择"IP Addresses"选项卡,向下滚动到底。
  7. 查看"IPAll"部分,"TCP Dynamic Ports"字段中显示的数字就是正确的端口号。

SQL Server Configuration Manager

最后,我最终使用的运行代码的命令是:

sqlserver-agent -debug=true -server=localhost -port=62587 -user=Username -password=Password -database=DatabaseName

英文:

I encountered this same issue not too long ago and after two days of researching I was able to fix the issue.

The confusing aspect seems to come from the fact that the go-mssqldb needs a parameter passed named "server" and in SQL Management Studio they have a clear reference to server in the connection properties:

Connection Properties

However, this is not the value go-mssqldb is looking for. It is looking for the actual server where the DB is installed. Meaning, if it is installed in your local PC this would be "localhost". If it's on another machine in your network, then this would be the corresponding IP address or server name

Another thing to keep in mind is that go-mssqldb needs a port to connect to the DB and if you don't provide one, it assumes the port is 1433. However, mine wasn't that value. In order to look for your port number:

  1. Open up SQL Server Configuration Manager.
  2. Expand "SQL Server Network Configuration" on the tree on the left.
  3. Click on "Protocols for SQLEXPRESS (or whatever name your DB has)"
  4. In the main menu, make sure "TCP/IP" is enabled. If not, right-click on it and select "Enable".
  5. Right-click on the "TCP/IP" again and select properties.
  6. Select the "IP Addresses" tab and scroll all the way down.
  7. Look at the section: "IPAll" and the number displayed in the "TCP Dynamic Ports" field is the correct port number.

SQL Server Configuration Manager

Finally, the command I ended up using to run my code ended up being:

sqlserver-agent -debug=true -server=localhost -port=62587 -user=Username -password=Password -database=DatabaseName

答案2

得分: -1

这个 https://code.google.com/p/odbc/ 驱动在我这里可以与 MS SQL Server 一起使用。

Alex

英文:

This https://code.google.com/p/odbc/ driver works with MS SQL Server for me.

Alex

huangapple
  • 本文由 发表于 2015年4月9日 10:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/29528609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定