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

izkcnapc  于 2022-12-17  发布在  Mysql
关注(0)|答案(1)|浏览(157)

我们必须使用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版本

xkftehaa

xkftehaa1#

下面的代码解决了这个问题。我能够成功地连接到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},
    }

相关问题