Trouble connecting to Google Cloud SQL server from

2019-07-08 10:34发布

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:

enter image description here

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:

enter image description here

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?

2条回答
Animai°情兽
2楼-- · 2019-07-08 11:02

Guys the Google provided solution to connecting to 2nd generation is here: "TLS requested but server does not support TLS" error with Google Cloud SQL (2nd generation) from Google App Engine?

Google's posted documentation was (and perhaps still is) wrong, but the correct solution is posted there. We're using 2nd generation Cloud SQL in production without issue.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-08 11:13

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.

查看更多
登录 后发表回答