473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Tietokanna n A valinta
epäonnistui!");
mysql_select_db ($base_B, $link_B) or die("Tietokanna n B valinta
epäonnistui!");

foreach($tables as $table)
{
$qid_A = mysql_query("SE LECT * FROM $table", $link_A)
or die("Haku tietokantaan A epäonnistui! : " . mysql_error());
$qid_DEL_B = mysql_query("DE LETE 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_formula te_row($row); // ?????? not implemented
$qid_B = mysql_query("IN SERT INTO $table $stuff") or die("");
}
}
?>
Jul 17 '05 #1
5 11536
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*******@elec trictoolbox.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_pre fix_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("Tietokanna n A valinta
epäonnistui!");
mysql_select_db ($base_B, $link_B) or die("Tietokanna n B valinta
epäonnistui!");
// -------------------------------------------------------
foreach($tables as $table)
{
$table_B = $table_prefix_B .$table;

$show_create = "SHOW CREATE TABLE $table";
// echo "<p>".$show_cre ate."</p>";
$qid_SHOW_A = mysql_query($sh ow_create, $link_A)
or die("Taulukuvau ksen haku tietokannasta A epäonnistui! : " .
mysql_error());
// -------------------------------------------------------
$drop = "DROP TABLE IF EXISTS $table_B";
// echo "<p>".$drop ."</p>";
$qid_DROP_B = mysql_query($dr op, $link_B)
or die("Taulun poisto tietokannasta B epäonnistui! : " . mysql_error());
// -------------------------------------------------------
$create_row = mysql_fetch_row ($qid_SHOW_A)
or die("Kuvausrivi n haku epäonnistui! : " . mysql_error());
$create = $create_row[1];
$create = str_replace("CR EATE TABLE `$table`", "CREATE TABLE `$table_B`",
$create)
or die("Taulunimen muodostus epäonnistui!");
// echo "<p>".$create." </p>";
$qid_CREATE_B = mysql_query($cr eate, $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($se lect, $link_A)
or die("Taulun datan haku tietokannasta A epäonnistui! : " .
mysql_error());
// -------------------------------------------------------
while($row = mysql_fetch_ass oc($qid_SELECT_ A))
{
$insert = formulate_inser t_row($table_B, $row, $link_B);
// echo "<p>".$insert," </p>";
$qid_INSERT_B = mysql_query($in sert, $link_B)
or die("Rivin kopiointi tauluun B epäonnistui! : " . mysql_error());
}
}
// -------------------------------------------------------
function formulate_inser t_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*******@elec trictoolbox.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*****@firstd basource.com> kirjoitti viestissä
news:lu******** *********@newss vr24.news.prodi gy.com...
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
1735
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 gracefully. but anyways that = shouldn't matter to my question I ran myisamchk on the data directories and I get a large report = containing things such as myisamchk: MyISAM file /usr/data/mysql/qbslive/MANIFESTSPOOL.MYI myisamchk: warning: 1 clients...
6
22542
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 cards, dell 2600/4600's with single channel backplanes (new ones will have dual channel) All have 2 gig of ram, but I've never seen mysql use more than 300mb of ram.
1
2201
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: log-bin log-slave-updates server-id=2 master-host=10.0.0.2 master-user=repl
39
8436
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 of stammering and sputtering, and managed to pull out something I heard a couple of years back - that there was no real transaction safety in MySql. In flight transactions could be lost.
15
4651
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 communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
4
4900
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 experience from people that are already running similar dimensioned database/application. As a rought estimate the database has around 80 tables 4-5 of these are supposed to grow up to 5.000.000 records another 30% will be in the 100.000 range...
2
1960
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 data of certain tables in server 2 with server 1. I've looked at mysqldump and mysqlimport but can't find any PHP examples between two servers.
3
1831
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 transfer and merge from Tables hosted on Server 1 TO Tables hosted on Server 2.
0
2169
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 'summary' which contains all the stats data for up to 15 days. After that we begin to move the data in a daily basis to a single 'archived like' unique table in another fast performance mysql instance. Why we do this ? to respect the 80%-20% rule: 80% of...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.