无法使用来自 Google App Engine 的 SSL + Golang 连接到 Google Cloud SQL

Cannot connect to Google Cloud SQL using SSL + Golang from Google App Engine

Google 表示您可以使用 Golang 和 go-sql-driver 连接到 Google Cloud SQL,如下所示:

import "database/sql"
import _ "github.com/go-sql-driver/mysql"

db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname")

参考:https://cloud.google.com/appengine/docs/go/cloud-sql/reference

...但是,这(对我而言)会生成 x509 证书错误:

x509: certificate is valid for projectName:instanceName, not projectName

我不知道如何解决这个问题。在连接字符串中再次添加实例名称(即使它已经存在)没有帮助,根据 Google 自己的文档也不正确。

有没有人设法做到这一点?怎么了?

您正在使用 SSL 连接吗?此错误消息表明,当您使用 mysql 驱动程序注册自定义 TLSConfig 时,必须设置 ServerName 属性, 除了 指定 project-id:instance-name 里面 sql.Open().

例如使用 TLS 设置 from the docs,但在对 RegisterTLSConfig:

的调用中添加 ServerName
mysql.RegisterTLSConfig("custom", &tls.Config{
            RootCAs:      rootCertPool,
            Certificates: clientCert,
            ServerName:   "projectName:instanceName",
        })

然后追加?tls=nameOfYourCustomTLSConfig

db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname?tls=custom")