-1

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?

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • did you open port 1433 on the Azure network security group for your VM as well as in any firewall that you might have running on the VM itself? – silent Aug 05 '19 at 18:05
  • @silent `netstat -peanut` shows `tcp 0 0 0.0.0.0:1433 0.0.0.0:* LISTEN`, which should be right – Green Cloak Guy Aug 05 '19 at 19:21
  • that just means that the SQL Server is running - not that you can really reach it from the outside – silent Aug 05 '19 at 19:22
  • @silent right, sorry. The Azure NSG is also set to `Any` source, `*` port, `Any` destination, `*` port. I have a similar thing worked up for MySQL and Postgres, and when I run my program for those it works fine, so I don't think the NSG is the issue. – Green Cloak Guy Aug 05 '19 at 19:23
  • Just a very simple powershell command and you'll know for sure: `Test-NetConnection -ComputerName 1.2.3.4 -port 1433` – silent Aug 05 '19 at 19:26
  • @silent I did that locally and got several lines of diagnostics after a few seconds, including `TcpTestSucceeded : True`. Seems to have worked properly. – Green Cloak Guy Aug 05 '19 at 19:30
  • ok, at least this is clear now. This one seems to be your issue. And it is... in the github repo of your go package that you are using. I'd try it there: https://github.com/denisenkom/go-mssqldb/issues/452 – silent Aug 05 '19 at 19:32
  • @silent unfortunately, there doesn't seem to be a workaround specified in that GitHub issue (I did see it before I asked my question here), or if there is I don't understand it. Am I mistaken? – Green Cloak Guy Aug 05 '19 at 20:05
  • no, but it seems related to that package. So I think you'd have better luck there than here on SO. – silent Aug 05 '19 at 20:17
  • 1
    I'm having the same problem now with Grafana and I cannot connect to the SQL Server via SQL Server Management Studio any more. My server is for test purposes and therefore not encrypted. This happened after installing updates on my Ubuntu machine. So this seems to be a problem on the SQL Server side, but I couldn't solve it with `mssql-conf`, so far. – Chris Tophski Aug 06 '19 at 11:42

1 Answers1

1

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.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53