How to connect to Google Cloud SQL with go-sql-driver/mysql on App Engine?

huangapple go评论77阅读模式
英文:

How to connect to Google Cloud SQL with go-sql-driver/mysql on App Engine?

问题

我正在使用Go语言中的go-sql-driver/mysql驱动程序在App Engine上连接到Cloud SQL实例,代码如下:

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

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

但是在pingErr中我得到了"permission denied"的错误。

当然,我已经在Cloud SQL控制台的"访问控制"下检查了我的应用程序是否被授权,参考了文档。我还尝试添加了一个具有权限的MySQL用户,并在root的位置使用了user:password,甚至没有指定用户。

我做错了什么?

...

更新:

根据@Kyle的建议,我尝试了另一个驱动程序ziutek/mymysql,并且以下代码可以正常工作:

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"

如果我能找出问题出在哪里,我可能需要在go-sql-driver/mysql上提交一个拉取请求!欢迎任何见解或经验!

英文:

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!

答案1

得分: 4

这是一个版本问题

如果你仔细看,App Engine的支持已经添加到了主分支,但最新的发布版本是1.1,并不包含这个功能。

所以你需要手动git clone https://github.com/go-sql-driver/mysql(主分支)到$GOPATH/src/,然后再部署到App Engine,而不是使用go get github.com/go-sql-driver/mysql命令!

这个作者的评论让我注意到了这个问题,而@Kyle的建议尝试另一个驱动程序(成功了)激发了我重新阅读所有内容的动力 - 谢谢!

英文:

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!

答案2

得分: 2

你给sql.Open的数据源名称(DSN)看起来与文档中的不一样:

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

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

来源:Go AppEngine Cloud SQL 参考

编辑:看起来你正在使用正确的DSN格式,就像这里文档中所述一样。我猜测此时它已经成功连接到数据库,但可能是用户没有被接受,或者由于某种原因没有正确的权限。

英文:

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

import &quot;database/sql&quot;
import _ &quot;&lt;some mysql package&gt;&quot;

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

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.

huangapple
  • 本文由 发表于 2014年1月27日 03:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/21368363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定