首页 > 解决方案 > 无法连接到任何指定的 MySQL 主机?C# + Heroku + ClearDB

问题描述

我有一个托管在 Heroku 上的 Web API。它通过 Golang 程序提供来自 ClearDB 的数据。我有一个需要将数据插入到 ClearDB 的 C# .NET 脚本。我有这个想法在我的本地机器上工作,但是当我把所有东西都移到 heroku 上时,C# 部分停止了工作。

我已经成功连接 MySQL Workbench 8.0 和我的 Golang 程序本身。GO 程序可以成功连接、查询和提供数据。

我知道还有很多其他类似的问题,其中大多数似乎都可以通过连接字符串解决。我已经尝试了几乎所有可以将连接字符串串起来的不同方法。这是我上次尝试的(凭证已更改为随机字母):

connectionData = @"Server = eu-cd-steel-west-01.cleardb.net; Port = 3306; Database = heroku_555555555; Uid = b7039sldk253; Pwd = e3502ldks;";

当我运行这个时,我得到的错误发生了:

connection = new MySqlConnection(connectionData);
connection.Open();

错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
 ---> System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
   at MySql.Data.Common.StreamCreator.GetStream(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at DatabaseTesting.DataBaser.connect() in C:\Users\surf.user\source\repos\DatabaseTesting\DatabaseTesting\DataBaser.cs:line 38

我知道有 SSL 凭证和各种各样的东西,但我相信这并不重要,因为我不需要配置任何特殊的东西来连接工作台。要在 C# 上成功打开 ClearDB 连接,我还需要做些什么吗?

标签: c#mysqlherokucleardb

解决方案


我怀疑您在 Oracle 的 MySQL 连接器/NET 中遇到了这个已知错误:错误 97448。(我无法解析您在问题中提供的域名,但如果它解析为多个A记录,则会触发错误。)

修复方法是卸载MySql.DataNuGet 包并MySqlConnector改为安装:https ://www.nuget.org/packages/MySqlConnector/

这支持与 MySql.Data 相同的 API,因此它应该是一个替代品。


推荐阅读