简体   繁体   English

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

[英]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. 我无法连接到我的sql服务器,我尝试了stackoverflow和google的一些修复,但并没有帮助我。 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. 为了配置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'@'%';

i tried : Unable to connect to any of the specified mysql hosts. 我尝试过: 无法连接到任何指定的mysql主机。 C# MySQL ( i tried to use MySqlConnectionStringBuilder, don't specify the port, instead of password in connection string i typed psw); C#MySQL (我尝试使用MySqlConnectionStringBuilder,不指定端口,而不是在我键入psw的连接字符串中指定密码); Disable my pc firewall, disable linux server firewall 禁用我的电脑防火墙,禁用Linux服务器防火墙

MySqlConnectionStringBuilder does enable you to specify port. MySqlConnectionStringBuilder 确实使您能够指定端口。 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? 您确定MySql服务器确实接受通过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. 因此,在Google上搜索如何在linux中设置sql连接并尝试尝试不同的设置后,我几乎没有修复它,因此我想与stackoverflow分享我如何修复它。 Thanks for help @Avo Nappo . 感谢您的帮助@Avo Nappo

First you need to comment out the line #bind-address from your sql config. 首先,您需要从sql配置中注释掉#bind-address行。 The accepted answer here : MySQL root access from all hosts . 可接受的答案是: 所有主机的MySQL根访问

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'@'%'; 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. 在c#中,我使用了问题中的默认连接字符串。 I don't know why but MySqlConnectionStringBuilder dose not work on my pc. 我不知道为什么,但是MySqlConnectionStringBuilder在我的电脑上不起作用。 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 您必须在C面板中进入远程MySQL并首先添加访问主机并更改此代码

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 必须为你工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM