爱堆栈,我的第一篇文章完全沮丧。谢谢你的评论!
我已经复制了这个基本的GO程序来连接我的MySQL实例。
我构建并运行它。 去构建mysqlexample.go ./mysqlexample
我无法成功建立连接。你可以看到我尝试过的所有各种连接字符串,右边是我得到的响应。
我可以使用mysql admin从我的本地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!")
}
}
答案 0 :(得分:2)
https://cloud.google.com/appengine/docs/flexible/go/using-cloud-sql
对于新人来说,App Engine和CloudSQL只是编写最简单的GO程序来连接和与您的CloudSQL第二代数据库进行通信是令人沮丧的!
您可以选择制作App Eng或App Eng Flex,SQL 1st或2nd Gen ....根据组合连接字符串的不同而不同。当您搜索时,所有Google的文档都会将您带到第一代SQL和App Engine而不使用flex,因为这主要是在生产中。如果您这样做,请确保您正在阅读Flex文档。如果这样做,请确保您正在阅读第二代文档。有时它们是完全不同的文档,有时文档堆叠在页面上,你必须转到底部才能看到第二代sql和app eng flex的新内容。
CloudShell很棘手,我仍然无法编译GO并在此处与SQL 2进行通信。我正在成功地与已部署的应用程序引擎flex进行云sql第2代谈话。在SQL PROXY RUNNING中,您必须使用SQL PROXY。你必须通过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")
}
}
答案 1 :(得分:2)
如果要迁移到第二代Go App Engine(Golang 1.11),请从以下位置更改连接字符串:
user:password@cloudsql(instanceID)/db
到
user:password@unix(/cloudsql/instanceID)/db
答案 2 :(得分:0)
遇到了连接到云SQL实例的同样问题。我在windows localhost环境下工作的一个变种如下。我的localhost的IP需要添加到db实例的授权网络中。
Windows localhost通过tcp连接:
user:password@tcp(104.xxx.xxx.xxx:3306)/dbname