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

Copy MySql DB between servers

code green
Expert 100+
P: 1,726
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.
Jul 11 '07 #1
Share this Question
Share on Google+
2 Replies

code green
Expert 100+
P: 1,726
Hello. Anybody there?.OK. I'll go some way to solving my own problem.
Found this chunk of code
[PHP] // Source server settings
$from_user = "Put User Here";
$from_password = "Put password here";
$from_database = "Put Database name here";
$from_server = "Put Source Server here";
$from_41 = False;

// Destination Server Settings
$to_user = "Put User here";
$to_password = "Put Password here";
$to_database = "Put Database name Here";
$to_server = "Put Source Server here";
// $to_server = "localhost";

// Don't Edit below here!!!! (Unless you know what your doing).
if ($from_41) {
$theOpt = "--skip-opt ";
} else {
$theOpt = "";

$Results = system( "mysqldump $theOpt -h$from_server -u$from_user -p$from_password $from_database | mysql -u$to_user -p$to_password -h$to_server $to_database", $ResultErr );

echo "Results from MySQL import of $DatabaseName with User $User with file $TheFile:\r\n $Results\r\n $ResultErr";[/PHP]At the following address. Will try to make it work then report back!
Expand|Select|Wrap|Line Numbers
Jul 12 '07 #2

code green
Expert 100+
P: 1,726
Struggled with mysqldump all day.
Tried implementing with system() and exec() by creating a backup file (mysqldumper.sql).
The code runs, creates the file but it is empty.
[PHP]$res = exec($dump,$result,$err)[/PHP];exec() returns an empty value, the array $result is empty and $err returns 1;
system() produces similar results.
Have used the full path to mysqldump.exe and tested it is there.
[PHP]$dumpfile = '../../../../mysql/bin/';
$dumpfile .= 'mysqldump';
$dump = "$dumpfile --opt $this->name -h $this->host -u $this->user > $to_name.sql"; [/PHP]
echoing out $dump gives
Expand|Select|Wrap|Line Numbers
  1. ../../../../mysql/bin/mysqldump --opt db198743945 -h localhost -u root > mysqldumper.sql 
Can anybody help?
Jul 12 '07 #3

Post your reply

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