无法连接到任何指定的mysql C#

时间:2017-10-07 17:26:31

标签: c# mysql linux

我无法连接到我的SQL服务器,我尝试了一些来自stackoverflow和谷歌的修复程序,它没有帮助我。感谢。

  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);
         }

要配置myuser,我在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'@'%';

我试过:Unable to connect to any of the specified mysql hosts. C# MySQL(我试图使用MySqlConnectionStringBuilder,不指定端口,而不是连接字符串中的密码我键入psw); 禁用我的电脑防火墙,禁用linux服务器防火墙

3 个答案:

答案 0 :(得分:0)

MySqlConnectionStringBuilder 允许您指定端口。刚刚测试过,这很好用:

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();

您是否确定您的MySql服务器实际上通过TCP接受传入连接?默认情况下已禁用。

答案 1 :(得分:0)

所以,在谷歌上搜索如何在linux中设置sql连接,并尝试不同的设置几乎我没有修复它所以我想与stackoverflow共享我如何修复它。感谢您的帮助@Avo Nappo

首先,您需要从sql配置中注释掉#bind-address行。 接受的答案是:MySQL root access from all hosts

然后我使用此命令创建用户CREATE USER 'golden'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON * . * TO 'golden'@'%'; 在c#中,我使用了问题中的默认连接字符串。我不知道为什么但MySqlConnectionStringBuilder无法在我的电脑上运行。我希望这会对某人有所帮助。祝你有个美好的一天,并保持编码。

答案 2 :(得分:0)

您必须进入C-Pannel中的远程MySQL ,然后首先添加Access Host并更改此代码

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

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

或使用此方法

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);
}

必须为您工作