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

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

How to use TLS 1.2 with MySql Go Drivers?

问题

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

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

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

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())

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

// 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())

错误信息:

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

我们正在使用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 -

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

答案1

得分: 0

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

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},
}
英文:

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},
	}

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:

确定