无法从C#连接到MySQL,但可以从其他应用程序连接到MySQL

时间:2019-06-25 10:33:42

标签: c# python mysql .net mysql-connector

我想在我的C#应用​​程序中更改MySQL服务器。该应用程序在我的公司中运行,并且旧的和新的MySQL服务器都只能从Intranet访问。问题是我可以从HeidiSQL软件和python代码连接,但不能从C#...

连接

例如,我尝试了所有可能的解决方案。禁用防火墙,使用来自NuGet的不同软件包,修改连接字符串,我创建了新的控制台项目,仅粘贴了各种MySQL连接代码-始终存在相同的错误

Message: Unable to connect to any of the specified MySQL hosts.
Source: MySql.Data
Number: 1042

我”,使用.net Framework 4.5.2(在我的测试项目中为4.6.1),而MySQL Server为“ 5.6.44-log-MySQL Community Server(GPL)”


我测试过的C#连接代码示例之一不起作用

using MySql.Data.MySqlClient;
public static MySqlConnection DB_connection;

DB_connection = new MySqlConnection(@"Server=MyIP;Database=myDB;Uid=my_user;Pwd=pass;");
try
{
    DB_connection.Open();
    isConn = true;
}
catch (...)

有效的python代码在同一台PC上运行

import pymysql
import pprint

connection = pymysql.connect(host='MyIP',
                             user='my_user',
                             password='pass',
                             db='myDB')

try:
    with connection.cursor() as cursor:
        sql = "select * from table;"
        cursor.execute(sql)
        # connection.commit()
        result = cursor.fetchall()
        pprint.pprint(result)
finally:
    connection.close()

1 个答案:

答案 0 :(得分:0)

看不到您发布的连接字符串有任何问题,但是如果您是复制场景(我是说存在数据库复制),那么您将必须指定复制的服务器IP /主机名

Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;