mysql_connect, Can't get hostname for your address

Hi. I am having trouble with the mysql_connect function. When I try it I get the “Can’t get hostname for your address” error. I have enabled skip-name-resolve on my mySQL server, and I am positive this error has something to do with PHP, not the mySQL server because this works from the EasyPHP, but not my actual web server. Here are the details:

MySQL server:
OS: Windows 7
IP: 192.168.0.188
Port: 3306
Version: 5.5.8

skip-name-resolve is in the my.ini configuration file under [mysqld], as it should be.
remote connections are enabled, and work from other machines.

PHP connecting client:
OS: Ubuntu Server 10.04.3
IP: 192.168.0.198
Apache Version: 2.2.14
PHP version: 5.3.2-1ubuntu4.10 with Suhosin-Patch (cli)

Code:
[php]<?php
$con = mysql_connect(“192.168.0.188:3306”,“root”,“password”);
if (!$con)
{
die('Cant Connect: ’ . mysql_error());
}
?>[/php]

That code is saved in a .php file as test.php. When I visit it from my PC’s version of EasyPHP (5.3.8.0) everything works fine, but when I visit the same page when its hosted on my Ubuntu server, I get “Could not connect!: Can’t get hostname for your address”.

If you are hosting the mysql server on a machine in your home, but trying to connect from outside you will have to use its proper IP address not it’s local network one.

If the php web-server is on the same machine as your mysql server you could simply try localhost as the IP.

Hi. Thanks for your response. Both the PHP server and the MySQL server are within my home, and I’m trying to connect across the LAN. They are on different machines, so localhost won’t work.

So It is only not working on local network? Have you tested from an outside network connecting back to your home?

I have not tested it from outside my network, as that would require port forwarding and hostnames, which would not only complicate the setup, but isn’t needed and would open a big security issue. I have tested it with my portable PHP server used for web development (EasyPHP), and it connects to the MySQL database fine. I just can’t get it to work from the actual Ubuntu PHP server.

It could be a configuration issue in your web-server but I suggest testing from outside. Port-forwarding isn’t that much of a security issue as long as the ports opened are assigned to something. All you need open is 3306 for mysql and as for hostname you can simply use your actual IP address as your router will direct it to the correct pc.

You know, I’m pretty sure the correct syntax for localhost is:

[php]
mysql_connect(“root”, “pass”, “localhost”)
[/php]

mysql connect paramater is always mysql_connect(server, user, pass)

http://uk.php.net/manual/en/function.mysql-connect.php

Sponsor our Newsletter | Privacy Policy | Terms of Service