问题
Iam trying to connect to Google Cloud Sql which is a MySql solution. I was able to connect using MySql Workbench.
How can i connect in c# using the MySqlConnectionStringBuilder
?
I found no way to provide the three certificate(s).
回答1:
I found a solution.
- Make sure you gain access to external connection using the google cloud console + you have to set a password.
- Export the 3 certificate files
Create a new certificate using
openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem -certfile server-ca.pem -out client.pfx
Source Code
var connectionStringBuilder = new MySqlConnectionStringBuilder { Server = "<Instance_Ip>", UserID = "root", Password = "<Password>", Database = "<Database_Name>", CertificateFile = @"<Path_To_The_File>\client.pfx", CertificatePassword = "<Password_For_The_Cert>" }; using (var conn = new MySqlConnection(connectionStringBuilder.ToString())) using (var cmd = conn.CreateCommand()) { cmd.CommandText = string.Format("SELECT * FROM test"); conn.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { var data = reader.GetString(0); Console.WriteLine(data); } }
来源:https://stackoverflow.com/questions/39031412/mysqlconnectionstringbuilder-connect-with-certificates