thesubroot
thesubroot

Reputation: 438

My code only connect to localhost mysql server but not for remote server

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.

Upvotes: 0

Views: 2550

Answers (2)

Rixhers Ajazi
Rixhers Ajazi

Reputation: 1313

$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.

Upvotes: 5

BenM
BenM

Reputation: 53198

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.

Upvotes: 4

Related Questions