ravi_sdl Posted July 24, 2012 Share Posted July 24, 2012 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, Quote Link to comment Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 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); Quote Link to comment Share on other sites More sharing options...
ravi_sdl Posted July 24, 2012 Author Share Posted July 24, 2012 Thanks for your reply. I have already added ip address in remote mysql host to provide access. Also as you suggested i have created a connection using "mysqli" but still it is not connected. Quote Link to comment Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 I guess a Firewall or something is blocking the connection there. Do you have (shell-)access to both servers ? Also, have you checked the connection-resource for any error's ? (when using mysqli) var_dump($connection); Quote Link to comment Share on other sites More sharing options...
ravi_sdl Posted July 24, 2012 Author Share Posted July 24, 2012 Nothing is set to prevent database connection. I can easily establish simple database connection. I think i am missing some steps. You can read above remote database connection steps which are followed by me. Quote Link to comment Share on other sites More sharing options...
trq Posted July 24, 2012 Share Posted July 24, 2012 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. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted July 25, 2012 Share Posted July 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
cpd Posted July 25, 2012 Share Posted July 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
ravi_sdl Posted July 25, 2012 Author Share Posted July 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
peipst9lker Posted July 25, 2012 Share Posted July 25, 2012 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? Quote Link to comment Share on other sites More sharing options...
ravi_sdl Posted July 25, 2012 Author Share Posted July 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
ravi_sdl Posted July 25, 2012 Author Share Posted July 25, 2012 t Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.