ds0678 2019-08-05 17:38
浏览 890
已采纳

Golang与SQL Server的连接错误-“ TLS握手失败:无法读取握手数据包:EOF”

I wrote a system that automates the process of deploying a VM on Microsoft Azure, installs Sql Server on it, and then executes a .sql script against the newly-installed server in order to initialize the environment. On Friday, this was all working as intended. Today, I'm getting this error.

My code has the following relevant imports:

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

and uses the following code to actually connect to the database once it's installed (error handling removed for brevity):

// variables
connectionString := "sqlserver://MasterUser:MasterPassword@xx.xx.xx.xxx:1433"
dbName := "mssql"
dbFile := "mssql.sql"
// open database / get metadata sorted
db, err := sql.Open(dbname, connectionString)
defer db.Close()
//Check to see if the connection is successful. 
err = db.Ping()                                         // <--------
// file input
fileBytes, err := ioutil.ReadFile("../sql/" + dbsql)
fileReader := bytes.NewReader(fileBytes)
// parse line-by-line
scanner := bufio.NewScanner(fileReader)
lineNo := 0
for scanner.Scan() {
    toExec := scanner.Text()
    lineNo += 1
    _, err = db.Exec(toExec)                           // <--------
}

The error occurs on both db.Ping() and on every db.Exec(). In those cases, the message associated with err is

TLS Handshake failed: Cannot read handshake packet: EOF

As for the server itself, I install that over SSH with the following script:

# install mssql server
sudo wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt-get -y update
sudo apt-get -y install mssql-server
# configure MSSQL
sudo /opt/mssql/bin/mssql-conf setup
# install local tools
sudo ACCEPT_EULA=y apt-get -y install mssql-tools
# do basic initialization in advance of .sql script
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ${MasterPassword} -Q "CREATE LOGIN ${MasterUser} WITH PASSWORD = '${MasterPassword}';"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ${MasterPassword} -Q "CREATE USER ${MasterUser} FOR LOGIN ${MasterUser};"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ${MasterPassword} -Q "ALTER SERVER ROLE sysadmin ADD MEMBER ${MasterUser};"

This seems to work fine otherwise - I have no trouble connecting to the instance manually via SSH, and that script exits with no errors. I'm not sure how relevant it is.


I tried SSHing into the server myself and opening a sql server console locally, which resulted in the following error messages:

$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P *********
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.

This is probably relevant, but I don't know how.


What's causing this, and how do I fix it? Is the error in my code, on the VM I've created that's acting as the server, or in between? How would I best fix the problem?

  • 写回答

1条回答 默认 最新

  • doumu8217 2019-08-06 17:47
    关注

    I ended up fixing this without identifying the root cause. Once I tried connecting to the VM over SSH and accessing the database locally, I experienced the following errors:

    Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
    Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
    

    Following up on those led me to this GitHub issue, which provided a fix to my problem. It looks to have been a versioning issue - in my install script, I replaced

    sudo apt-get -y install mssql-server
    

    with

    sudo apt-get -y install mssql-server=14.0.3192.2-2
    

    and the problem went away.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题