如何在App Engine上使用go-sql-driver / mysql连接到Google Cloud SQL?

如何在App Engine上使用go-sql-driver / mysql连接到Google Cloud SQL?

问题描述:

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!

我正在使用 go-sql-driver / mysql 驱动程序以连接到Cloud SQL实例,如下所示: p>

  import  (
“数据库/ sql” 
 _“ github.com/go-sql-driver/mysql"
)

db,dbErr:= sql.Open(” mysql“,” root @ cloudsql(project: 实例)/数据库” 
 ... 
pingErr:= db.Ping()
  code>  pre> 
 
 

,但我收到“ 权限被拒绝 strong>” 在 pingErr code>中。 p>

当然,我已经检查了我的应用是否已在Cloud SQL控制台的“访问控制”下得到了授权根据文档。我还尝试添加具有特权的MySQL用户并使用 user:password code>代替 root code>,甚至没有指定用户。 p>

我在做什么错了? p>

... p>

更新: strong> p>

根据@Kyle的建议,我尝试了 备用驱动程序 ziutek / mymysql ,它可与以下代码一起使用: p>

 导入(
“数据库/ 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“ 
  code>  pre> 
 \  n 

猜想是时候在 go-sql-driver / mysql 上发出拉取请求了 如果我能找出问题所在! 任何见解或经验都值得赞赏! p> div>

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!

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.