How to connect Google Cloud SQL with C#

后端 未结 1 1039
时光说笑
时光说笑 2020-12-12 04:55

I just want to manage my database on Google Cloud using C#. I have just started learning database. P.S. I am not good at English. I hope you understand me.

相关标签:
1条回答
  • 2020-12-12 05:16

    I assume you've already created your Google Cloud SQL MySQL instance.

    Following the instructions at Connect to your Cloud SQL instance using SSL, you'll need to enable external SSL access and create a client certificate.

    You'll download the three files: server-ca.pem, client-cert.pem, client-key.pem.

    Install the MySqlConnector NuGet package into your C# application.

    Create your connection string as follows:

    var csb = new MySqlConnectionStringBuilder
    {
        Server = "Google Cloud SQL IP address",
        UserID = "Your UserName",
        Password = "Your Password",
        Database = "Your Database/Schema Name",
        SslCert = @"C:\Path\To\client-cert.pem",
        SslKey = @"C:\Path\To\client-key.pem",
        SslCa = @"C:\Path\To\server-ca.pem",
        SslMode = MySqlSslMode.VerifyCA,
    };
    
    using var connection = new MySqlConnection(csb.ConnectionString);
    connection.Open();
    

    Note that for old versions of the MySqlConnector library, you will need to combine the SSL certificate and key into one PFX file. Following these instructions, convert client-cert.pem and client-key.pem to a pfx file:

    openssl pkcs12 -inkey client-key.pem -in client-cert.pem -export -out client.pfx
    

    Then remove the Ssl* connection string options and add the following:

        CertificateFile = @"C:\Path\To\client.pfx",
        CACertificateFile = @"C:\Path\To\server-ca.pem",
    
    0 讨论(0)
提交回复
热议问题