To remotely connect to a Google-cloud Mysql database via the Go Proxy library, I need to provide service account credentials. This can be done through setting the GOOGLE_APPLICATION_CREDENTIALS
environment variable, but since I want the application to be able to run on different machines without having to set the environment variable everywhere, that is not an option.
As a result, I have to provide the service account credentials manually to my Golang application. The code below (without the authentication) gives this error message:
default proxy initialization failed; consider calling proxy.Init explicitly: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
Google provides elaborate documentation on how to fix the manual authentication for a number of programming languages, but not for Go:
https://cloud.google.com/docs/authentication/production#auth-cloud-explicit-csharp
Could anyone help me out with setting authentication credentials manually in Golang?
Thanks a bunch!
package main
import (
"database/sql"
"fmt"
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
)
var db *sql.DB
func main() {
cfg := mysql.Cfg("mysql", "********", "********") //name , username, password
cfg.DBName = "MyDBName"
db := mysql.DialCfg(cfg)
defer db.Close()
}