简体   繁体   English

如何使运行在ubuntu机器上的.net核心与本地数据库连接

[英]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.

相关问题 如何在 ubuntu 桌面上使用 Entity Framework Core 将 asp.net core MVC 项目连接到本地数据库 - how to connect asp.net core MVC project to local DB using Entity Framework Core on ubuntu desktop 如何正确地为在 systemd 上运行的 Ubuntu 制作无尽的控制台应用程序 .NET Core 3.1 - How to make correctly endless console app .NET Core 3.1 for Ubuntu running on systemd 如何在.NET Core中连接到不同类型的数据库 - How to connect to different type of database in .NET Core 如何将.Net Web服务与本地数据库连接 - How to connect .Net Webservice with local database 如何保护ado.net实体在本地计算机上生成的数据库 - How to protect ado.net entity generated database on a local machine 在 .NET Core 中,如何向在本地服务器上运行的 API 发出 HTTP 请求(IIS Express 使用 VS 2019)? - In .NET Core, how do you make HTTP request to an API running on local server (IIS Express using VS 2019)? 如何与本地数据库连接 - How to connect with local database 如何从 .Net Core 连接到 Oracle 数据库连接 - How to connect to an Oracle database Connection from .Net Core 如何使用.NET Core CLR连接到Postgresql数据库 - How to connect to a Postgresql database using .NET Core CLR 如何将 .NET Core ASP 应用程序连接到 MS Access 数据库 - How to Connect .NET Core ASP Application to MS Access database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM