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?