473,378 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to copy mysql tables between two servers?

I want to copy certain tables from server to server b.
How do I make it fastly and efficiently with PHP?
I wrote below my current start, but it may not be the best approach.
Perttu Pulkkinen, FINLAND
----------------------------
<?
$host_A = "xxx";
$user_A = "xxx";
$pass_A = "xxx";
$base_A = "xxx";

$host_B = "yyy";
$user_B = "yyy";
$pass_B = "yyy";
$base_B = "yyy";

$tables = array(""this", that", "those", "them");

$link_A = mysql_connect($host_A, $user_A, $pass_A);
or die("Yhteys tietokantaan A epäonnistui! : " . mysql_error());
$link_B = mysql_connect($host_B, $user_B, $pass_B);
or die("Yhteys tietokantaan B epäonnistui! : " . mysql_error());

mysql_select_db($base_A, $link_A) or die("Tietokannan A valinta
epäonnistui!");
mysql_select_db($base_B, $link_B) or die("Tietokannan B valinta
epäonnistui!");

foreach($tables as $table)
{
$qid_A = mysql_query("SELECT * FROM $table", $link_A)
or die("Haku tietokantaan A epäonnistui! : " . mysql_error());
$qid_DEL_B = mysql_query("DELETE FROM $table", $link_B):
or die("Taulun tyhjennys tietokannassa B epäonnistui! : " .
mysql_error());

// THIS PART IS MAYBE STUPID, PAINFUL-TO-DO AND UNEEFICIENT

while($row = mysql_fetch_row($qid_A))
{
$stuff = somehow_formulate_row($row); // ?????? not implemented
$qid_B = mysql_query("INSERT INTO $table $stuff") or die("");
}
}
?>
Jul 17 '05 #1
5 11515
Perttu Pulkkinen wrote:
I want to copy certain tables from server to server b.
How do I make it fastly and efficiently with PHP?
I wrote below my current start, but it may not be the best approach.


The following query shows you the query required to create the table:
SHOW CREATE TABLE tablename

But the fastest way to dump and load a database is using the mysqldump
command line utility eg

mysqldump -u username -p databasename1 > filename.txt

will dump all tables database structure and data into a file containing all
the insert queries. There are additional flags that can speed this up.

mysql -u username -p databasename2 < filename.txt

will then load this into the other database. After hitting the enter key in
both cases you'll be prompted for your password.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
"Chris Hope" <bl*******@electrictoolbox.com> wrote
But the fastest way to dump and load a database is using the mysqldump


I would like to do with php the best I can plus I dont want to copy all
tables.
Of course
1) if with flags I could select the tables I want to dump and
2) if manage to use shell correctly through php and
3) if manage to pick up that dump file to php
this approach would work.
Jul 17 '05 #3
Original problem: How to copy ONLY CERTAIN mysql tables between TWO servers
USING PHP? This approach is working, but its is slow and it can easily
exceed php maximum executution time. (Could maximum time extended only for
current page request?)

-perttu pulkkinen, jkl
<?php

$host_A = "x";
$user_A = "x";
$pass_A = "x";
$base_A = "x";

$host_B = "y";
$user_B = "y";
$pass_B = "y";
$base_B = "y";

$tables = array("qwer", "wer", "ert", "rty");
$table_prefix_B = "copy_of_";

// -------------------------------------------------------
$table_prefix_B = trim($table_prefix_B);
if($host_A == $host_B && $base_A == $base_B && $table_prefix_B =="")
die("Samaa taulua ei voi kopioida samannimiseksi saman tietokannan
sisällä!");
// -------------------------------------------------------
$link_A = mysql_connect($host_A, $user_A, $pass_A)
or die("Yhteys tietokantaan A epäonnistui! : " . mysql_error());
$link_B = mysql_connect($host_B, $user_B, $pass_B)
or die("Yhteys tietokantaan B epäonnistui! : " . mysql_error());
// -------------------------------------------------------
mysql_select_db($base_A, $link_A) or die("Tietokannan A valinta
epäonnistui!");
mysql_select_db($base_B, $link_B) or die("Tietokannan B valinta
epäonnistui!");
// -------------------------------------------------------
foreach($tables as $table)
{
$table_B = $table_prefix_B.$table;

$show_create = "SHOW CREATE TABLE $table";
// echo "<p>".$show_create."</p>";
$qid_SHOW_A = mysql_query($show_create, $link_A)
or die("Taulukuvauksen haku tietokannasta A epäonnistui! : " .
mysql_error());
// -------------------------------------------------------
$drop = "DROP TABLE IF EXISTS $table_B";
// echo "<p>".$drop."</p>";
$qid_DROP_B = mysql_query($drop, $link_B)
or die("Taulun poisto tietokannasta B epäonnistui! : " . mysql_error());
// -------------------------------------------------------
$create_row = mysql_fetch_row($qid_SHOW_A)
or die("Kuvausrivin haku epäonnistui! : " . mysql_error());
$create = $create_row[1];
$create = str_replace("CREATE TABLE `$table`", "CREATE TABLE `$table_B`",
$create)
or die("Taulunimen muodostus epäonnistui!");
// echo "<p>".$create."</p>";
$qid_CREATE_B = mysql_query($create, $link_B)
or die("Taulun luonti tietokantaan B epäonnistui! : " . mysql_error());
// -------------------------------------------------------
$select = "SELECT * FROM $table";
// echo "<p>".$select."</p>";
$qid_SELECT_A = mysql_query($select, $link_A)
or die("Taulun datan haku tietokannasta A epäonnistui! : " .
mysql_error());
// -------------------------------------------------------
while($row = mysql_fetch_assoc($qid_SELECT_A))
{
$insert = formulate_insert_row($table_B, $row, $link_B);
// echo "<p>".$insert,"</p>";
$qid_INSERT_B = mysql_query($insert, $link_B)
or die("Rivin kopiointi tauluun B epäonnistui! : " . mysql_error());
}
}
// -------------------------------------------------------
function formulate_insert_row($table, $row, $link)
{
$fields=""; $values = "";
$flag=true;
foreach($row as $key=>$value)
{
if($flag) $flag=false; else { $values.= ","; $fields.= ","; }
$values.= "'".mysql_real_escape_string($value, $link)."'";
$fields.= $key;
}
return " INSERT INTO $table($fields) VALUES($values) ";
}
// -------------------------------------------------------
?>
Jul 17 '05 #4
Perttu Pulkkinen wrote:
"Chris Hope" <bl*******@electrictoolbox.com> wrote

But the fastest way to dump and load a database is using the mysqldump

I would like to do with php the best I can plus I dont want to copy all
tables.
Of course
1) if with flags I could select the tables I want to dump and
2) if manage to use shell correctly through php and
3) if manage to pick up that dump file to php
this approach would work.


Can you give us an idea of why you want to do it this way? Doing this
will eventually have the databases out of sync with each other. So, why
not back up the originating database and restore it to the remote
location on some pre-determined interval... now all tables will be
identical... and probably much faster than moving table data from one
database to another...

Michael Austin
Sr DBA.
Jul 17 '05 #5
"Michael Austin" <ma*****@firstdbasource.com> kirjoitti viestissä
news:lu*****************@newssvr24.news.prodigy.co m...
Can you give us an idea of why you want to do it this way? Doing this
will eventually have the databases out of sync with each other. So, why
not back up the originating database and restore it to the remote
location on some pre-determined interval... now all tables will be
identical... and probably much faster than moving table data from one
database to another...


I understand your point. But in our system this "database" is yet a
collection of
tables in one real "master database". Thats why I need only dump those
related tables.

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Luc Foisy | last post by:
Last week many of our server and client servers had a power problem. Not = quite sure how the servers were handled, wasn't on site, but I don't = think some of these servers got shut down...
6
by: Matt Liverance | last post by:
I REALLY dont want to switch to oracle :( but I cant get these tables working any faster. I've got 2 dedicated servers, each with a slave, all run 32gig 15k rpm raid 5 on u320 perc raid...
1
by: adiavr | last post by:
Hi, I have read up on MySQL failover/replication and decided that weren't many cons to setting up two way replication where two servers are both master and slave to eachother. Here's the my.cnf:...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
4
by: Federico | last post by:
Hi everybody, I'm evaluating the possibility of using MySQL 5.0 as a database backend for an application we are doing and I'd like to have hardware requirements rough estimates and/or real world...
2
code green
by: code green | last post by:
How do I update the same MySql database from server 1 to server 2 using a php script. I can make the connection between the two and 'see' the tables using SHOW TABLES. Now I want to replace the...
3
by: 2401 members, members can post | last post by:
Dear Madams and Sirs, Ever had to split a website + SQL Tables ? Have a UNIX pc-linux-gnu on i686 + FEDORA 1) We have to split a sub-domain for the main domain name. 2) We have to build a...
0
by: jllanten | last post by:
I will appreciate any help you can provide me. In the company where i work we have a project which creates about 4-5M records daily of stats. We're currently storing this data in a db named...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.