I am working in a corporate environment and need to access a mySQL database ouside of the corporate network and therefore need to go thru our proxy server to get to the outside. Using a mysql_connect("externalsite", "user", "password") just does not work. Somehow I need to be able to set the proxy so that all further requests go thru the proxy and then on to the external database.

I have been searching high and low and not found any answers. I have even heard that this is not possible through php as it does not support setting proxy for database access.

I have seen using fsockopen which allows accessing web pages etc but not opening and running queries on an external database.

We are using php version 4.3.0

Can anybody please help before I pull all my hair out?

    a month later

    Hey dude, Are you still wanting help?

    If so:
    a) What proxy server is being used? (This will help with specific answers)
    b) Generally what needs to happen is:

    • On the proxy server, setup a port that directly connects to a specific host (in this case the external database server). There are three bits (at minimum) that you will need to configure.
      1) destination host eg: db.blah.com
      2) destination port eg: 3306
      3) proxy port eg: 13306

    • On the php app, connect to yourProxyAddress:13306 as if it were actually the db server.

    So the above solution was using the proxy as you asked. Alternativley, you could set up routing/port forwarding. If you're using linux, this solution is likley to be way faster and more reliable than any proxy solution you could use. You basically use the same info as the proxy solution above, but you have more control over how it happens [note that traffic accounting will not be as easy to do... but not out of the question 😉]. When you read the howtos, this looks really complicated, but really isn't all that hard.

    Basically, you need to make sure that specific ports can establish connections out and only established connections on that port can come in. IE don't allow outsiders to connect into you. 😉

    How to do it, depends a lot on your setup. So rather than me telling you lots of stuff you don't need/want to know or already know, I'll stop here and if you want more information, just let me know what you want to know and what stuff you are using. eg which OS (on the proxy server), proxy server etc

    As a side note. If you are using windows, there used to be some really useful tools for this type of thing by a group called AnalogX
    I don't know if there will be anything useful for what you want. I needed it in ages, but it might help.

    Hmmm, just looking at what's there. It may be worth trying:
    http://www.analogx.com/contents/download/network/pmapper.htm

    Right, I really have to get back to work. Feel free to ask if you want more information.

      Just incase someone suggests it. You could also use http tunneling (or ssh tunneling if the proxy supports ssh).

      ssh tunneling may work well, but is unlikley to work well through a proxy. http tunneling will almost definitley work but it will be remarkably slow and unreliable. There will also be a potential security risk since tunneling works by connecting to someone else on the outside who doesn't have the proxy problem, and then forwards your connection to the desired location.

      Anyway, you've got lots of options anyway 😉

        Write a Reply...