Jump to content

PHP refuses to connect to remote MySQL server


kaigoh

Recommended Posts

Hi there.

 

First, let me appologise in advance if this question has been answered elsewhere, I have been looking for the solution for this since 8 this morning and I'm getting fed up with trawling through forums! :)

 

Ok, here is the problem:

 

PHP will talk to a local database (through socket) but will not talk to a remote one. I have added all necasary permissons to allow a user to connect to the db remotely on the server I am trying to connect to. I can connect to the database remotely using mysql client on the web box, which is why I do not understand why PHP will not connect to it? I have trapsed through loads of websites and forums and IRC but everything just draws a blank?!

 

I hope someone can help me, this truly is driving me nuts.

 

Just so you know:

 

Linux (CentOS 5)

Apache 2

PHP 5

MySQL 5

 

Remote end, firewall is allowing the correct ports.

 

Thanks,

 

Kai.

Link to comment
Share on other sites

There should be no reason for it not to connect unless you are filling in the wrong details. Maybe you should try putting the port at the end of the ip if you already haven't:

mysql_connect("127.0.0.1:3306","username","password");

 

I have tried it already. I know it should work, I really just don't understand why it wont?

Link to comment
Share on other sites

I have tried to connect to the server both with the firewall on and off. The server has 3306 open. The wierd thing is I can connect to the server with the mysql -h <SERVERIP> -u blah blah -p command and manipulate the database. It is just PHP that won't connect.

Link to comment
Share on other sites

If a mysql client will connect, php should be able. Post your code (xxxxx'ing out any sensitive information but don't change any punctuation or syntax.)

 

How do you know it is not connecting? What errors or other symptoms? Anyway, posting your code will help in finding out what it is doing that is not working.

Link to comment
Share on other sites

<?php

if(ISSET($_SERVER["HTTPS"])) {

$server = gethostbyname('<HIDDEN>');

//Remote <HIDDEN> connection
$link = mysql_connect($server.':3306', '<HIDDEN>', '<HIDDEN>') or die('Could not connect 

to <HIDDEN> server! Connection Error: '.mysql_error());
mysql_select_db('<HIDDEN>') or die('Could not connect to <HIDDEN> server! Error: DBSELECTERROR');
$query = " SELECT * FROM activation WHERE ACTIVATION_CODE = '".$pbx_local_data["ACTIVATION_CODE"]."' LIMIT 1 ; 

";
$result = mysql_query($query) or die('Could not connect to <HIDDEN> server! Query Error: ' . mysql_error());
$pbx_remote_data = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);

 

Thanks.

Link to comment
Share on other sites

Is the host name that you are putting into - gethostbyname(); the host name that corresponds to the IP address of the database server? Have you checked that the IP address it returns is the IP address that the database server is listening to.

 

Basically, the error message indicates that there is no server listening at the IP : PORT combination.

Link to comment
Share on other sites

Yes, the server is setup to bind to address 0.0.0.0 because it has two IP addresses, both a private and a public one.

 

I have tried connecting to another sql database on a completely different server with the same results.

 

And yes, gethostbyname() is returning the correct IP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.