0

I want to connect to a MySQL database in my Xamarin.Android app, however, no matter which framework (Pomelo, Xamarin.MySql.Data, etc.), it writes "Unable to connect to any of the specified MySQL hosts". I have tried everything I could think of. I also have a RAGE:Multiplayer server where everything works fine with the same data, so it should not (?) be due to any firewall settings.

Code:

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;
            }
        }
}

Error: MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. at MySql.Data.MySqlClient.NativeDriver.Open () [0x0003f] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.Driver.Open () [0x0000b] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x00036] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () [0x00000] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () [0x00083] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () [0x00042] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.MySqlPool.GetConnection () [0x0001c] in <5e4982151d314701bcfe1f0a397fe0db>:0 at MySql.Data.MySqlClient.MySqlConnection.Open () [0x000ad] in <5e4982151d314701bcfe1f0a397fe0db>:0

sckindvl
  • 1
  • 2
  • use the name or IP of the server, not `localhost`. `localhost` means that the server is running **on the Android device**. And I'll add the general warning that connecting directly to a DB server from a mobile device is almost always a terrible idea. – Jason Apr 28 '23 at 19:32
  • Its currently for debug, its running on android emulator, so its local (or isnt that possible in this case?) – sckindvl Apr 28 '23 at 19:33
  • 2
    no, `localhost` then would mean **the Android emulator**. There are numerous articles and posts about configuring networking between the emulator and host machine – Jason Apr 28 '23 at 19:41

0 Answers0