Home >Backend Development >Golang >How to use TLS 1.2 with MySql Go driver?

How to use TLS 1.2 with MySql Go driver?

WBOY
WBOYforward
2024-02-10 09:40:09739browse

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

php Xiaobian Youzi will introduce you to how to use TLS 1.2 with the MySQL Go driver in this article. TLS 1.2 is a secure transport protocol used to protect the security of network communications. When using the MySQL Go driver to connect to a database, enabling TLS 1.2 can improve the security of data transmission. This article will detail how to configure and use TLS 1.2 with the MySQL Go driver to establish a secure database connection. Following the guidance of this article, you will be able to easily use TLS 1.2 with the MySQL Go driver to protect your data and communications.

Question content

We must use tls1.2 to connect to our mysql server. In our java application we are using the following jdbc url -

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

I am unable to achieve a similar configuration when connecting 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 the following statements. But it doesn't help, it throws the following 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())

mistake-

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 the 5.7.12 mysql version

Solution

The following code solves this problem. and i am 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},
    }

The above is the detailed content of How to use TLS 1.2 with MySql Go driver?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete