Home:ALL Converter>Go MySQL with SSL - unexpected EOF

Go MySQL with SSL - unexpected EOF

Ask Time:2019-03-12T05:51:30         Author:Jordan

Json Formatter

I'm using the Go MySQL driver as part of a project. Recently, there was a need to enable SSL on the MySQL database I am connecting to. I have two servers that are configured with TLSv1.1. On one server, I am able connect successfully, but on the other I am getting unexpected EOF from the library.

I tried setting MaxIdleConnections to 0 as described in Golang MySQL error - packets.go:33: unexpected EOF, but no luck. Prior to enabling SSL, connections to the server were working. There are other apps not written in Go that are able to connect successfully using the same credentials.

When my app runs, the ping command fails immediately. The MySQL server logs are saying bad handshake. Is there something off with my connection code below, or is there a database setting that could be refusing to connect with skip-verify?

import (
    "database/sql"
    "io/ioutil"
    "strings"
    "text/template"

    log "github.com/sirupsen/logrus"
    // Import runtime for MySQL
    mysql "github.com/go-sql-driver/mysql"
)

type DatabaseConnection struct {
    Hostname string
    Port     int
    Database string
    Username string
    Password string
}

func CreateSSLConnection(db *DatabaseConnection) (*sql.DB, error) {
    templateString := `{{.Username}}:{{.Password}}@{{.Hostname}}:{{.Port}}/{{.Database}}?tls=skip-verify`

    conn, err := connectToDatabase("mysql", templateString, db)

    // error: "unexpected EOF"
    err = conn.Ping()
    return conn, err
}

func connectToDatabase(databaseType string, connectionTemplateString string, db *DatabaseConnection) (*sql.DB, error) {
    connectionTemplate := template.Must(template.New("connectionString").Parse(connectionTemplateString))
    builder := strings.Builder{}

    err := connectionTemplate.Execute(&builder, db)
    if err != nil {
        log.WithFields(log.Fields{
            "template": connectionTemplateString,
            "error":    err,
        }).Error("Failed to create connection string")
        return nil, err
    }

    return sql.Open(databaseType, builder.String())
}

Author:Jordan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55110970/go-mysql-with-ssl-unexpected-eof
yy