Question

I'm using the go-sql-driver/mysql driver in Go on App Engine to connect to a Cloud SQL instance like this:

import (
   "database/sql"
   _ "github.com/go-sql-driver/mysql"
)

db, dbErr := sql.Open("mysql", "root@cloudsql(project:instance)/database"
...
pingErr := db.Ping()

but I get "permission denied" in pingErr.

Of course, I've checked my app is authorized in the Cloud SQL console under "Access Control" per the docs. I also tried adding a MySQL user with privileges and using user:password in place of root and even not specifying a user.

What am I doing wrong?

...

Update:

Per @Kyle's suggestion I tried an alternate driver ziutek/mymysql and it works with the following code:

import (
   "database/sql"
   _ "github.com/ziutek/mymysql/godrv"
   _ "github.com/ziutek/mymysql/mysql"
   _ "github.com/ziutek/mymysql/native"
)

db, dbErr := sql.Open("mymysql", "cloudsql:project:instance*database/user/password"

Guess it's time for a pull request on go-sql-driver/mysql if I can figure out what's going wrong! Any insights or experience appreciated!

Was it helpful?

Solution

It's a versioning issue!

If you look carefully, App Engine support was added to the master branch but the latest release is 1.1 and doesn't include it.

Instead of go get github.com/go-sql-driver/mysql you need to manually git clone https://github.com/go-sql-driver/mysql (master branch) into $GOPATH/src/ and then deploy to App Engine!

This comment from the author is what tipped me off and @Kyle's suggestion to try another driver (which worked) motivated me to re-read everything again - thanks!

OTHER TIPS

The DSN (data source name) you're giving to sql.Open doesn't look like the one in the docs:

import "database/sql"
import _ "<some mysql package>"

db, err := sql.Open("mysql", "cloudsql:my-instance*dbname/user/passwd")

Source: Go AppEngine Cloud SQL Reference

Edit: It looks like you're using the correct DSN format for your SQL package as documented here. My guess at that point is that it's correctly connecting to the database, but that either the user isn't being accepted or, for whatever reason, doesn't have the correct permissions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top