无法从部署的应用连接到Google Cloud SQL服务器 [英] Trouble connecting to Google Cloud SQL server from deployed app

查看:174
本文介绍了无法从部署的应用连接到Google Cloud SQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Cloud SQL实例(第二代),并且在授权我自己的IP后,我可以在本地成功连接到它。我有Go代码成功地从表中检索行,我有代码成功地将行添加到表。但是,当我部署我的应用程序时,该应用程序无法连接到服务器。每当调用SQL函数时,服务器都会返回500响应。我的应用程序被授权连接到这个sql实例:





  func dialSQL()(* sql.DB,error){
如果appengine.IsDevAppServer(){
返回sql.Open(mysql,root:password @ tcp(xxx.xxx.xxx.xxx:3306)/ Dbname)
} else {
return sql.Open(mysql,root @ cloudsql(projectid:regionname:instancename)/ Dbname)
}
}

部署的版本和本地版本使用相同的代码库,但部署的版本使用 dialSQL中的第二个连接字符串)函数。



我使用这个Go MySQL驱动程序:

最后,我转到Google Cloud Console的计算引擎部分,正在为我的应用程序的最新实例提供服务的VM。我成功调整了云SQL服务器的IP,但是 telnet xxx.xxx.xxx.xxx 3306 超时。我还运行了 lsof -i TCP:3306 lsof -i | grep 3306 但没有进程出现。我也在这个盒子上安装了mysql,并尝试从那里连接到服务器,但连接失败。



有人知道可能会导致此问题吗?

解决方案

看起来可能有一个问题 Go MySQL当部署的Google App Engine应用程序尝试连接到第二代 Google云端SQL Server时,驱动程序

我创建了一个 第一代 Cloud SQL实例,并且能够使用已部署的Google App Engine应用程序使用此连接字符串成功连接到服务器:



user @ cloudsql(project-id:instance-name)/ dbname



不需要地区名称。


I am using a Google Cloud SQL instance (second generation) and I can successfully connect to it locally after authorizing my own IP. I have Go code that successfully retrieves rows from tables and I have code that successfully adds rows to tables. However, when I deploy my app, the app is unable to connect to the server. Any time an SQL function is called, the server returns a 500 response. My app is authorized to connect to this sql instance:

I am using the following Go connection code:

func dialSQL() (*sql.DB, error) {
    if appengine.IsDevAppServer() {
        return sql.Open("mysql", "root:password@tcp(xxx.xxx.xxx.xxx:3306)/Dbname")
    } else {
        return sql.Open("mysql", "root@cloudsql(projectid:regionname:instancename)/Dbname")
    }
}

The deployed version and the local version are using the same codebase, but the deployed version uses the second connection string in the dialSQL() function.

I am using this Go MySQL driver: https://github.com/go-sql-driver/mysql

The documentation for this driver mentions the following connection string for connecting to a Google Cloud SQL server via Google App Engine: user@cloudsql(project-id:instance-name)/dbname

I am using projectid:regionname:instancename rather than projectid:instancename because this post mentions the need to include the regionname: google app engine golang, driver: bad connection

Confusingly, Google's documentation also omits the regionname: https://cloud.google.com/appengine/docs/go/cloud-sql/reference

I have tried both variants of the connection string.

According to this post, the deployed app will attempt to connect to the server using host = localhost, and according to this post, the password for 'root'@'localhost' must be null.

I made the following query in my mysql console to verify that the user 'root'@'localhost' indeed has a null password: select User, Host, HEX(authentication_string) from mysql.user. The 'root'@'%' user still has the password I specified in the Google Cloud SQL instance console.

I looked into a possible issue regarding the region of the GAE app and the Cloud SQL server, but they are in the same region. I also learned that for second generation Google Cloud SQL servers the region of the app and the server are allowed to differ.

I also created a new user and tried to connect to the instance with the new user's username in the connection string. This didn't fix the error.

I also tried adding a firewall rule, but this didn't fix the problem either:

Finally, I went to the Compute Engine section of the Google Cloud Console and SSHed into the VM which is serving the latest instance of my app. I successfully pinged the Cloud SQL server's IP, but telnet xxx.xxx.xxx.xxx 3306 timed out. I also ran lsof -i TCP:3306 and lsof -i | grep 3306 but no processes appeared. I also installed mysql on this box and tried to connect to the server from there, but the connection failed.

Does anyone know what might be causing this issue?

解决方案

It looks like there could be an issue with the Go MySQL driver when a deployed Google App Engine application tries to connect to a second generation Google Cloud SQL Server.

I created a first generation Cloud SQL instance and I was able to successfully connect to the server using a deployed Google App Engine application with this connection string:

user@cloudsql(project-id:instance-name)/dbname

No region name was required.

这篇关于无法从部署的应用连接到Google Cloud SQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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