我在c#中连接到一个mysql数据库,只要我输入服务器地址为" localhost"或者它的ip,但是如果我尝试远程ip它失败了:
Error: 0 : Unable to connect to any of the specified MySQL hosts.
这里是代码:
server = Properties.Settings.Default.DBHost;
port = Properties.Settings.Default.DBPort;
database = Properties.Settings.Default.DBName ;
uid = Properties.Settings.Default.DBUser;
password = Properties.Settings.Default.DBPassword ;
string connectionString;
connectionString = "SERVER=" + server + ";" + "PORT=" + port + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
[...]
if (this.OpenConnection() == true){
// exec db operations
}
else{
MessageBox.Show("Database Connection Error.");
}
测试:
答案 0 :(得分:0)
如果是你的mysql服务器那么,
如果它不是你的mysql服务器那么,
答案 1 :(得分:0)
首先,你必须检查你的服务器是否支持远程MySQL database Connection
然后
你可以尝试这个MySQL Connection String Builder
例子,我一直在努力..没有错误:
_connectionStr = new MySqlConnectionStringBuilder
{
Server = "127.0.0.1",
Database = myDatabase,
UserID = myUserName,
Password = myPassword,
ConnectionTimeout=60,
Port = 3306,
AllowZeroDateTime = true
};
_con = new MySqlConnection(_connectionStr.ConnectionString);
try
{
_con.Open();
}
catch
{
MessageBox.Show("Error, help i can't get connected!");
}
希望这有效!
答案 2 :(得分:0)
也许您的MySQL服务器未配置为侦听任何IP接口。 也许只是在听你的“localhost:3306”
在MySQL服务器配置文件“bind-address”中查看它。