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

Linda Hamilton
Release: 2024-10-23 17:48:21
Original
323 people have browsed it

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")
Copy after login

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",
})
Copy after login

Next, append ?tls=nameOfYourCustomTLSConfig to the connection string:

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

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!