Home  >  Article  >  Backend Development  >  How to Connect to Cloud SQL with SSL using Go from App Engine and Resolve Certificate Errors?

How to Connect to Cloud SQL with SSL using Go from App Engine and Resolve Certificate Errors?

Linda Hamilton
Linda HamiltonOriginal
2024-10-23 17:48:21324browse

How to Connect to Cloud SQL with SSL using Go from App Engine and Resolve Certificate Errors?

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.

The above is the detailed content of How to Connect to Cloud SQL with SSL using Go from App Engine and Resolve Certificate Errors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn