简体   繁体   English

无法使用MySQLConnection连接到远程数据库

[英]Unable to connect with MySQLConnection to remote database

Connection made from MySQL Workbench and Visual Studio Server Explorer works just fine. 通过MySQL Workbench和Visual Studio Server Explorer建立的连接工作正常。 But when I try to use it in code trough MySQL connection with the same connection string as Server Explorer it gives me an Error. 但是,当我尝试通过与服务器资源管理器相同的连接字符串在MySQL连接中通过代码使用它时,出现错误。

"An attempt was made to access a socket in a way forbidden by its access permissions {MyDatabaseIP}:3306" “试图以一种其访问权限{MyDatabaseIP}:3306禁止的方式访问套接字”

I have tried multiple connection strings, and tried MySql connector from NuGet (many versions) and installing manually. 我尝试了多个连接字符串,并尝试使用NuGet(许多版本)的MySql连接器并手动安装。 Also tried disabling firewall and looked for answer through web. 还尝试禁用防火墙,并通过Web寻找答案。

     string myConnectionString;

        myConnectionString = "server=MyDatabaseIP;user id=admin;pwd=mYPasSword;database=MyBase;persistsecurityinfo=True";


        using (var conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString))
        {
            conn.Open();
            MessageBox.Show("Success");
        }

I've got the error message on conn.Open(); 我在conn.Open()上收到了错误消息;

Below my working server explorer configuration. 在我的工作服务器资源管理器配置下面。

Server Explorer的工作配置

server=MyDatabaseIP;user id=admin;database=MyBaser;persistsecurityinfo=True server = MyDatabaseIP;用户ID = admin; database = MyBaser; persistsecurityinfo = True

[EDIT] I've been to able to somehow closely determine what is causing the problem. [编辑]我已经能够以某种方式紧密地确定是什么导致了问题。 I have tried different machines and problem persists on windows 10 ones, my program and test script are working fine on all windows 7 and 8 machines. 我尝试了不同的机器,但问题在Windows 10机器上仍然存在,我的程序和测试脚本在所有Windows 7和8机器上都可以正常工作。 I have also tried connectivity from python script and it works!(Even on windows 10) But when I made an exe from py file it still cannot connect to server. 我也尝试过从python脚本进行连接,并且可以正常工作!(即使在Windows 10上也是如此),但是当我从py文件制作一个exe时,它仍然无法连接到服务器。 The same issue. 同样的问题。

Try it this way, 这样尝试

MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString); MySql.Data.MySqlClient.MySqlConnection conn =新的MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

Or try to include these, 或者尝试包括这些

using MySql.Data; 使用MySql.Data; using MySql.Data.MySqlClient; 使用MySql.Data.MySqlClient;

Try adding port in connection string and the make the connection - 尝试在连接字符串中添加端口并建立连接-

 var ConnectionString = "server=127.0.0.1;user id=test;password=test@123;persistsecurityinfo=True;port=3306;database=mydb;SslMode=none;"

 using (MySqlConnection con = new MySqlConnection(DatabaseServiceUrl))
 {
        con.Open();
 }

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

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