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

时间:2019-08-05 17:38:11

标签: sql-server azure go ssl

我编写了一个系统,该系统可自动在Microsoft Azure上部署VM,在其上安装Sql Server,然后针对新安装的服务器执行resource "aws_alb" "bluegreen_balancer" { name = "${var.app_name}-${var.environment_name}-bluegreen-balancer" internal = true load_balancer_type = "application" security_groups = [ "${data.aws_cloudformation_export.ecs_host_sg.value}", "${data.aws_cloudformation_export.load_balancer_sg.value}", "${data.aws_cloudformation_export.redis_host_sg.value}" ] subnets = split(",", "${data.aws_cloudformation_export.subnets.value}") depends_on = [aws_cloudformation_stack.infrastructure] } resource "aws_alb_target_group" "ecs_blue_target_group" { name = "ecs-blue-targetgroup" port = 10002 protocol = "HTTP" vpc_id = "${data.aws_cloudformation_export.vpc_id.value}" target_type = "ip" health_check { path = "/" protocol = "HTTP" matcher = "200" interval = "60" timeout = "30" unhealthy_threshold = "3" healthy_threshold = "3" } depends_on = [aws_cloudformation_stack.infrastructure] } resource "aws_alb_target_group" "ecs_green_target_group" { name = "ecs-green-targetgroup" port = 10004 protocol = "HTTP" vpc_id = "${data.aws_cloudformation_export.vpc_id.value}" target_type = "ip" health_check { path = "/" protocol = "HTTP" matcher = "200" interval = "60" timeout = "30" unhealthy_threshold = "3" healthy_threshold = "3" } depends_on = [aws_cloudformation_stack.infrastructure] } resource "aws_alb_listener" "blue_listener" { load_balancer_arn = "${aws_alb.bluegreen_balancer.arn}" port = 10002 protocol = "HTTP" default_action { type = "forward" target_group_arn = "${aws_alb_target_group.ecs_blue_target_group.arn}" } depends_on = [aws_cloudformation_stack.infrastructure] } resource "aws_alb_listener" "green_listener" { load_balancer_arn = "${aws_alb.bluegreen_balancer.arn}" port = 10004 protocol = "HTTP" default_action { type = "forward" target_group_arn = "${aws_alb_target_group.ecs_green_target_group.arn}" } depends_on = [aws_cloudformation_stack.infrastructure] } resource "aws_ecs_service" "fargate_service" { #count = length(var.service_names) count = 1 launch_type = "FARGATE" desired_count = "2" name = "${var.environment_name}-${var.service_names[count.index]}" cluster = "${data.aws_cloudformation_export.cluster_arn.value}" task_definition = "${aws_cloudformation_stack.task_definitions[count.index].outputs["TaskDefinitionArn"]}" deployment_controller { type = "CODE_DEPLOY" } load_balancer { target_group_arn = "${aws_alb.bluegreen_balancer.arn}" container_name = "${var.environment_name}-${var.service_names[count.index]}" container_port = "${var.service_ports[count.index]}" } network_configuration { assign_public_ip = false subnets = split(",", "${data.aws_cloudformation_export.subnets.value}") security_groups = [ "${data.aws_cloudformation_export.ecs_host_sg.value}", "${data.aws_cloudformation_export.load_balancer_sg.value}", "${data.aws_cloudformation_export.redis_host_sg.value}" ] } depends_on = [ aws_cloudformation_stack.infrastructure ] } 脚本以初始化环境。在星期五,这一切都按预期进行。今天,我遇到了这个错误。

我的代码具有以下相关导入:

.sql

,并在安装数据库后使用以下代码实际连接到数据库(为简便起见删除了错误处理):

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

// 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) // <-------- } 和每个db.Ping()上均发生错误。在这些情况下,与db.Exec()关联的消息是

err

对于服务器本身,我使用以下脚本通过SSH进行安装:

TLS Handshake failed: Cannot read handshake packet: EOF

否则,这似乎可以很好地工作-我通过SSH手动连接到实例没有问题,并且脚本退出没有错误。我不确定它的相关性。


我尝试通过SSH自己进入服务器并在本地打开sql服务器控制台,这导致出现以下错误消息:

# 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};"

这可能是相关的,但我不知道如何。


这是什么原因导致的,如何解决?是我的代码中,我创建的充当服务器的VM上的错误还是两者之间?我如何最好地解决该问题?

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题,但没有找到根本原因。尝试通过SSH连接到VM并在本地访问数据库后,遇到以下错误:

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 GitHub issue,从而解决了我的问题。看来是版本问题-在我的安装脚本中,我替换了

sudo apt-get -y install mssql-server

使用

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

问题消失了。