问题描述
我只想使用C#在Google Cloud上管理我的数据库.我刚刚开始学习数据库. P.S.我不会英语.我希望你能理解我.
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.
推荐答案
我假设您已经创建了Google Cloud SQL MySQL实例.
I assume you've already created your Google Cloud SQL MySQL instance.
按照以下说明进行操作:连接到您的Cloud SQL实例使用SSL ,您需要启用外部SSL访问并创建客户端证书.
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.
您将下载三个文件:server-ca.pem,client-cert.pem,client-key.pem.
You'll download the three files: server-ca.pem, client-cert.pem, client-key.pem.
按照这些说明,将客户端转换为cert.pem和client-key.pem转换为pfx文件:
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
将 MySqlConnector NuGet程序包安装到C#应用程序中.
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",
CertificateFile = @"C:\Path\To\client.pfx",
CACertificateFile = @"C:\Path\To\server-ca.pem",
SslMode = MySqlSslMode.VerifyCA,
};
using (var connection = new MySqlConnection(csb.ConnectionString))
{
connection.Open();
}
这篇关于如何将Google Cloud SQL与C#连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!