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.
Asked
Active
Viewed 8,366 times
1 Answers
9
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",

Bradley Grainger
- 27,458
- 4
- 91
- 108
-
Thank you so much ! – user10391265 Sep 22 '18 at 17:47
-
How do i connect if there api is on Google Cloud, and not use ssl, do i just skip the Certificate rows? @Bradley Grainger – Jonas Feb 06 '19 at 09:23
-
If your application is hosted in Google Cloud, you may want to use the Cloud SQL Proxy: https://cloud.google.com/sql/docs/mysql/connect-admin-proxy – Bradley Grainger Feb 06 '19 at 22:57
-
@BradleyGrainger if i were to use the connecting through public IP, thats mean i have to assign IP in the console every time a new user PC runs my C# app? is there any workaround where new user can connect to the google cloud sql more conveniently? – Nov 13 '19 at 05:51
-
1@John Exposing your database directly to end users is extremely dangerous. The typical approach for handling this is having a web service that authenticates client devices and performs database operations for users through an API. – Bradley Grainger Nov 13 '19 at 17:01
-
@BradleyGrainger can google cloud sql database be used with c# desktop app? because i dont see c# language in the official doc – Nov 14 '19 at 03:07
-
1@John Yes, there is no problem connecting to a Google Cloud SQL instance from a C# desktop application or C# web API (running on Google App Engine, or running on a server you manage); I've successfully done all of those. – Bradley Grainger Nov 14 '19 at 06:49