0

When trying to connect to remote Mysql server I get this error :

php_network_getaddresses: getaddrinfo failed: Hôte inconnu. (translation: Host unknown).

con.php :

<?php 
  $db_host     = 'http://xxx.xxx.xxx.xxx';
  $db_username = 'xxx';
  $db_password = 'xxx';
  $db_name     = 'xxx';

  $con = mysql_connect($db_host,$db_username,$db_password);

  $select_db = mysql_select_db($db_name,$con);

?>

When connecting to localhost all goes fine, but for remote connection it didn't work.

BenM
  • 52,573
  • 26
  • 113
  • 168
thesubroot
  • 462
  • 6
  • 16
  • 2
    You can't connect to MySQL over HTTP. Remove the protocol, and ensure that the user has permission to access on all domains. – BenM Apr 18 '13 at 14:34

2 Answers2

5

$db_host = 'xxx.xxx.xxx.xxx'; Instead

You can't use the protocol HTTP for Mysql since they both run on different ports / use different protocols.

Further more HTTP (web service) = 80 Mysql (your database) = 3306 just to note as mentioned by BenM this is the default port. In most instance this is not changed. However you change its port to any other port (like you can with any other service).

And in addition please take a look at PDO since Mysql_ functions are going to be deprecated and are also dangerous to use in new systems and in old ones as well.

Take a look at one of my answers on how to set up PDO

Edit 1

As you mentioned on benM's answer in a comment, "No connection could be made because the target machine actively refused it."

so now if you have root access on the actual server the database is hosted on then you have to check if the server (Example in my case is ubuntu server)

run this command :

sudo netstat -pan | grep mysql

Also take a look at this picture : Pretty much it shows the TCP port, if it iS Listening or not listing (here it is listening)

enter image description here

again this is assuming you have root access to the actual linux / (whatever server) I am only proficient at ubuntu but can assist you with other server information as well.

Community
  • 1
  • 1
Rixhers Ajazi
  • 1,303
  • 11
  • 18
  • MySQL is not necessarily running on `3306`. And they're going to be deprecated, not depreciated ;) – BenM Apr 18 '13 at 14:40
  • To the best of my knowledge (which is limited) the Database server is listening to requests on port 3306. If I am incorrect could you please elaborate? – Rixhers Ajazi Apr 18 '13 at 14:42
  • 1
    `3306` is the default port for MySQL, but it can be configured to use a different port. – BenM Apr 18 '13 at 14:42
  • (face palm) - Lol, yes of course, but in this case (with this OP) we could all come to a conclusion that he or she is currently using 3306 for the port address. But yes you are correct. – Rixhers Ajazi Apr 18 '13 at 14:43
4

You cannot connect to MySQL using the HTTP protocol (namely because of port conflicts). The parameter should be an IP string. Remove the http protocol as follows:

$db_host     = 'xxx.xxx.xxx.xxx';
$db_username = 'xxx';
$db_password = 'xxx';
$db_name     = 'xxx';

$con = mysql_connect($db_host, $db_username, $db_password);
$select_db = mysql_select_db($db_name, $con);

You should also note that MySQL can be configured to prevent anything other than localhost access on user accounts. Ensure that the user you're using is able to access the database from any domain.

Finally, the mysql_* family of functions are now being deprecated, and you should avoid using them for new code. Instead, check out MySQLi or PDO.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • Now i get this error `No connection could be made because the target machine actively refused it.` I'm using the root login, and it has all privileges. – thesubroot Apr 18 '13 at 14:54
  • you have to check if the machine that the DB is hosted one 1) is allowing incoming traffic to that port. 2) If the user that you are connected as is privileged to use those tables / database. – Rixhers Ajazi Apr 18 '13 at 15:01
  • and also allowing out going traffic from that port. – Rixhers Ajazi Apr 18 '13 at 15:02
  • @RixhersAjazi, thx for your answer, It was about allowing the root user to connect from any domain: `mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;` – thesubroot Apr 18 '13 at 15:46