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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧