By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

PHP/MySQL connecting 2 databases from 2 servers/hosts

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. $hostname_mywebsite = "localhost";
  2. $database_mywebsite = "my_database";
  3. $username_mywebsite = "my_username";
  4. $password_mywebsite = "my_password";
  6. $mywebsite = mysql_pconnect($hostname_mywebsite, $username_mywebsite, $password_mywebsite) or trigger_error(mysql_error(),E_USER_ERROR); 
  7. mysql_select_db($database_mywebsite, $mywebsite);
  9. 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 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?
Nov 26 '11 #1
Share this Question
Share on Google+
3 Replies

P: 13
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 manual for that)
Nov 26 '11 #2

P: 2
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 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?

Nov 26 '11 #3

P: 13

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

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.
Nov 26 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.