Hi I am trying to connect my CloudRun to CloudSQL MySQL using public IP.
I can connect successfully with public ip but I needed to allow all networks 0.0.0.0 for CloudSQL.
I am now trying to connect this way:
public DapperContext(IConfiguration configuration)
{
// Equivalent connection string:
// "Server=<dbSocketDir>/<INSTANCE_CONNECTION_NAME>;Uid=<DB_USER>;Pwd=<DB_PASS>;Database=<DB_NAME>;Protocol=unix"
//String dbSocketDir = Environment.GetEnvironmentVariable("DB_SOCKET_PATH") ;
//String instanceConnectionName = Environment.GetEnvironmentVariable("INSTANCE_CONNECTION_NAME");
var connectionString = new MySqlConnectionStringBuilder
{
// The Cloud SQL proxy provides encryption between the proxy and instance.
SslMode = MySqlSslMode.None,
// Remember - storing secrets in plain text is potentially unsafe. Consider using
// something like https://cloud.google.com/secret-manager/docs/overview to help keep
// secrets secret.
Server = "/cloudsql/master-xxxxx-12341234:asia-southeast2:mysql-1",
//Server = $"{dbSocketDir}/{instanceConnectionName}",
UserID = "root", // e.g. 'my-db-user
Password = "123456", // e.g. 'my-db-password'
Database = "db-name", // e.g. 'my-database'
ConnectionProtocol = MySqlConnectionProtocol.UnixSocket,
Pooling = true
};
// Specify additional properties here.
_connectionString = connectionString.ConnectionString;
}
public IDbConnection CreateConnection() => new MySqlConnection(_connectionString);
However I am getting "Unknown socket error" and from CloudSQL logs: "[MY-010914] [Server] Got an error reading communication packets"
2
Answers
First thing first, your Google cloud should not have a public IP (in your scenario) since they can talk to each other privately.
Now with regards to your request there are some options:
1-Within the Sql screen check Public IP and on the New network, provide a name and most important a value of 0.0.0.0/0 Notice the warning
2-Connect using the Cloud SQL Auth proxy
Using the Cloud SQL Auth proxy is the recommended method for connecting to a Cloud SQL instance. The Cloud SQL Auth proxy:
That said going directly to a response.
Notice: Be sure to Enable Cloud Sql Admin api, more info here
1-Check your cloud run configuration, on the command line make the following replacements.
IMAGE with the image you are deploying
SERVICE_NAME with the name of your Cloud Run service
INSTANCE_CONNECTION_NAME with the instance connection
name of your Cloud SQL instance, or a comma delimited
list of connection names.
gcloud run deploy
–image=IMAGE
–add-cloudsql-instances=INSTANCE_CONNECTION_NAME
2-Connect to CloudSQL
For public IP paths, Cloud Run provides encryption and connects using the Cloud SQL Auth proxy through Unix sockets.
Once correctly configured, you can connect your service to your Cloud SQL instance’s Unix domain socket accessed on the environment’s filesystem at the following path: /cloudsql/INSTANCE_CONNECTION_NAME.
The INSTANCE_CONNECTION_NAME uses the format project:region:instance-id. You can find it on the Overview page for your instance in the Google Cloud console or by running the following command:
gcloud sql instances describe [INSTANCE_NAME]
The code sample shown below is an extract from more complex examples located here
After 2 days of searching the problem was the version of MySQL.Data, the version that worked for me is 8.0.23, for more info https://bugs.mysql.com/bug.php?id=104685