简体   繁体   中英

Unable to connect to any specified mysql C#

I can't connect to my sql server, i tried some fixes from stackoverflow and google and it didn't help me. Thanks.

  connString = "SERVER ='''myserverip''';PORT=3306;DATABASE=mydatabase;UID=myuser;PASSWORD=mypassword";
        try
         {
             conn = new MySqlConnection();
             conn.ConnectionString = connString;
             conn.Open();
             MessageBox.Show("Connection success");

         }
         catch (MySql.Data.MySqlClient.MySqlException ex)
         {
             MessageBox.Show(ex.Message);
         }

To configure myuser I used this on my linux vps.

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypassword'; GRANT ALL ON *.* TO 'myuser'@'localhost'; GRANT ALL ON *.* TO 'myuser'@'%';

i tried : Unable to connect to any of the specified mysql hosts. C# MySQL ( i tried to use MySqlConnectionStringBuilder, don't specify the port, instead of password in connection string i typed psw); Disable my pc firewall, disable linux server firewall

MySqlConnectionStringBuilder does enable you to specify port. Just tested, this works just fine:

MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = "192.168.1.105";
csb.Port = 3307;
csb.Database = "test";
csb.UserID = "me";
csb.Password = "mypassword";

cn = new MySqlConnection(csb.ToString());
cn.Open();

Are you quite sure your MySql server actually accepts incoming connections over TCP? By default that is disabled.

So, after searching on google how to setup sql connection in linux, and trying different setups hardly I fixed it so I want to share with stackoverflow how I fixed it. Thanks for help @Avo Nappo .

First you need to comment out the line #bind-address from your sql config. The accepted answer here : MySQL root access from all hosts .

Then I create a user using this command CREATE USER 'golden'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON * . * TO 'golden'@'%'; CREATE USER 'golden'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON * . * TO 'golden'@'%'; And in c# I used my default connection string that is in the question. I don't know why but MySqlConnectionStringBuilder dose not work on my pc. I hope this will help someone. Have a nice day and keep coding.

You must go in your Remote MySQL in your C-Pannel and Add Access Host first and change this code

connString = "SERVER ='''myserverip''';PORT=3306;DATABASE=mydatabase;UID=myuser;PASSWORD=mypassword";

to

connString = "SERVER =*Put here your Hostname with Port*;DATABASE=mydatabase;UID=myuser;PASSWORD=mypassword;";

Or use this Method

MySqlConnectionStringBuilder ConStD = new MySqlConnectionStringBuilder();

ConStD.Server = "Hostname that get from Manage Access Hosts";
ConStD.Port = Port that get from Manage Access Hosts;
ConStD.Database = "YourDatabaseName";
ConStD.UserID = "yourUsername";
ConStD.Password = "yourpassword";

try
{
    MySqlConnection conn = new MySqlConnection(ConStD.ToString());
    conn.Open();
    MessageBox.Show("connection Success");
}
catch (MySqlException ex)
{
    MessageBox.Show(ex.Message);
}

It's Must working For You

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