connecting to another db from one server

1.3k views Asked by At

Ok so I have a client that is trying to move half his site to another server...in this i still need to pull data from both databases. SO i have the new site and i need to do a mysql db query on the old site so i can include the old nav....but when i do

  <?php include("http://www.othersite.com/includes/db.php"); ?>
  <?php include("http://www.othersite.com/includes/nav.php"); ?>

I get

Warning: mysql_query(): Access denied for user 'www-data'@'localhost'
(using password: NO) in /vol/www/othersite.com/public_html/includes/nav.php
on line 223 Warning: 

How do i access another db from the new site and not allow it to interfere with the new db connection

4

There are 4 answers

0
Mchl On

You'd need to show us the actual code used to connect to these dataases (with passwords blanked out). But even without it I can tell you that the remote server is NOT called localhost.

It seems you're including these files from the remote site. It won't work this way, since the code is still executed on local server.

You must also make sure, that the remote MySQL allows remote connections, and that there is a MySQL user account with appropriate privileges created.

See: http://dev.mysql.com/doc/refman/5.5/en/privilege-system.html

0
Andrejs Cainikovs On

...in this i still need to pull data from both databases.

Good luck with this!

The way I would do this:

  1. Copy code to server #2
  2. Adjust it to fetch data from server #1
  3. Change DNS record from ip of server #1 to ip of server #2
  4. Wait 1 day
  5. Give blocking 'server maintenance' for 5 minutes on server #1
  6. rsync db from server #2 to #1 / adjust server #2 to fetch data from itself
  7. Remove 'server maintenance'
  8. Sell server #1 on ebay and get boozed

NOTE: Sure, if you are talking about some private host.. There are far more successful yet complicated mechanisms to do the same without putting server offline.

0
Álvaro González On

Load http://www.othersite.com/includes/db.php in your browser. You'll notice that you cannot see the PHP code: what you obtain is the output of the PHP code. That means than changing all your ìnclude constructs to point to a remote HTTP server will simply break your site since you'll no longer have access to the source code.

Now, answering to your question, if you want to connect to a remote database you must find wherever you have the DB password and change the data: localhost with the new server address, www-data with your new user, etc. However, you probably need to configure the remote MySQL server so it accepts external connections from your new server's IP address.

0
SERPRO On

you can use:

$newLink = mysql_connect($host, $user, $password);
mysql_select_db($db, $newLink);

and the you should add the $newLink to your queries so it will not use the "old" database link connection, like:

mysql_query('SELECT * FROM USERS', $newLink);

Hope it helps

P.S. It'll be more easy if you put the code from db.php and nav.php