无法连接到任何指定的MySQL主机.与C# [英] Unable to connect to any of the specified MySQL hosts. with c#

查看:127
本文介绍了无法连接到任何指定的MySQL主机.与C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mysql数据库.
当我使用本地mysql数据库创建连接时,它可以正常工作,但是当我使用服务器数据库时,则无法正常工作.

我的本地字符串是:

服务器=本地主机;数据库= hello42_new; uid = root;密码= hello42 @ 123;"

该连接字符串可以正常工作,但是当我使用
时 服务器= 50.62.22.158:8085;数据库= hello42_new; uid = root;密码= hello42 @ 123;"

则它不与数据库通信并显示错误:

无法连接到任何指定的MySQL主机."

请告诉我我该如何解决.

我的代码是:

I am using mysql database.
When I create conection with local mysql database it works fine but when I am using server database it don''t work.

My local string is:

"server=localhost; database=hello42_new; uid=root; password= hello42@123;"

This connection string works fine but when I use
"server=50.62.22.158:8085; database=hello42_new; uid=root; password= hello42@123;"

then it doesn''t communicate with database and shows the error:

"Unable to connect to any of the specified MySQL hosts."

Please telll me how can i resolve it.

My code is:

MySqlConnection con = new MySqlConnection("server=50.62.22.108:8085; database=hello42; uid=root; password= hello42; ");

 MySqlCommand command = con.CreateCommand();
 command.CommandText = "select * from login";
 con.Open();
 MySqlDataReader Reader = command.ExecuteReader();
 while (Reader.Read())
 {
     string name = "";
     string pass = "";
     if (!Reader.IsDBNull(0))
         name = (string)Reader["userName"];
     pass = (string)Reader["userPass"];
 }
 MessageBox.Show("success");
 Reader.Close();

推荐答案

尝试将逗号:"替换为逗号,"
50.62.22.108:8085
50.62.22.108,8085

并立即更改密码,因为您刚刚将其公开:)下次使用更改后的值myPassword,MyUID或其他内容替换您的真实连接字符串.
只是友好的建议:)
Try simply replacing colon ":" with comma ","
50.62.22.108:8085
50.62.22.108,8085

And immediately change the password since you just put it public :) Next time replace your real connection string with changed values myPassword, MyUID or something.

Just friendly advice :)


两件事:

-默认情况下,root用户不允许远程访问.这不是一个好主意,但是您可以通过运行以下命令来启用它:

2 things:

- Remote access is not allowed by default to root user. It''s not a good idea,but you can enable it by running this command:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;



-另一方面,检查my.cnf文件,找到此行



- On the other hand, check my.cnf file, find this line

bind-address = 127.0.0.1

并将其注释掉.

希望对您有帮助

and comment it out.

Hope it helps


这篇关于无法连接到任何指定的MySQL主机.与C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆