简体   繁体   English

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

[英]Unable connect to MySQL from C# but possible from other apps

I wanted to change MySQL server in my C# app. 我想在我的C#应用​​程序中更改MySQL服务器。 The app is run in my company and both - old and new MySQL servers are only accessible from the intranet. 该应用程序在我的公司中运行,并且旧的和新的MySQL服务器都只能从Intranet访问。 The problem is I can connect from HeidiSQL software and from python code but not from C#... 问题是我可以从HeidiSQL软件和python代码连接但不能从C#连接...

I have tried all possible solutions what I found, for ex. 我已经尝试了所有可能的解决方案,例如。 disabling firewall, using different packages from NuGet, modifying connection string, I created new console project only to paste various MySQL connection code - always with same error 禁用防火墙,使用来自NuGet的不同程序包,修改连接字符串,我创建了新的控制台项目,仅粘贴了各种MySQL连接代码-始终出现相同的错误

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

I', using .net Framework 4.5.2 (4.6.1 in my test project) and MySQL Server is '5.6.44-log - MySQL Community Server (GPL)' 我使用的是.net Framework 4.5.2(在我的测试项目中为4.6.1),MySQL Server为“ 5.6.44-log-MySQL Community Server(GPL)”


One of C# connection code example that I have tested that is NOT working 我测试过的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 (...)

Working python code run from the same PC 可在同一台PC上运行的有效python代码

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()

Don't see anything wrong with your posted connection string but in case your's is a replicated scenario (I mean DB replication exists) then you will have to specify the replicated server IP/hostname as well like 张贴的连接字符串没有发现任何问题,但是如果您是复制的场景(我的意思是存在数据库复制),那么您还必须指定复制的服务器IP /主机名,例如

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

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

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