Golang:访问Google App Engine CloudSQL

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

Golang: Accessing Google App Engine CloudSQL

问题

新问题:我在Google上找到的所有信息都表明,CloudSQL连接器应该可以工作,除了源代码本身(导入SDK的cloudsql不起作用)。如果存在的话,我可以在哪里找到这个更新的库?

---编辑:我已经找到答案:问题似乎是CloudSQL本身而不是驱动程序或类似的东西。---

我正在尝试从Google App Engine Go程序访问CloudSQL。
我尝试过go-sql-driver/mysql和ziutek/mymysql,使用Go 1.2.1和Go 1.4.2。我尝试过go-sql-driver的go get版本,也尝试过直接从Github克隆。我尝试过App Engine安装程序和存档文件。

每次我尝试使用我的应用程序访问数据库时,结果的网页显示:
cloudsql:在开发环境中尚不支持

我看到了这里的其他类似问题,尝试了那里提到的所有方法,但都没有成功。

相关代码:

import (
_ "github.com/go-sql-driver/mysql"
_ "appengine/cloudsql"
"database/sql"
"net/http"

)

func adminLogin(w http.ResponseWriter, r *http.Request) {
username := formatter(r.FormValue("username"))
password := formatter(r.FormValue("password"))
db, err := sql.Open("mysql",
"root:password@cloudsql(ws-virtual-classroom:database)/logins") // And all the variations on that string I could think of...
defer db.Close()
if err != nil {
log.Print(err)
} else {
rows, err := db.Query("SELECT username FROM admin_logins WHERE username=? AND password=? LIMIT 1", username, password)
defer rows.Close()
if err != nil {
log.Print(err)
} else {
var user string
for rows.Next() {
err = rows.Scan(&user)
if err != nil {
log.Print(err)
} else {
makeCookie(w, r, user, true, true)
}
}
}
}
teachersHome(w, r)
}

结果(在我提交登录表单后在浏览器中显示):

the runtime process gave a bad HTTP response: ''

2015/05/17 01:53:06 cloudsql: not supported in dev yet
2015/05/17 01:53:06 http: panic serving 127.0.0.1:56970: runtime error: invalid memory address or nil pointer dereference
goroutine 12 [running]:
net/http.func·011()
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1130 +0xbb
database/sql.(*Rows).Close(0x0, 0x0, 0x0)
    /tmp/appengine/go_appengine/goroot/src/database/sql/sql.go:1659 +0x31
main57750.adminLogin(0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    main.go:208 +0x25a
net/http.HandlerFunc.ServeHTTP(0x927c78, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1265 +0x41
github.com/gorilla/mux.(*Router).ServeHTTP(0xc20800c730, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    /home/daniel/go/src/github.com/gorilla/mux/mux.go:98 +0x297
net/http.(*ServeMux).ServeHTTP(0xc20803a6f0, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1541 +0x17d
appengine_internal.handleFilteredHTTP(0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    /tmp/appengine/go_appengine/goroot/src/appengine_internal/api_dev.go:98 +0x413
net/http.HandlerFunc.ServeHTTP(0x927248, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1265 +0x41
net/http.serverHandler.ServeHTTP(0xc208042540, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1703 +0x19a
net/http.(*conn).serve(0xc2080457c0)
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1204 +0xb57
created by net/http.(*Server).Serve
    /tmp/appengine/go_appengine/goroot/src/net/http/server.go:1751 +0x35e

这是在使用goapp serve运行时发生的。如果我部署它,似乎遇到了同样的问题-结果页面是空白的,而不是显示该文本,但日志错误消息是相同的。

英文:

New question: Everything I can find on Google suggests that the CloudSQL connector should be working, with the exception of the source code itself (and importing the SDK's cloudsql doesn't work). Is there some place I can find this updated library, if it exists?

---EDIT: I have my answer: it seems the problem is with CloudSQL itself rather than with the driver or something similar.---

I'm trying to access CloudSQL from a Google App Engine Go program.
I've tried both go-sql-driver/mysql and ziutek/mymysql, using Go 1.2.1 and Go 1.4.2. I've tried both the go get version of go-sql-driver and cloned it directly from Github. I've tried both the App Engine installer and the archive.

Every time I try to access a database with my application, the resulting webpage states:
cloudsql: not supported in dev yet

I've seen the other similar question here, tried everything noted there, none of it worked.

The code in question:

    import (
    _ "github.com/go-sql-driver/mysql"
    _ "appengine/cloudsql"
    "database/sql"
    "net/http"
)

func adminLogin(w http.ResponseWriter, r *http.Request) {
	username := formatter(r.FormValue("username"))
	password := formatter(r.FormValue("password"))
	db, err := sql.Open("mysql",
		"root:password@cloudsql(ws-virtual-classroom:database)/logins") // And all the variations on that string I could think of...
	defer db.Close()
	if err != nil {
		log.Print(err)
	} else {
		rows, err := db.Query("SELECT username FROM admin_logins WHERE username=? AND password=? LIMIT 1", username, password)
		defer rows.Close()
		if err != nil {
			log.Print(err)
		} else {
			var user string
			for rows.Next() {
				err = rows.Scan(&user)
				if err != nil {
					log.Print(err)
				} else {
					makeCookie(w, r, user, true, true)
				}
			}
		}
	}
	teachersHome(w, r)
}

The result (this is displayed in my browser after I submit the login form):

the runtime process gave a bad HTTP response: ''

2015/05/17 01:53:06 cloudsql: not supported in dev yet
2015/05/17 01:53:06 http: panic serving 127.0.0.1:56970: runtime error: invalid memory address or nil pointer dereference
goroutine 12 [running]:
net/http.func·011()
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1130 +0xbb
database/sql.(*Rows).Close(0x0, 0x0, 0x0)
	/tmp/appengine/go_appengine/goroot/src/database/sql/sql.go:1659 +0x31
main57750.adminLogin(0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	main.go:208 +0x25a
net/http.HandlerFunc.ServeHTTP(0x927c78, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1265 +0x41
github.com/gorilla/mux.(*Router).ServeHTTP(0xc20800c730, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	/home/daniel/go/src/github.com/gorilla/mux/mux.go:98 +0x297
net/http.(*ServeMux).ServeHTTP(0xc20803a6f0, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1541 +0x17d
appengine_internal.handleFilteredHTTP(0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	/tmp/appengine/go_appengine/goroot/src/appengine_internal/api_dev.go:98 +0x413
net/http.HandlerFunc.ServeHTTP(0x927248, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1265 +0x41
net/http.serverHandler.ServeHTTP(0xc208042540, 0x7f76a72ef5d8, 0xc208045860, 0xc2080c4820)
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1703 +0x19a
net/http.(*conn).serve(0xc2080457c0)
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1204 +0xb57
created by net/http.(*Server).Serve
	/tmp/appengine/go_appengine/goroot/src/net/http/server.go:1751 +0x35e

This is when running it with goapp serve. If I deploy it I seem to have the same problem--the resulting page is blank rather than showing that text, but the log error messages are the same.

答案1

得分: 6

更新的库在这里:https://godoc.org/google.golang.org/appengine。不过它没有CloudSQL。也许你应该在本地使用普通的mysql连接:

func dialSQL() (*sql.DB, error) {
    if appengine.IsDevAppServer() {
        // 或者 sql.Open("mysql", "user-name:password@ip-address-of-google-cloud-sql-instance/dbname")
        return sql.Open("mysql", "user:password@/dbname")
    }
    return sql.Open("mysql", "cloudsql:my-instance*dbname/user/passwd")
}

使用本地数据库通常是最好的选择,因为连接到实时数据库可能非常危险。例如,很容易不小心在生产数据库上运行测试并删除所有数据。

尽管如此,Google在这里提供了连接到CloudSQL的说明:https://cloud.google.com/sql/docs/introduction。第三方工具的说明也适用于Go语言。

英文:

The updated library is here: https://godoc.org/google.golang.org/appengine. It doesn't have CloudSQL though. Perhaps you should just use a regular mysql connection locally:

func dialSQL() (*sql.DB, error) {
	if appengine.IsDevAppServer() {
		// or sql.Open("mysql", "user-name:password@ip-address-of-google-cloud-sql-instance/dbname")
		return sql.Open("mysql", "user:password@/dbname")
	}
	return sql.Open("mysql", "cloudsql:my-instance*dbname/user/passwd")
}

Using a local database is usually the best option because connecting to a live database can be super dangerous. For example it's all too easy to accidentally run your tests against your production database and drop everything.

Nevertheless, Google has instructions for connecting to CloudSQL here: https://cloud.google.com/sql/docs/introduction. The instructions for 3rd party tools will work for Go too.

答案2

得分: 1

我不认为cloudsql做了什么。无论如何,该源代码看起来只是返回了那个错误。

https://code.google.com/p/appengine-go/source/browse/appengine/cloudsql/cloudsql.go

// Dial连接到指定的Cloud SQL实例。
func Dial(instance string) (net.Conn, error) {
return connect(instance)
}

https://code.google.com/p/appengine-go/source/browse/appengine/cloudsql/cloudsql_dev.go

func connect(instance string) (net.Conn, error) {
return nil, errors.New("cloudsql: 在开发环境中尚不支持")
}

英文:

I don't think cloudsql does anything? The source just looks like it returns that error no matter what lol

https://code.google.com/p/appengine-go/source/browse/appengine/cloudsql/cloudsql.go

// Dial connects to the named Cloud SQL instance.
    func Dial(instance string) (net.Conn, error) {
            return connect(instance)
    }

https://code.google.com/p/appengine-go/source/browse/appengine/cloudsql/cloudsql_dev.go

func connect(instance string) (net.Conn, error) {
        return nil, errors.New("cloudsql: not supported in dev yet")
}

huangapple
  • 本文由 发表于 2015年5月17日 09:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/30282570.html
匿名

发表评论

匿名网友

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

确定