[英]how to make .net core running on an ubuntu machine connect with the local database
I have a .net core program running on ubuntu, and I need to add a connection to the database running on the same machine. 我有一个在ubuntu上运行的.net核心程序,我需要向在同一台计算机上运行的数据库添加连接。
I created a test program on my windows machine which connected to and interacted with the database and tables I want perfectly (via a putty tunnel), but used MySql.Data.MySqlClient
, which it would seems is windows specific. 我在Windows机器上创建了一个测试程序,该程序与我想要的数据库和表完美连接(并通过腻子隧道)并与之交互,但是使用了MySql.Data.MySqlClient
,这似乎是Windows特有的。 I copied the connection.cs
code up to the ubuntu server (changed the namespace, referenced it it the main program etc.). 我将connection.cs
代码复制到ubuntu服务器(更改了名称空间,在主程序中引用了它,等等)。 When i try to build, it can't find MySql.Data.MySqlClient
(type or namespace error on mysql). 当我尝试构建时,找不到MySql.Data.MySqlClient
(mysql上的类型或名称空间错误)。
Is getting it working a matter of a) loading MySql.Data.MySqlClient
into the ubuntu machine, b) using a different package (if so does anyone know what) or c) doing it a totally different way. 使它工作的问题是:a)将MySql.Data.MySqlClient
加载到ubuntu机器中; b)使用其他程序包(如果有人知道的话);或c)完全不同的方式。
I have tried looking at tutorials/ other SO posts to see their code but none of them are about connecting to a normal db hosted on the same machine. 我尝试查看教程/其他SO帖子以查看其代码,但是它们均与连接到同一台计算机上托管的普通db有关。
Shamelessly copypasta'd from https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-connection-string.html 从https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-connection-string.html无耻地复制
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=root;" +
"pwd=12345;database=test";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
You do need to install MySQLClient on your ubuntu machine if it's not there already. 您确实需要在ubuntu计算机上安装MySQLClient(如果尚未安装)。 Just select it in your package manager. 只需在包管理器中选择它即可。
Also, if this doesn't work, verify that mysql is actually listening on a TCP port. 另外,如果这不起作用,请确认mysql实际上正在侦听TCP端口。 It's possible that you're attempting a TCP connection but its actually listening on a socket. 您可能正在尝试进行TCP连接,但实际上是在套接字上监听。
https://dev.mysql.com/doc/refman/8.0/en/can-not-connect-to-server.html https://dev.mysql.com/doc/refman/8.0/zh-CN/can-not-connect-to-server.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.