December 2, 2011

mysql_connect() using http protocol

Question by Saheelram

I am trying to connect to a MySql server (not localhost) from my computer using the code below. It is giving this error:

Warning: mysql_connect() [function.mysql-connect]: [2002] Connection refused (trying to connect via tcp://10.6.3.6:3306) in on line 7

I wonder if we can use the http protocol to connect instead of tcp that is being used by default? I searched quite a bit on how to change the protocol, but most of the answers were describing how to connect to localhost, and not much about how to connect to another server. Please help.

PS: I am able to connect to the server by going to http://10.6.3.6/phpmyadmin/…). So I am sure the server is up.

My Code

<?php

$db_hostname = '10.6.3.6';
$db_database = 'db_user11';
$db_username = 'db_user11';
$db_password = '########';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());

?>

Answer by deceze

First of all, HTTP is layered on top of TCP/IP.
In order to connect to something via HTTP, it needs to run an HTTP server. MySQL does not run an HTTP server and there’s no current/realistic/supported way to tunnel an SQL connection through HTTP. Even if there was, HTTP is not exactly the best protocol for this.

In short: no. You’re trying to solve the wrong problem. You need to configure your MySQL server to allow connections from other machines over the network, give the user you’re trying to connect with appropriate permissions to connect from other machines and make sure the MySQL server is reachable from other machines.

Answer by Starx

Just because the phpmyadmin is hosted on that server, does not neccesarily mean that the database server is in the same IP address. Please ask the domain administrator to give you the details of database server.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!