Connect to a mysql database on another webserver?

Posted on 2009-12-31
Last Modified: 2013-12-12
Hello, I have a web server running a php web app off a MySQL database that I created a while back. (the database is constantly being updated every minute or so) There's a second website hosted on another server (different location and ip) that I want to pull some information from the first server and display (and possibly modify). I thought I'd just open the mysql port on the first server's firewall (available only to the second server's IP), and connect via PHP directly to the first server from the second, but I've never done this before so I wanted to see if anyone who's had experience doing this type of thing knows if this is the most secure/efficient way to do what I'm trying to do, or if there's a better way to do this.
Question by:kendalltech
    LVL 32

    Accepted Solution

    You've got the right idea. It's pretty simple and striaght forward. The onlything to check is to make sure that the OUTBOUND firewall of server A can connect to server B (where B is the remote  box).

    I have done this with PHP and with VB6 / ODBC. It's really straight forward.
    LVL 7

    Expert Comment

    You can setup MySQL to take connections on any port, you don't need to use the standard 3306.

    Also you can setup to only allow connections from certain IP addresses, so here you could specify your other webserver.

    Setup a new user on your MySQL server with only access allowed from the second IP.

    Usually MySQL users are localhost only.
    $link = mysql_connect('', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';

    Open in new window

    LVL 7

    Expert Comment

    You can also restrict was statements the remote user can execute.

    "The GRANT statement specifies that this user is only allowed to run a limited amount of statements on the MySQL server. This user will be allowed to: select records, insert records, update records, delete records, create databases, and DROP DATABASEs. More importantly, this user is not permitted to create users and set privileges.

    You could further reduce a user's privileges by removing other items from the GRANT statement. The best policy here is that the user should only be given permission to access the functions that are necessary to perform their tasks."
    LVL 2

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now