喜欢堆栈,这是我完全沮丧的第一篇文章。感谢您的评论!
- 创建的App Engine Project
- 在我的应用引擎项目中创建了第二代MySQL实例
- 在MySQL实例中创建数据库
- 在App Engine中,我激活 -> Google Cloud Shell< - 。(我在我的console.cloud.google.com上在命令提示符下工作)
我已经复制了这个基本的GO程序以连接到我的MySQL实例。
我构建它并运行它。去构建mysqlexample.go./mysqlexample
我无法达到成功的联系。您可以看到我尝试过的所有各种连接字符串,在它们的右边是我得到的响应。
我可以使用MySQL管理员从本地Windows机器连接。
帮助?
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
)
func main() {
const dbIP = "104.xxx.xx.x"
const dbInstanceName = "esp-1-dev:us-central1:espdev"
const dbName = "servpro"
const dbUserName = "root"
const dbPassword = "xxxxxxx"
const dbOpenString = dbUserName + ":" + dbPassword + "@/" + dbInstanceName + "/" + dbName //GETS RESPONSE default addr for network 'AppEngine:Zone:Project' unknown
//const dbOpenString = dbUserName + "@cloudsql(" + dbInstanceName + ")/" + dbName //GETS RESPONSE dial cloudsql: unknown network cloudsql
//const dbOpenString = dbUserName + "@/" //+ "?parseTime=true&loc=UTC" //GETS RESPONSE getsockopt: connection refused
//const dbOpenString = dbUserName + ":" + dbPassword + "@tcp(" + dbIP + ":3306)/" + dbName //GETS RESPONSE dial tcp 104.xxx.xxx.x:3306: getsockopt: connection timed out
// Got this from stack overflow. GoDocs are not updated to reflect 2nd Gen databases.
// http://stackoverflow.com/questions/38890022/tls-requested-but-server-does-not-support-tls-error-with-google-cloud-sql-2nd
//user:password@cloudsql(copiedPastedInstanceConnectionName)/databaseName?charset=charset&collation=collation&tls=tlsConfigName&parseTime=true
//First Generation Connection String
//username:password@cloudsql(appID:CloudSQLInstance)/databasename?parseTime=true&loc=UTC
db, err := sql.Open("mysql", dbOpenString);
defer db.Close()
log.Println("Attempting Ping of database....")
err = db.Ping()
if err != nil {
log.Println("db.Ping() failed: " + dbOpenString)
log.Println(err)
} else {
log.Println("Success!")
}
}
以下是正确的连接字符串,但根据您从。
连接的App Engine的版本不同。应用引擎标准:
user:password@cloudsql(INSTANCE_CONNECTION_NAME)/dbname
应用引擎灵活:
user:password@unix(/cloudsql/INSTANCE_CONNECTION_NAME)/dbname
如果要迁移到第二代GO App Engine(Golang 1.11)
user:password@cloudsql(instanceID)/db
to
user:password@unix(/cloudsql/instanceID)/db
https://cloud.google.com/appengine/docs/flexible/go/using-cloud-sql
供新朋友使用,应用引擎和CloudSQL仅编写最简单的GO程序来与您的CloudSQL第二代数据库进行连接和通信!
您可以选择,应用程序工程或应用程序Eng Flex,SQL 1st或第二代....取决于组合连接字符串不同。当您搜索搜索时,Google的所有文档都将驱动到第一个Gen SQL和App Engine没有Flex,因为这主要是生产中的内容。如果您这样做,请确保您正在阅读Flex文档。如果这样做,请确保您正在阅读第二代文档。有时它们是完全不同的文档,有时文档堆叠在页面上,您必须拿到底部才能查看较新的东西第二代SQL和App Eng flex。
。Cloudshell很棘手,我仍然无法编译去这里的SQL第二。我正在从部署的App Engine Flex中成功地与Cloud SQL第二代交谈,并运行SQL代理,您必须使用SQL代理。您必须在Appengine和SQL上通过此创建用户进行设置。
这是我的工作程序。
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
"fmt"
"net/http"
)
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
}
func main() {
http.HandleFunc("/", handle)
http.HandleFunc("/_cloudshellProxy/_ah/health", healthCheckHandler)
log.Print("Listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handle(w http.ResponseWriter, r *http.Request) {
const dbIP = "104.xxx.xxx.x"
const dbInstanceName = "projectname:us-central1:sqlinstance"
const dbName = "servxxx"
const dbUserName = "sqlproxysuser"
const dbPassword = "xxxRockxxx"
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Fprint(w, "Hello SQL! Hello?")
fmt.Fprint(w, "n")
const dbOpenString = dbUserName + ":" + dbPassword + "@unix(/cloudsql/" + dbInstanceName + ")/" + dbName
//const dbOpenString = dbUserName + ":" + dbPassword + "@cloudsql(" + dbInstanceName + ")/" + dbName
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=- SQL OPEN Statement, per docs, DOES NOT return an error ever
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
db, err := sql.Open("mysql", dbOpenString);
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Fprint(w, "Failed Connection" + " " + dbOpenString)
fmt.Fprint(w, "n")
fmt.Fprint(w, err)
return
} else {
fmt.Fprint(w, "SUCCESSFUL CONNECTION" + " " + dbOpenString)
fmt.Fprint(w, "n")
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS exercisecloudsql101 (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, description TEXT, PRIMARY KEY (id))")
if err != nil {
fmt.Fprint(w, "CREATE TABLE failed:")
fmt.Fprint(w, "n")
fmt.Fprint(w, err)
fmt.Fprint(w, "n")
} else {
fmt.Fprint(w, "SUCCESSFUL CreateTable" + " " + dbOpenString)
fmt.Fprint(w, "n")
}
}
已经遇到了连接到云SQL实例的相同问题。下面是我在Windows Localhost环境上工作的一种变体。我的本地主机的IP需要添加到DB实例的授权网络中。
Windows Localhost通过TCP连接:
user:password@tcp(104.xxx.xxx.xxx:3306)/dbname