"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Posted on 2025-03-23
Browse:547

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.

Release Statement This article is reproduced on: 1729669644 If there is any infringement, please contact [email protected] to delete it.
Latest tutorial More>

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