You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Android连接MySQL报错:无法连接到指定MySQL主机

Xamarin.Android连接MySQL报“无法连接到指定的MySQL主机”问题解决

问题概况

在Xamarin.Android应用中尝试连接MySQL数据库,无论使用Pomelo、Xamarin.MySql.Data等框架,均提示Unable to connect to any of the specified MySQL hosts(无法连接到指定的MySQL主机)错误。相同的连接信息在RAGE:Multiplayer服务器中可正常使用,排除防火墙设置问题。

代码示例

public static void TryLogin(string username, string password)
{
   Connection con = new Connection();
   bool tried = con.TryConnection(mContext);
   Toast.MakeText(mContext, $"Worked?: {tried}", ToastLength.Long).Show();
}

public class Connection
{
        public bool TryConnection(Context context)
        {
            MySqlConnectionStringBuilder Builder = new MySqlConnectionStringBuilder();
            Builder.Port = 3306;
            Builder.Server = "localhost";
            Builder.Database = "scgp";
            Builder.UserID = "root";
            Builder.Password = "M9Pb3Tnr3y4DDR4P2I3v";
            Console.WriteLine("[Builded Connection String]: " + Builder.ToString());
            try
            {
                MySqlConnection ms = new MySqlConnection(Builder.ToString());
                ms.Open();
                return true;
            }
            catch (Exception ex)
            {
                Toast.MakeText(context, ex.ToString(), ToastLength.Long).Show();
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
}

错误信息

MySql.Data.MySqlClient.MySqlException: 无法连接到指定的MySQL主机。
在 MySql.Data.MySqlClient.NativeDriver.Open () [0x0003f] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.Driver.Open () [0x0000b] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x00036] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () [0x00000] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () [0x00083] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () [0x00042] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.MySqlPool.GetConnection () [0x0001c] in <5e4982151d314701bcfe1f0a397fe0db>:0  
在 MySql.Data.MySqlClient.MySqlConnection.Open () [0x000ad] in <5e4982151d314701bcfe1f0a397fe0db>:0 

解决办法

1. 替换localhost为正确的服务器地址

安卓模拟器中的localhost指向模拟器自身,而非运行MySQL的电脑。需替换为以下地址之一:

  • 若使用安卓自带模拟器,用10.0.2.2(模拟器映射到主机localhost的特殊地址)
  • 若使用第三方模拟器(如夜神、蓝叠),用主机的局域网IP(可通过主机的ipconfig/ifconfig查询,例如192.168.1.100)

修改代码中的服务器地址:

Builder.Server = "10.0.2.2"; // 或你的主机局域网IP

2. 确保MySQL允许远程连接

默认root用户可能仅允许本地登录,需修改权限:

  1. 登录MySQL控制台:
mysql -u root -p
  1. 授权root用户从任意地址登录(或指定设备IP):
GRANT ALL PRIVILEGES ON scgp.* TO 'root'@'%' IDENTIFIED BY 'M9Pb3Tnr3y4DDR4P2I3v';
FLUSH PRIVILEGES;
  1. 修改MySQL配置文件(my.cnf或my.ini),将bind-address设为0.0.0.0(允许所有IP访问),然后重启MySQL服务。

3. 关闭SSL连接

移动端连接MySQL时,SSL可能导致连接失败,可在连接配置中添加禁用SSL的设置:

Builder.SslMode = MySqlSslMode.None;

4. 检查网络连通性

  • 确保手机/模拟器与运行MySQL的主机处于同一局域网
  • 在模拟器终端执行ping 你的主机IP,测试能否正常连通

内容的提问来源于stack exchange,提问作者sckindvl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 18:42:41