skip to Main Content

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


  1. 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

    enter image description here

    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:

    • Works with both public and private IP endpoints
    • Validates connections using credentials for a user or service account
    • Wraps the connection in a SSL/TLS layer that’s authorized for a Cloud SQL instance

    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

    using MySql.Data.MySqlClient;
    using System;
    
    namespace CloudSql
    {
        public class MySqlUnix
        {
            public static MySqlConnectionStringBuilder NewMysqlUnixSocketConnectionString()
            {
                // Equivalent connection string:
                // "Server=<INSTANCE_UNIX_SOCKET>;Uid=<DB_USER>;Pwd=<DB_PASS>;Database=<DB_NAME>;Protocol=unix"
                var connectionString = new MySqlConnectionStringBuilder()
                {
                    // The Cloud SQL proxy provides encryption between the proxy and instance.
                    SslMode = MySqlSslMode.None,
    
                    // Note: Saving credentials in environment variables is convenient, but not
                    // secure - consider a more secure solution such as
                    // Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
                    // keep secrets safe.
                    Server = Environment.GetEnvironmentVariable("INSTANCE_UNIX_SOCKET"), // e.g. '/cloudsql/project:region:instance'
                    UserID = Environment.GetEnvironmentVariable("DB_USER"),   // e.g. 'my-db-user
                    Password = Environment.GetEnvironmentVariable("DB_PASS"), // e.g. 'my-db-password'
                    Database = Environment.GetEnvironmentVariable("DB_NAME"), // e.g. 'my-database'
                    ConnectionProtocol = MySqlConnectionProtocol.UnixSocket
                };
                connectionString.Pooling = true;
                // Specify additional properties here.
                return connectionString;
            }
        }
    }
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search