doulan3436 2016-10-13 12:18
浏览 48
已采纳

使用Go on AppEngine连接到第二代Google SQL Cloud

I've seen other posts with the same issue here however none of the answers could help in my case. I have a Google SQLCloud 2nd generation database which I'm trying to make my Go Appengine app establish a connection to. I'm using go-sql-driver/mysql

At first I tried to connect the same way I used with the 1st generation just passing the project and instance names as parameters:

sql.Open("mysql", "root@cloudsql(project:instance)/database"

Then I've noticed from the Cloud Console and also from this post that the region is also required so my connection parameters now look like:

sql.Open("mysql", "root@cloudsql(project:uscentral1:instance)/database"

and also with password

sql.Open("mysql", "root:password@cloudsql(project:uscentral1:instance)/database"

Still not working

My last attempt was to connect using SSL as described on this post using the code provided from the drivers documentation:

    rootCertPool := x509.NewCertPool()
    pem, err := ioutil.ReadFile("/path/ca-cert.pem")
    if err != nil {
        log.Fatal(err)
    }
    if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
        log.Fatal("Failed to append PEM.")
    }
    clientCert := make([]tls.Certificate, 0, 1)
    certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
    if err != nil {
        log.Fatal(err)
    }
    clientCert = append(clientCert, certs)
    mysql.RegisterTLSConfig("custom", &tls.Config{
        RootCAs: rootCertPool,
        Certificates: clientCert,
    })
    sql.Open("mysql","root:password@cloudsql(project:uscentral1:instance)/database?tls=custom")

Also no luck.

At this point I'm just running out of ideas in what the issue could be. Has anyone done anything different than what most people did in order to get it working? At this point I'm not sure if the issue is with my code, with the driver or some configuration in the CloudSQL database.

  • 写回答

2条回答 默认 最新

  • duanqi5114 2016-10-26 22:43
    关注

    Turns out my project did not have the default credentials for AppEngine. The documentation in the console says that at project creation the default Credentials for AppEngine and Compute Engine would be created by default, however this project in particular has been created a while ago through the Old App Engine console and my assumptions is that when Google migrated AppEngine projects onto the new architecture it hasn't created the default credentials as it does when we create a new project nowadays. I've deleted the old project and created a new one and everything works fine. I suppose creating the right credentials for the old project would also work. I'm leaving this answer here because I was just relying at the documentation and actually spent a lot of time with this before checking the obvious. It might be a good suggestion to the Google Cloud Doc guys to document this somewhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答