0

I'm trying to access a database remotely. I am able to access it from the server itself, but I need to access it remotely and am doing that using XAMPP on my computer.

I've added the possibility for the database user to access the database from my ip (also tried using %). Here is the code:

$servername = $websiteIP; //e.g. xx.xx.xxx.xxx, also tried website URL and URL to phpMyAdmin 
$username = 'username';
$password = 'password'; 
$dbname = 'dbname';
$port = '3306';
$conn = new mysqli($servername, $username, $password, $dbname);
// also tried $conn = new mysqli($servername, $username, $password, $dbname, $port);

But I always get this error:

Warning: mysqli::mysqli(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in ...

except when I use $servername = $urlToMyPhpAdmin; Then I get this error:

Warning: mysqli::mysqli(): MySQL server has gone away in xxx on line 10. Warning: mysqli::mysqli(): Error while reading greeting packet. PID=102448 in xxx on line 10. Warning: mysqli::mysqli(): (HY000/2006): MySQL server has gone away in xxx on line 10

What am I missing?

Andri
  • 453
  • 4
  • 22
  • did you check inbound port? Check once by disabling firewall of your DB server (iptable/Selinux). – BetaDev Apr 06 '17 at 22:34
  • @webDev Sorry, I'm not quite sure what an inbound port is. And I am pretty sure I don't have the permission to disable the firewall. – Andri Apr 06 '17 at 22:39

1 Answers1

0

Is the database on the same server as the website? You might need the IP or address of the database host, not the website (or PHPMyAdmin). I would go into PHPMyAdmin and run the following query, taken from this answer :

select * from GLOBAL_variables where variable_name like 'hostname';

and try using the result from that as your $servername

Community
  • 1
  • 1
dave
  • 2,750
  • 1
  • 14
  • 22
  • I get an error when I use your SQL but this works: SHOW VARIABLES WHERE Variable_name = 'hostname' Is that the same? Result is a textstring ($string). Then I use this code: echo gethostbyname($string) and that results in an IP that is the same as the website IP. – Andri Apr 06 '17 at 22:52
  • 1
    Ok cool so you definitely want to be using that IP as your `$servername`. I would have a look at [this](https://support.rackspace.com/how-to/mysql-connect-to-your-database-remotely/) website and ensure the user you've set up has the correct privileges to connect remotely, and perhaps try running mysql locally to connect to the remote DB to ensure it can accept a remote connection – dave Apr 06 '17 at 23:16
  • Seems you've got me on the right track. If I use the IP as $servername (not localhost) and connect locally from the remote server, everything works fine. But when I move the same code to my XAMPP server, I get the error. So I guess even though the control panel for the remote server allows me to change the host from localhost to an ip for each user, I still can't access. – Andri Apr 06 '17 at 23:38