kaigoh Posted February 18, 2008 Share Posted February 18, 2008 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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/ Share on other sites More sharing options...
Chris92 Posted February 18, 2008 Share Posted February 18, 2008 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"); Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469717 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469722 Share on other sites More sharing options...
Chris92 Posted February 18, 2008 Share Posted February 18, 2008 Can you connect through other ports to the server? Make sure port 3306 is being forwarded. Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469727 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469732 Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2008 Share Posted February 18, 2008 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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469742 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 <?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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469750 Share on other sites More sharing options...
Chris92 Posted February 18, 2008 Share Posted February 18, 2008 Try getting rid of the gethostbyname function $link = mysql_connect('<HIDDEN>:3306', '<HIDDEN>', '<HIDDEN>') or die('Could not connect to <HIDDEN> server! Connection Error: '.mysql_error()); Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469753 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 I have done already. Same problem. (P.S. I need that in there in case the IP address of the server changes) Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469754 Share on other sites More sharing options...
Chris92 Posted February 18, 2008 Share Posted February 18, 2008 Is it giving a mysql error? Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469759 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 The only output is: "Connection Error: Can't connect to MySQL server on '84.xxx.xxx.xxx' (13)" Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469761 Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 Does your_user_name@your_host have an account set up in the remote database with the proper permissions? Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469762 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 Yes, first thing I looked at Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469764 Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 Is the machine you're working on in the /etc/hosts file of the other? Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469769 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 No. The code I am writing is destined to be installed on clients machines. It will be impossible to include all the hosts. The permisions for mySQL are set to user@% so that a connection can be established from any host. Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469772 Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2008 Share Posted February 18, 2008 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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469801 Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 Is the server using bind-address or skip-networking in its configuration? Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469802 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 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 https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469807 Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 Is sql.safe_mode off in your php.ini? Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469814 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 yup Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469819 Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2008 Share Posted February 18, 2008 See if php will give you any more information about why it is not connecting. Add the following after your first <?php tag - ini_set ("display_errors", "1"); error_reporting(E_ALL); Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469822 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 Nope Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '84.xxx.xxx.xxx' (13) in /var/www/html/index.php on line 11 Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469825 Share on other sites More sharing options...
effigy Posted February 18, 2008 Share Posted February 18, 2008 Does PHP see the other box? For example: <pre> <?php echo `ping the_other_box_ip`; ?> </pre> Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469827 Share on other sites More sharing options...
kaigoh Posted February 18, 2008 Author Share Posted February 18, 2008 Yes, it can ping the other box. Link to comment https://forums.phpfreaks.com/topic/91709-php-refuses-to-connect-to-remote-mysql-server/#findComment-469832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.