简体   繁体   English

无法使用 webapi 连接到 mysql 数据库

[英]can't connect to mysql database with webapi

So I have zyxel server in my home.所以我家里有zyxel服务器。 I made mySQL database using phpmyadmin.我使用 phpmyadmin 创建了 mySQL 数据库。 And I cannot reach this database using asp.net core webAPI.而且我无法使用 asp.net core webAPI 访问此数据库。

I got System.Data.SqlClient.SqlException on my IRepository's method.我在 IRepository 的方法上得到了 System.Data.SqlClient.SqlException。 Of course this is because the method is trying to get database data.当然这是因为该方法试图获取数据库数据。

Connection string looks like this:连接字符串如下所示:

"ConnectionStrings": {
  "DefaultConnection": "Server=(xxx.xxx.x.xx);Database=sensor;User=temp;Password=pwd;Trusted_Connection=True;MultipleActiveResultSets=true"
}

What am I doing wrong?我究竟做错了什么? I also have problems with connecting same db with python.我也有用 python 连接同一个数据库的问题。

Issue 1第 1 期

That connection string appears to be for SQL server.该连接字符串似乎用于 SQL 服务器。 You need a MySql-specific connection string.您需要一个特定于 MySql 的连接字符串。 I've always found ConnectionStrings.com to be helpful in situations like this.我一直发现ConnectionStrings.com在这种情况下很有帮助。

Here's the MySQL template that they provide:这是他们提供的 MySQL 模板:

Standard标准

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;服务器=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Specifying TCP port指定 TCP 端口

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;服务器=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername; Pwd=myPassword;密码=我的密码;

Multiple servers多台服务器

Use this to connect to a server in a replicated server configuration without concern on which server to use.使用它连接到复制服务器配置中的服务器,而无需考虑使用哪个服务器。

Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;服务器=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase; Uid=myUsername;Pwd=myPassword; uid=myUsername;Pwd=myPassword;

See https://www.connectionstrings.com/mysql/ for additional info.有关其他信息,请参阅https://www.connectionstrings.com/mysql/

Issue 2第二期

Based on the error message that you provided, you're using a SQL data provider instead of a MySQL connector.根据您提供的错误消息,您使用的是 SQL 数据提供程序而不是 MySQL 连接器。 Bottom line, you need a MySQL connector.最重要的是,您需要一个 MySQL 连接器。 You can download that here .你可以在这里下载。

See this post for additional information: System.Data.SqlClient Namespace for MySQL?有关其他信息,请参阅此帖子: MySQL 的 System.Data.SqlClient 命名空间?

Solved my problem by using Pomelo's library .通过使用Pomelo 的库解决了我的问题。 Docs library didn't seem to work.文档库似乎不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM