Reputation: 98
I'm creating a system in C# and MySQL, but I keep getting this frustrating error, "Unable to connect to any of the specified MySQL hosts". I've tried many different things but nothing has worked, could someone PLEASE try to help me?
public bool tryLogin(string username, string password)
{
MySqlConnection con = new MySqlConnection("host=hostremoved;user=user_removed;password=passwordremoved;database=databaseremoved;");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM login WHERE user_name = '" + username + "' AND user_pass = '" + password + "';");
cmd.Connection = con;
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() != false)
{
if (reader.IsDBNull(0) == true)
{
cmd.Connection.Close();
reader.Dispose();
cmd.Dispose();
return false;
}
else
{
cmd.Connection.Close();
reader.Dispose();
cmd.Dispose();
return true;
}
}
else
{
return false;
}
}
private void btnlogin_Click(object sender, EventArgs e)
{
if (tryLogin(txtuser.Text, txtpass.Text) == true)
{
MessageBox.Show("Login worked!");
}
else
{
MessageBox.Show("Login failed!");
Upvotes: 0
Views: 13735
Reputation: 98
Thanks for the help everyone! I had to whitelist my IP on my firewall and it worked!
Upvotes: 0
Reputation: 262
If it is not the firewall, and the database is setup to listen for remote connections, then you should check that your user credentials are configured so that the specified database will accept a connection from the given IP. If the user can only connect from localhost, then you'll need to login as root and issue a GRANT statement, like:
GRANT ALL ON yourDbName.* TO 'yourUser'@'yourIP' IDENTIFIED BY "yourPassword";
For more on using GRANT, see:
http://dev.mysql.com/doc/refman/5.1/en/grant.html
Upvotes: 1
Reputation: 3306
Is it a firewall issue? Check that the machine the MySql instance is running on is able to accept connection through the firewall.
Upvotes: 0