简体   繁体   中英

Unable to connect with MySQLConnection to remote database

Connection made from MySQL Workbench and Visual Studio Server Explorer works just fine. 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.

"An attempt was made to access a socket in a way forbidden by its access permissions {MyDatabaseIP}:3306"

I have tried multiple connection strings, and tried MySql connector from NuGet (many versions) and installing manually. Also tried disabling firewall and looked for answer through 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();

Below my working server explorer configuration.

Server Explorer的工作配置

server=MyDatabaseIP;user 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. 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. The same issue.

Try it this way,

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

Or try to include these,

using MySql.Data; using 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();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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