如何在Go中使用TLS 1.2与MySql Go驱动程序?

huangapple go评论165阅读模式
英文:

How to use TLS 1.2 with MySql Go Drivers?

问题

我们必须使用TLS1.2来连接我们的MySQL服务器。在我们的Java应用程序中,我们使用以下JDBC URL:

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

但是在我们的Go应用程序中连接MySQL时,我无法实现类似的配置:

  1. cfg1 := mysql.Config{
  2. User: "admin",
  3. Passwd: "xxxxxxx",
  4. Net: "tcp",
  5. Addr: "xxxx-001-dev.cluster-xx-2.rds.amazonaws.com:3306",
  6. DBName: "xxxx",
  7. AllowNativePasswords: true,
  8. }
  9. sql.Open("mysql", cfg1.FormatDSN())

我尝试添加以下语句,但没有帮助,它会抛出以下错误:

  1. // enabledTLSProtocolsTLSv1.2
  2. cfg1 := mysql.Config{
  3. User: "admin",
  4. Passwd: "xxxxxx",
  5. Net: "tcp",
  6. Addr: "xxxx-001-dev.cluster-xx-2.rds.amazonaws.com:3306",
  7. DBName: "xxxx",
  8. AllowNativePasswords: true,
  9. }
  10. cfg1.TLS.MinVersion = tls.VersionTLS12
  11. cfg1.TLS.MaxVersion = tls.VersionTLS12
  12. sql.Open("mysql", cfg1.FormatDSN())

错误信息:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xc0000005 code=0x1 addr=0xf8 pc=0x64ac21]
  3. goroutine 1 [running]:
  4. main.main()
  5. C:/cmb-mmt/chp-schema-validation/main.go:28 +0x61

我们正在使用5.7.12版本的MySQL。

英文:

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

  1. 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 -

  1. cfg1 := mysql.Config{
  2. User: "admin",
  3. Passwd: "xxxxxxx",
  4. Net: "tcp",
  5. Addr: "xxxx-001-dev.cluster-xx-2.rds.amazonaws.com:3306",
  6. DBName: "xxxx",
  7. AllowNativePasswords: true,
  8. }
  9. sql.Open("mysql", cfg1.FormatDSN())

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

  1. // enabledTLSProtocolsTLSv1.2
  2. cfg1 := mysql.Config{
  3. User: "admin",
  4. Passwd: "xxxxxx",
  5. Net: "tcp",
  6. Addr: "xxxx-001-dev.cluster-xx-2.rds.amazonaws.com:3306",
  7. DBName: "xxxx",
  8. AllowNativePasswords: true,
  9. }
  10. cfg1.TLS.MinVersion = tls.VersionTLS12
  11. cfg1.TLS.MaxVersion = tls.VersionTLS12
  12. sql.Open("mysql", cfg1.FormatDSN())

Error -

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xc0000005 code=0x1 addr=0xf8 pc=0x64ac21]
  3. goroutine 1 [running]:
  4. main.main()
  5. C:/cmb-mmt/chp-schema-validation/main.go:28 +0x61

We are using 5.7.12 MySQLversion

答案1

得分: 0

以下代码解决了问题,并且我能够成功连接到MySQL。

  1. cfg1 := mysql.Config{
  2. User: cfg.Db.Dev.User,
  3. Passwd: cfg.Db.Dev.Pass,
  4. Net: "tcp",
  5. Addr: "cxx-cxxx-auroramysql-001-dev.xxxxxxxxx.us-west-2.rds.amazonaws.com:3306",
  6. DBName: "xxxx",
  7. AllowNativePasswords: true,
  8. TLSConfig: "skip-verify",
  9. TLS: &tls.Config{MinVersion: tls.VersionTLS12, MaxVersion: tls.VersionTLS12},
  10. }
英文:

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

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

huangapple
  • 本文由 发表于 2022年12月14日 03:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/74790148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定