MySqlConnectionStringBuilder - Connect with Certificates

£可爱£侵袭症+ 提交于 2019-12-06 15:45:45

问题


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.

  1. Make sure you gain access to external connection using the google cloud console + you have to set a password.
  2. Export the 3 certificate files
  3. Create a new certificate using

    openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem 
                   -certfile server-ca.pem -out client.pfx
    
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!