2019-08-05 17:38
浏览 761

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:

    _ "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条回答 默认 最新

相关推荐 更多相似问题