Connecting to Cloud SQL with SSL using Go from App Engine
Google's documentation suggests using the following code to connect to Cloud SQL using Go and the go-sql-driver:
import "database/sql" import _ "github.com/go-sql-driver/mysql" db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname")
However, this may result in an x509 certificate error, indicating an invalid certificate for the specified project name and instance name. This issue arises when using SSL connections. To resolve it, the ServerName property must be set when registering a custom TLSConfig with the mysql driver, in addition to specifying the project-id:instance-name in sql.Open().
Here's an example of how to set up the TLS configuration:
mysql.RegisterTLSConfig("custom", &tls.Config{ RootCAs: rootCertPool, Certificates: clientCert, ServerName: "projectName:instanceName", })
Next, append ?tls=nameOfYourCustomTLSConfig to the connection string:
db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname?tls=custom")
By following these steps, you can successfully connect to Cloud SQL using SSL from Google App Engine.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3