Jump to content

How to create Remote database connection


ravi_sdl

Recommended Posts

Hi,

 

I want to establish remote database connection. I have detail of database, user name and password. I tried to create a remote connection but it not connected to remote server.

 

For example my website name is "http://test.com" and another website name is "http://remotesite.com". I want to fetch data of http://remotesite.com from http://test.com. I have followed following steps:

 

  1. Firstly login at http://remotesite.com control panel and click on remote database icon and enter http://test.com and its ip information.

  2. I make a connection in http://test.com to connect with http://remotesite.com. I have used following tricks.

 

                    (a).  mysql_connect("http://remotesite.com","username","password");

                            mysql_select_db("database_name");

 

                    (b).  mysql_connect("http://remotesite.com:portname","username","password");

                            mysql_select_db("database_name");

 

                    ©.  mysql_connect("IP Address:portname","username","password");

                            mysql_select_db("database_name");

 

                    (d).  mysql_connect("IP Address","username","password");

                            mysql_select_db("database_name");

 

But it is not connect with remote database. It means not connect with http://remotesite.com database.

Any on can help me in this regards.

 

Thanks,

Link to comment
Share on other sites

You should use the IP instead of domain (To get the IP of a domain simply ping it)

Have you tried Mysqli instead of standart mysql class?

 

Edit: The Port would be another parameter, dont put it behind the IP.

Edit2: Here is a small example of Mysqli (replace the $db-vars with your's!)

$connection = @new mysqli($db->host, $db->user, $db->pass, $db->name, $db->port, $db->socket);

Link to comment
Share on other sites

Firstly, mysql doesn't run via http, so http://blahblah is not valid.

 

Have you setup a user with permission to connect from this remote address?

 

eg; Say your connecting from server A to server B. You need to setup a user in mysql on server B that has permission to access server B from server A.

 

This is really a mysql question.

Link to comment
Share on other sites

When using mysql_connect, the port number (if it is not the standard port, 3306) is appended to the hostname, with mysqli_connect it is a separate parameter.

 

Use mysql_connect('remotesite.com:##', 'user', 'password') or die(mysql_error()); for testing. The error will tell you why the connection failed. I would use the hostname rather than IP address, so if the server is moved, DNS will continue to resolve it. Do not use http since that protocol is specifically for websites. Don't use www. since that sub-domain is probably directed to the webserver. You just use the domain and top-level-domain i.e. remotesite.com. Unless the remote administrator gave you a specific sub-domain to use.

 

If the error says Can't connect or the server can't be reached or refused the connection (or something), then the server may not be listening on the specified port.

 

If the error says something like Access Denied, then either the username is wrong, or the password is wrong, or there is no entry in the users table for that username from test.com. By default, mySql adds users with permission to connect from localhost only. You have to specifically give the user permission to connect from a remote host. And, unless I'm mistaken, the same username can have a different password depending on the origin of the login request.

 

When granting the user permission, I would, again, specify the hostname, rather than IP address, so if the server is moved, DNS will continue to resolve it.

 

I'm pretty sure the remote server has to be configured to allow remote connections as well.

 

If you are doing this on development systems, either modify the /etc/hosts file for hostname resolution, or use the IP addresses -- DNS will not be able to resolve your internal host names.

Link to comment
Share on other sites

You should use the IP instead of domain (To get the IP of a domain simply ping it)

Have you tried Mysqli instead of standart mysql class?

 

Edit: The Port would be another parameter, dont put it behind the IP.

Edit2: Here is a small example of Mysqli (replace the $db-vars with your's!)

$connection = @new mysqli($db->host, $db->user, $db->pass, $db->name, $db->port, $db->socket);

 

As already indirectly pointed out, you can use a domain name to connect, it just maps across to an IP address. You can also put "localhost:3306" as the host as its the correct way to define a port on the end of a host. Have you eer considered how MySQLi does it behind the scenes? Just like that.

Link to comment
Share on other sites

Hello,

 

Thanks for reply.

I am try to make connection as below:

 

mysql_connect('remotesite.com:3306', 'user', 'password') or die(mysql_error());

 

It show "Connect failed: Unknown MySQL server host 'remotesite.com:3306' (1)" error.

 

 

Link to comment
Share on other sites

As already indirectly pointed out, you can use a domain name to connect, it just maps across to an IP address. You can also put "localhost:3306" as the host as its the correct way to define a port on the end of a host. Have you eer considered how MySQLi does it behind the scenes? Just like that.

 

Thanks, I didn't know that, I just knew there is an extra parameter for it - so why not using it.

 

mysql_connect('remotesite.com:3306', 'user', 'password') or die(mysql_error());

It show "Connect failed: Unknown MySQL server host 'seovalley.com:3306' (1)" error.

 

Looks like the server can't resolve the domain - do you get any other error when you try the IP instead?

Link to comment
Share on other sites

Hello,

 

Thanks for reply.

I am try to make connection as below:

 

mysql_connect('remotesite.com:3306', 'user', 'password') or die(mysql_error());

 

It show "Connect failed: Unknown MySQL server host 'remotesite.com:3306' (1)" error.

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.