Home >Backend Development >Golang >How to Resolve SSL Certificate Errors in Go When Connecting to Google Cloud SQL?
According to Google's documentation, establishing a database connection between Go and Google Cloud SQL using the go-sql-driver and SSL should be straightforward. However, users may encounter a perplexing x509 certificate error.
The error message suggests that the user is connecting with SSL. To rectify this issue, ensure that the ServerName property is set within the RegisterTLSConfig call made with the mysql driver. This configuration must be performed in conjunction with specifying the project-id:instance-name within the sql.Open() function.
To address this issue, modify your TLS configuration to include a custom ServerName setting, as shown below:
<code class="go">mysql.RegisterTLSConfig("custom", &tls.Config{ RootCAs: rootCertPool, Certificates: clientCert, ServerName: "projectName:instanceName", })</code>
Subsequently, append the following string to your database connection string:
<code class="go">?tls=nameOfYourCustomTLSConfig</code>
For instance:
<code class="go">db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname?tls=custom")</code>
The above is the detailed content of How to Resolve SSL Certificate Errors in Go When Connecting to Google Cloud SQL?. For more information, please follow other related articles on the PHP Chinese website!