从App Engine连接到CloudSQL(第二代CloudSQL)转到 [英] Connecting to CloudSQL from App Engine (Second Generation CloudSQL) GO

查看:151
本文介绍了从App Engine连接到CloudSQL(第二代CloudSQL)转到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

爱堆栈,我的第一篇文章完全无奈。
$ b

  • 创建App Engine项目

  • 在我的应用程序中创建第二代MySQL实例Engine项目

  • 在MySQL实例中创建数据库

  • 在App Engine中,我激活 - > Google Cloud Shell< - 。 (我在我的console.cloud.google.com命令提示符下工作)



我已经复制了这个基本的GO程序来连接到我的MySQL实例。



我构建并运行它。
go build mysqlexample.go
./mysqlexample



我无法实现成功的连接。你可以看到我尝试过的所有连接字符串,并且它们的右边是我得到的响应。



我可以使用mysql管理员从本地windows机器连接。

帮助?

 包主

导入(
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
$ b const const dbOpenString = dbUserName +:+ dbPassword +@ /+ dbInstanceName +/+ dbName // GETS RESPONSE网络默认地址'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:连接超时

//从堆栈溢出中获得此值。 GoDocs不会更新以反映第二代数据库。
// http://stackoverflow.com/questions/38890022/tls-requested-but-server-does-not-support-tls-error-with-google-cloud-sql-2nd
/ / user:password @ cloudsql(copiedPastedInstanceConnectionName)/ d atabaseName?charset = charset& collat​​ion = co llation& tls = tlsConfi gName& parseTime = true
//第一代连接字符串
/ / username:password @ cloudsql(appID:CloudSQLInstance)/ databasename?parseTime = true& loc = UTC

db,err:= sql.Open(mysql,dbOpenString);
defer db.Close()

log.Println(试图Ping数据库....)

err = db.Ping()

if err!= nil {
log.Println(db.Ping()failed:+ dbOpenString)
log.Println(err)
} else {
log.Println(成功!)
}

}




App Engine标准版:

  user:密码@ cloudsql(INSTANCE_CONNECTION_NAME)/ dbname 




App Engine灵活:

  user: password @ unix(/ cloudsql / INSTANCE_CONNECTION_NAME)/ dbname 


Love the Stack, My first post out of complete frustration. Thanks for you comments!

  • Created App Engine Project
  • Created Second Generation MySQL Instance in my App Engine Project
  • Created Database in the MySQL Instance
  • In App Engine, I activate the --> Google Cloud Shell <--. ( I am working at a command prompt in my console.cloud.google.com)

I have copied this basic GO program to connect to my MySQL instance.

I build it and run it. go build mysqlexample.go ./mysqlexample

I have not been able to achieve a successful connection. You can see all the various connection strings that I have tried and to the right of them is the response I get.

I can connect from my local windows machine using mysql admin.

Help?

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)/d‌​atabaseName?charset=‌​charset&collation=co‌​llation&tls=tlsConfi‌​gName&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!")
    }

}

解决方案

The following are the correct connection strings, but they differ depending on which version of App Engine you are connecting from.

App Engine Standard:

user:password@cloudsql(INSTANCE_CONNECTION_NAME)/dbname

App Engine Flexible:

user:password@unix(/cloudsql/INSTANCE_CONNECTION_NAME)/dbname

这篇关于从App Engine连接到CloudSQL(第二代CloudSQL)转到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆