Home > database >  How to use TLS 1.2 with MySql Go Drivers?
How to use TLS 1.2 with MySql Go Drivers?

Time:12-17

We have to use TLS1.2 to connect to our MySQL servers. In our java applications, we use the below JDBC URL -

jdbc:mysql://xxxx-001-dev.cluster-xx-2.rds.amazonaws.com/bats?**enabledTLSProtocols=TLSv1.2**

I am not able to achieve similar configuration when connection to mysql in our Go application -

cfg1 := mysql.Config{
        User:                 "admin",
        Passwd:               "xxxxxxx",
        Net:                  "tcp",
        Addr:                 "xxxx-001-dev.cluster-xx-2.rds.amazonaws.com:3306",
        DBName:               "xxxx",
        AllowNativePasswords: true,
    }

    sql.Open("mysql", cfg1.FormatDSN())

I tried adding below statements. But no help, it throws below error -

// enabledTLSProtocolsTLSv1.2
    cfg1 := mysql.Config{
        User:                 "admin",
        Passwd:               "xxxxxx",
        Net:                  "tcp",
        Addr:                 "xxxx-001-dev.cluster-xx-2.rds.amazonaws.com:3306",
        DBName:               "xxxx",
        AllowNativePasswords: true,
    }

    cfg1.TLS.MinVersion = tls.VersionTLS12
    cfg1.TLS.MaxVersion = tls.VersionTLS12

    sql.Open("mysql", cfg1.FormatDSN())

Error -

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x1 addr=0xf8 pc=0x64ac21]

goroutine 1 [running]:
main.main()
        C:/cmb-mmt/chp-schema-validation/main.go:28  0x61

We are using 5.7.12 MySQLversion

CodePudding user response:

Below code solved the issue. And I'm able to connect to MySQL successfully.

cfg1 := mysql.Config{
        User:                 cfg.Db.Dev.User,
        Passwd:               cfg.Db.Dev.Pass,
        Net:                  "tcp",
        Addr:                 "cxx-cxxx-auroramysql-001-dev.xxxxxxxxx.us-west-2.rds.amazonaws.com:3306",
        DBName:               "xxxx",
        AllowNativePasswords: true,
        TLSConfig:            "skip-verify",
        TLS:                  &tls.Config{MinVersion: tls.VersionTLS12, MaxVersion: tls.VersionTLS12},
    }
  • Related