PHP/MySQL connecting 2 databases from 2 servers/hosts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsquared
    New Member
    • Nov 2011
    • 2

    #1

    PHP/MySQL connecting 2 databases from 2 servers/hosts

    Hey so I have been doing a lot of research and haven't found too much.

    I am working on a project where I need to select calendar events from 2 different calenders (on 2 different servers) and display the combined events on 1 calendar. In order to do this I am going to create a script that grabs the data from the database on server a and insert it into the database on server b.

    In order to do this I need to make 2 different mysql connections. Just to clearify, I understand how to connect to a database and display data from a table.

    For my database i just did a normal connect:
    Code:
    $hostname_mywebsite = "localhost";
    $database_mywebsite = "my_database";
    $username_mywebsite = "my_username";
    $password_mywebsite = "my_password";
    
    $mywebsite = mysql_pconnect($hostname_mywebsite, $username_mywebsite, $password_mywebsite) or trigger_error(mysql_error(),E_USER_ERROR); 
    mysql_select_db($database_mywebsite, $mywebsite);
    
    etc....
    I figured to connect to the database on the other server i'll have to create a mysql connect as well. I wasn't sure what I need to put under hostname? I know localhost won't work because it is a different server. I tried using the domain name theotherwebsite .com and the IP address, and neither worked?

    Is that the correct way of doing it? Or do I need to configure both servers to allow connections from non-local servers? And depending on the server (i.e. linux, mac, etc..) will the server be configured in a different manner?
  • Artnessde
    New Member
    • Nov 2011
    • 13

    #2
    Your script will connect the remote server which is not located on your local host with its external public IP Adress. The User you are using to connect needs to have privileges to connect from that ip adresse.

    At normal user/database configs users always only have access from localhost.

    In the mysql Database (called mysql) you will find two tables "user" and "db" ... at both tables you need to add "%" as host (replacing localhost) or if you always will connect from the same ip, set it to the ip Adress.

    After editing the tables, run an "flush privileges" SQL to realod the users into mysql deaemon.

    AND ... add "true" as last parameter to your mysql_connect to tell PHP it is a new connection so it wont' try to use the localhost resource you already connected to (read the php.net manual for that)

    Comment

    • gsquared
      New Member
      • Nov 2011
      • 2

      #3
      ok that was very helpful, are there any security measures that i need to take into consideration? it sounds like it shouldn't be a problem, because of what you said; that the permission to connect is set by ip. also i am doing this as a volunteer job, im not sure of their public ip address of the top of my head. i just used ping thewebsite.com to get the ip, that should get me the correct ip address to use for the hostname, correct?

      Also, i checked the database, the default mysql database is emtpy, they are using a database that they created, which means there are no table named 'user' or 'db'. i did a search of the database and there are no fields that contain the value, 'localhost'? any tips?

      thanks!
      Last edited by gsquared; Nov 26 '11, 09:20 PM. Reason: need to add additional info

      Comment

      • Artnessde
        New Member
        • Nov 2011
        • 13

        #4
        say:

        your script is located on "mylocalwebsite .com" and it is connecting to the database-server on "thewebsite.com ".

        Getting the target ip by PING is ok but THEY need to have YOUR IP to be set into their MySQL Server for the user and db permissions.

        Speaking about security .. well .. data will be send through the inernet ... what should i say :-) security flaws are everywhere and i bet you are not going to add SSL / X509 MySQL Auth stuff.

        Comment

        Working...