Honesty Rocks! truth rules.

Connecting To A Remote Database

HOME      >>       Programming

glodrop

I wondered if anyone here can help.I have a database on a remote server (A). I want to access it through a php script on a different server C.I have allowed access on (A) in Cpanel - mysql dbases - Remote Access by putting the IP address of server CThe host of server (A) has also whitelisted the IP of server C.Though I am having trouble connecting from server C which is with a different webhost.any ideas how to get round this?I have though of SSH forwarding, but I think this can only be used to connect a client - e.g. my pc to the remote server A. I do not have shell access on server CI have thought of accessing through SSH on an additional server B - but I can't get my head round any of it or see how the final php settings are going to end up.


faulty.lee

Though I am having trouble connecting from server C which is with a different webhost.
any ideas how to get round this?

Can you access the database on server A from your down PC? If server A's database cannot be access external, maybe it's not meant to be done as such. You can write php script as proxy to do the query and return the result in xml form. You can refer to SQLYog, it's using such method to bypass those server that doesn't allow external access. Though the proxy feature is only available in the enterprise version of SQLyog. The opensource free version doesn't not. Anyway, you can get a trial version and find out how it works. If you're not doing much query, you can also write the php script for a limited number of function.

I'ven't really tested this. http://www.phpclasses.org/package/4000-PHP-Execute-remote-MySQL-queries-across-the-Web.html. But it should works. Or you can look for something similar.

I have though of SSH forwarding, but I think this can only be used to connect a client - e.g. my pc to the remote server A. I do not have shell access on server C
I have thought of accessing through SSH on an additional server B - but I can't get my head round any of it or see how the final php settings are going to end up.


Not sure about SSH. Normally there's simple way to solve problem. If it ended up with complex solution, think again, there's always simpler ones.

glodrop

Hi,Firstly I tried testing access on from my pc through telnet to the remote server A and I just get '4.1.22-standard' and 25 asciicharacters and a flashing cursor.My host has just installed ssh for me on the cpanel so i'll try and test it through that. I'm new to telnet and ssh.The script you gave a link for. I will try and test the server with that as well - though my goal is to not just to execute queries.The database is a directory of websites on server A. On server C is the php script which will access that database. Instead of inputting 'localhost' in the config file I would add the ip of server C. Though when I have done that I have not had much luck - I just get the message - Can't connect to MySQL server on 'IP for server A' (4) in /home/a9463775/public_html/directory/index.php on line 72I wrote $db_host = 'IP for server A:3306' in the config file.My host of the remote server A assures me that it should work, it is remotely accessible, they have whitelisted the IP and port 3306 is open.The php script on server C is meant to update the database with new data as well as perform search queries on existing data in the database on server A. It is like any other directory php script with it normally accessing the database on 'localhost'Any other ideas on this?


faulty.lee

Firstly I tried testing access on from my pc through telnet to the remote server A and I just get '4.1.22-standard' and 25 asciicharacters and a flashing cursor.

My host has just installed ssh for me on the cpanel so i'll try and test it through that. I'm new to telnet and ssh.


If you can telnet in via 3306, then it's accessable externally. Though you won't be able to do anything via your telnet connection, it's not meant to be connected that way. Thus, I don't see the purpose for ssh connection too. SSH is a secure version of telnet.

The database is a directory of websites on server A. On server C is the php script which will access that database. Instead of inputting 'localhost' in the config file I would add the ip of server C. Though when I have done that I have not had much luck - I just get the message -
Can't connect to MySQL server on 'IP for server A' (4) in
/home/a9463775/public_html/directory/index.php on line 72

I wrote $db_host = 'IP for server A:3306' in the config file.

My host of the remote server A assures me that it should work, it is remotely accessible, they have whitelisted the IP and port 3306 is open.


It seems more that your server C is having problem accessing external databases. Some web server denied external socket connection, to prevent usage for illegal purposes. You can try connect to another external database to confirm, or check with server C's host

yordan

It has to work.I did it with a phpbb3 forum, I had php on one system and the dabase on the other system.So, instad of defining the database server as "localhost" I defined it with the connect hotname and host port given by the database server, and it worked.Yordan


glodrop

Hi,My host on server A suggested me testing it via telnet. Is the reason why I just get a load of ascii characters - because its not meant to be used that way. ? It seems that I am connecting. Is that what I am supposed to get on telnet?Server C is a free host, server A isn't, so I can't get anything from server C, I've tried communicating with them before on this. I have tried accessing the dbase uploaded to other remote(free) hosts though they had a firewall themselves, I couldn't connect to the database there either. Which is why I thought i'd go over to paid hosting and have IPs whitelisted.Is there anyway round accessing it through an external socket, and instead access solely with the IP?I would therefore put $db_host = 'IP for server A' without the port in the config file.or any other ideas????Yordan - what is different about the way your servers are set up as opposed to the ones I'm using?It is correct to write the port with the ip as ip:port in place of 'localhost' ?? in the php script.


faulty.lee

My host on server A suggested me testing it via telnet. Is the reason why I just get a load of ascii characters - because its not meant to be used that way. ? It seems that I am connecting. Is that what I am supposed to get on telnet?

Yes, that's what you should be getting, meaning, you don't even need whitelist, the port is already open, and they allow external access

Server C is a free host, server A isn't, so I can't get anything from server C, I've tried communicating with them before on this. I have tried accessing the dbase uploaded to other remote(free) hosts though they had a firewall themselves, I couldn't connect to the database there either. Which is why I thought i'd go over to paid hosting and have IPs whitelisted.

Why not just host your pages and database on the same server. If you're worried about your domain name, you can assign it to another server of your choice.

Is there anyway round accessing it through an external socket, and instead access solely with the IP?
I would therefore put $db_host = 'IP for server A' without the port in the config file.

or any other ideas????

Yordan - what is different about the way your servers are set up as opposed to the ones I'm using?

It is correct to write the port with the ip as ip:port in place of 'localhost' ?? in the php script.

Yes, that's the way to put the port. If you ignore the port, it will use the default 3306. So, if you're using default port, you don't have to put it in the config. IP alone will do. I guess you mean "route" (round), since you can't make socket connection, i guess you can't route also, you still need a socket connection to route.

glodrop

I don't host my pages and database together on the same server as the domain name is a sub-domain of the free host; I can't just change nameservers. I am running out of space on server C and server C doesn't allow upgrades. It would also be useful to have a few sites on different servers accessing the same databaseWould it be possible to have a port eg. 22 or 2222 with the ip - to read as ip:22 in the php script and then once connected to server A it would then be forwarded to port 3306 still giving the necessary login and password.Perhaps server C is only denying external socket connection for port 3306, perhaps not for all the ports????How could I set this up to work on server A?Perhaps I could ask the host of server A to assign my database to a different port. Though if server C is not denying external socket connection for all ports - which would be the best port to use?


faulty.lee

Perhaps server C is only denying external socket connection for port 3306, perhaps not for all the ports????

Denial of external socket connection means all ports. You should first try to confirm if server C is in fact denying all external socket connection including MySQL.

It's really not worth the effort to setup proxy via php. It's a very complex solution for a very simple problem. Running php based proxy will make you face more problem later, as proxy require more direct access to the host's hardware as oppose to conventional method. 99% of all web host limit such functionality as it post a huge risk of security.

You can setup redirection to your subdomain on server C to your server A and host everything there. You can keep those non data related pages/images on server C if space is a constrain. Though the best practice is still keeping everything under one roof for easy maintenance and faster access(performance)

yordan

Maybe your database server has a problem.
I would suggest, for debugging purposes, to use a public free database server.
For instance, try http://forums.xisto.com/no_longer_exists/ , register to an account, create a database, use their phpmyadmin to verifiy that the database is functionnal, and then try to remotely connect to your database.
I don't like freemysql.net because their control panel has a lot of ads and popups ; however, their remote database server works fine, so for testing purposes it's OK.
yordan


iGuest

how to connect to a my sql data baseConnecting To A Remote Database

there is a problem  by connecting to mysql data base here is my code bt it didint work

 

Imports System.Data.SqlClientPublic Class Form   Dim myConnection As SqlConnection   Dim myCommand As SqlCommand   Dim ra As Integer   'integer holds the number of records inserted   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)   myConnection = New SqlConnection("server=localhost;uid=weerauda;pwd=98435;database=eranda")   'you need to provide password for sql server   myConnection.Open()   myCommand = New SqlCommand("select * from  feed_data", myConnection)   ra = myCommand.ExecuteNonQuery()   MessageBox.Show(ra)   myConnection.Close()   End SubEnd Class

-reply by upeka