英文:
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},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论