Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to remote mySQL database

I've been trying the following code to attempt to connect to a remote database I'm hosting on digital ocean.

    MySqlConnection myConnection = new MySqlConnection("Server = 104.236.197.146; Port = 3306; Database = BattleRoyale; Uid = root; Pwd = password");
    try
    {
        myConnection.Open();
    }
    catch (Exception e)
    {
        MessageBox.Show( e.ToString(), "Database connection error" , MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

Whenever I attemt to run this code I get the following error:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySql hosts.

I believe the problem is with the hosted database. I've tried the following things:

GRANT ALL PRIVILEGES ON . TO root@"%" IDENTIFIED BY 'rootPass';

I've been trying to check to see if TCP/IP is enabled, but I cannot find it via phpMyAdmin nor going through putty or anything.

As you can tell, I don't know much about Linux machines and I'm just trying to connect to this database to work on a school project. Who knows what I've done trying to fix this issue. Any help would be greatly appreciated.

like image 323
user3376703 Avatar asked Nov 22 '25 01:11

user3376703


1 Answers

Possible reasons include:

  1. MySQL is configured to listen on "localhost" or 127.0.0.1 only - this does not allow remote connections at all, regardless of the firewall configuration
  2. Firewall may be configured to not let through communication on port 3306
  3. MySQL may not be running on the standard port 3306 but some other port

Most probably it's 1. or 2. Especially 1. is something you don't think of at first.

Please note that many hosters don't allow access to the databases from outside their own network. That's why often PhpMySQL works but remote connections don't.

like image 200
Thorsten Dittmar Avatar answered Nov 24 '25 16:11

Thorsten Dittmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!