[英]How to connect ASP.NET Web Api project with hosted mySQL database?
I am working with an api web application that is connected to mysql database using Visual Studio for Mac, my problem is that when I try to connect with hosted mysql db in the server I got the following exception "Unable to connect to any of the specified MySQL hosts." 我正在使用使用Visual Studio for Mac连接到mysql数据库的api Web应用程序,我的问题是当我尝试与服务器中的托管mysql db连接时,出现以下异常“无法连接到任何指定的MySQL主机。” However, when you connect to the local db mysql by using XAMPP, the connection works well and successfully.
但是,当您使用XAMPP连接到本地db mysql时,连接工作良好且成功。
here is my code. 这是我的代码。
in Program.cs 在Program.cs中
public static MySqlConnection Conn(){
var connection_string = "Data Source=MYSQL5015.site4now.net;Initial Catalog=db_a42078_holzqp;User ID=xxxxxx;Password=xxxxxxx";
var conn = new MySqlConnection(connection_string);
return conn;
}
in ValuesController.cs 在ValuesController.cs中
public string Get(int id){
conn = Program.Conn();
try
{
conn.Open();
}
catch (MySqlException ex)
{
return "Failure: " + ex.Message.ToString();
}
if(conn.State == System.Data.ConnectionState.Open){
return "opened";
}else{
return "closed";
}}
Note : i also tried the standard mysql connection string like "SERVER:XXX;DATABASE=XXX;UID=XXXX;PWD=XXXX" and i got the same exception. 注意 :我还尝试了标准的mysql连接字符串,例如“ SERVER:XXX; DATABASE = XXX; UID = XXXX; PWD = XXXX”,并且遇到了相同的异常。
please help and thanks for all 请帮助并感谢大家
My first guess, is that the error you are getting indicates it couldn't find the host name (a DNS issue). 我的第一个猜测是,您收到的错误表明它找不到主机名( DNS问题)。 So, if the issue is around the server name in your connection string you can try 1 of 2 things:
因此,如果问题出在连接字符串中的服务器名称附近,则可以尝试以下两种方法之一:
You can use your " Network Utility ", since you are using MAC, to discard if this is a DNS issue by ping ing the server name and it will probably give you the same error if it can't resolve the name. 您可以使用“ 网络实用程序 ”(因为您使用的是MAC)通过ping服务器名称来丢弃这是否是DNS问题,如果无法解析该名称,则可能会出现相同的错误。 This will also give you the IP Address if you want to use this to connect.
如果要使用此IP地址进行连接,还将为您提供IP地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.