C# 无法连接到任何指定的 MySQL 主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10618720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:32:14  来源:igfitidea点击:

unable to connect to any of the specified MySQL hosts

c#mysql

提问by Nikolay Dyankov

I am trying to create a login form connected to a MySQL database. I installed the sql connector inserted in the form but when I try to connect I get error unable to connect to any of the specified MySQL hosts. Here is my code:

我正在尝试创建一个连接到 MySQL 数据库的登录表单。我安装了插入表单中的 sql 连接器,但是当我尝试连接时,出现错误,无法连接到任何指定的 MySQL 主机。这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ECBSRecruitmentAgencySoftware
{
    public partial class LogIn : Form
    {
             public LogIn()
        {
            InitializeComponent();
        }

             public bool tryLogin(string username, string password)
             {
                 MySqlConnection con = new MySqlConnection("host=think-tek.net;user=ctutorial;password=chang3d;database=ctutorial_logintest;");
                 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 button1_Click(object sender, EventArgs e)
        {

            if (tryLogin(textBox1.Text, textBox2.Text) == true)
            {
                MainScreen F2 = new MainScreen();
                F2.Show();
                this.Hide();
            }

             else MessageBox.Show("Wrong details!");

        }




}
}

回答by Romil Kumar Jain

Try the connection string in given format in the sample:

尝试示例中给定格式的连接字符串:

public bool tryLogin(string username, string password)
             {
                 MySqlConnection con = new MySqlConnection("SERVER=localhost;" +
                    "DATABASE=mydatabase;" +
                    "UID=testuser;" +
                    "PASSWORD=testpassword;");
                 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;
                 }
             }          

回答by Robert H

My connection string for MySQL is:

我的 MySQL 连接字符串是:

string mySqlConn = "server=localhost;user=username;database=databasename;port=3306;password=password;";

What exception is thrown for your connection string? There should be a error number.

您的连接字符串抛出了什么异常?应该有错误编号。

回答by Splatbang

This siteis pretty helpful in terms of connection strings. Your connection string seems to be invalid.

该站点在连接字符串方面非常有用。您的连接字符串似乎无效。

Also: Make sure your user has the proper access privileges. Many hosting providers only enable access from localhost. You may have to request that they enable your user for remote access.

另外:确保您的用户具有适当的访问权限。许多托管服务提供商只允许从本地主机访问。您可能需要请求他们为您的用户启用远程访问。

回答by Sylca

I had the same problem and error was in connection string! Somehow, I declare it twice to two different locations. Once to my localhost and second time to server machine database. When I published the project on client machine it was impossible to connect to localhost(what was my computer).:-) and I was wondering how do I get this: "unable to connect to any of specified mysql hosts"

我遇到了同样的问题,错误出现在连接字符串中!不知何故,我向两个不同的位置声明了两次。一次到我的本地主机,第二次到服务器机器数据库。当我在客户端机器上发布项目时,无法连接到本地主机(我的电脑是什么)。:-) 我想知道如何得到这个:“无法连接到任何指定的 mysql 主机”

this is how man can make his life complicated for days!:-)

这就是人类可以使他的生活复杂几天的方式!:-)

of course, i was able to connect to db on server machine.

当然,我能够连接到服务器机器上的数据库。

So, just for example, this was my problem. And, solution was to declare to it once and to one database!!!

所以,举个例子,这是我的问题。并且,解决方案是向它声明一次和一个数据库!!!

And also, I would like to share my connection string that worked just fine:

而且,我想分享我的工作正常的连接字符串:

cs = @"server=xxx.xxx.xxx.xxx;uid=xxxx;password=xxxxxxxxx;database=xxxxx;port=3306";

cs = @"server=xxx.xxx.xxx.xxx;uid=xxxx;password=xxxxxxxxx;database=xxxxx;port=3306";

回答by Adiii

your string connection is right there is no error,but fist check it on local server this is exception only raise your xampp is not running, first start xampp go to browser and type localhost,if its working you will see xampp menu if it open localhost then try to connect whatever your server is

您的字符串连接是正确的,没有错误,但是首先在本地服务器上检查它,这是异常只会引发您的 xampp 未运行,首先启动 xampp 转到浏览器并键入 localhost,如果它工作正常,如果打开 localhost,您将看到 xampp 菜单然后尝试连接您的服务器

string connection = "server=localhost;database=student_record_database;user=root;password=;";
            MySqlConnection con = new MySqlConnection(connection);

回答by Ritesh Roushan

public bool tryLogin(string username, string password)
{
    MySqlConnection con = new MySqlConnection("SERVER=xx.xx.xx.xx;DATABASE=dbname;UID=user;PASSWORD=password;CheckParameters=False;");
    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;
    }
}