How to Resolve SSL Connection Issues for Google Cloud SQL with Golang?

Mary-Kate Olsen
Release: 2024-10-24 05:37:30
Original
820 people have browsed it

How to Resolve SSL Connection Issues for Google Cloud SQL with Golang?

Troubleshooting SSL Connections to Google Cloud SQL with Golang from Google App Engine

When attempting to establish a connection to Google Cloud SQL from Google App Engine using the go-sql-driver and SSL, developers often encounter a "certificate is valid for projectName:instanceName, not projectName" error. This issue arises when the ServerName property is not explicitly set when registering a custom TLSConfig with the mysql driver.

To address this problem, ensure you include the following steps in your code:

  1. Register a TLSConfig with the desired certificate and keys, setting the ServerName to your project and instance name:
<code class="go">mysql.RegisterTLSConfig("custom", &tls.Config{
    RootCAs:      rootCertPool,
    Certificates: clientCert,
    ServerName:   "projectName:instanceName", // <-- Added ServerName property
})</code>
Copy after login
  1. Append "?tls=nameOfYourCustomTLSConfig" to your database connection string:
<code class="go">db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname?tls=custom")</code>
Copy after login

By implementing these adjustments, you will successfully establish an SSL connection to your Cloud SQL instance from Google App Engine using Golang.

The above is the detailed content of How to Resolve SSL Connection Issues for Google Cloud SQL with Golang?. 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!