簡體   English   中英

無法從C#連接到MySQL,但可以從其他應用程序連接到MySQL

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

我想在我的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 (...)

可在同一台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()

張貼的連接字符串沒有發現任何問題,但是如果您是復制的場景(我的意思是存在數據庫復制),那么您還必須指定復制的服務器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