0

I am trying to connect to a MySQL database through PHP's PDO. It works fine on my home network and some other networks (coffee shops, public internet). When I try to connect through my phone's personal hotspot or through hotel wifi, it no longer works.

I am using the same login information across all of these with the same machine setup (XAMPP VM - Mac). This is my connection file:

$DBO = "mysql:host=$DB_Servername;dbname=$DB_Name";

try {
    $conn = new PDO($DBO, $DB_Username, $DB_Password);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection Failed: " . $e->getMessage();
}

This is the error I am getting:

Connection Failed: SQLSTATE[HY000] [2002] Connection timed out

I've tried the server name with the website address and the ip address. The strange thing is when I'm using the VS code database extension, I am able to connect to the database fine (no matter the network I am on). I can't figure out why that would be working but the PDO wouldn't be.

I read this answer about adding a space after 'mysql: ', and it returns this new message:

Connection Failed: SQLSTATE[HY000] [1045] Access denied for user 'dailyrts_realDB'@'localhost' (using password: YES)

The user account I am logging in with is assigned to the correct database and the new IP's I connect to are being registered.

dns_get_record returns:

dns_get_record(): A temporary server error occurred.
Dharman
  • 30,962
  • 25
  • 85
  • 135
CodyJHeiser
  • 67
  • 2
  • 2
  • 9
  • 1
    Seems like maybe some of these networks don't have the address of your server registered in their DNS. Have you tried using the IP address instead of a hostname? – ADyson May 19 '22 at 23:25
  • I've tried that as well and no avail. Reached out to my hosting provider and they weren't much help. I don't know enough about networking/web servers to even know what is wrong/what to search for. Everything I search for is talking about how you have to use the loopback IP instead of localhost, which isn't too helpful in this circumstance. – CodyJHeiser May 20 '22 at 13:36
  • Ok. Just to check - presumably if you're accessing the database remotely from these different locations, you've had to add whitelist entries in the mysql configuration and/or in your hosting control panel to allow access to it from different IP addresses? – ADyson May 20 '22 at 15:18
  • Yes that is correct. I have to do that for every new network I connect to. I was originally thinking it was some issue with ports being blocked? I know mobile hotspots usually end up blocking every port, but then I got it connected just fine through that VS Code extension so that kind of threw a wrench in things. I’ll try it out with the regular XAMPP and not the VM version. I’m thinking it might have something to do with that – CodyJHeiser May 22 '22 at 17:02

1 Answers1

0

I wasn't able to find a fix using XAMPP-VM however, the same code works perfectly when using the XAMPP Installer version of the app on MacOS. I looked around for a bit to see if anyone else was having this problem and I didn't find anything that matches this issue specifically. Most were referring to connecting to a local database.

This answer about accessing MySQL through the root user is pretty close to the issue I was having and they recommend following this guide to fix the issue. I don't need to have the VM version installed so I haven't tested this myself however, it seems to have worked for him.

I'm not sure why the VM version only works on some networks and not others when connecting to MySQL databases. I'm sure this has something to do with port blocking on the network's end (mobile hotspot providers are notorious for blocking ports) and because the VM version handles network traffic differently from the regular version.

It looks like issues with the VM version are not isolated to this specific issue on MacOS. In my personal experience (and others), I'd recommend going with the installer version if you are able to. It is more developed and has more online support surrounding it.

CodyJHeiser
  • 67
  • 2
  • 2
  • 9