'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
Solution 1:[1]
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'@'%';
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.
Solution 2:[2]
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.
Solution 3:[3]
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
Solution 4:[4]
1-Un check all Firewall and Network protection settings 2-Go in database management system running icon like wamp green icon 3-Go to Mysql CMD 3a-
CREATE USER 'muzamil'@'%' identified by 'muzamil';
grant all privileges on *.* to 'muzamil'@'%';
flush privileges;
These three commands run one by one 4-Windows Defender Firewall then Click on Advance settings ==> Inbound Rules ->Enable riles Remote Desktop TCP All Rules
5: C:\wamp64\alias open this File then change local to ==> Require all granted ==> Allow from all
Your remote server will work with c# application
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mihai |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Muzamil Ali |
