I have a Database in Local server named 'empData' and i want that all the data from that database to be copied to another Database, which is in the another server(web server) with the name 'empDataBackup'.
I have tried this code mysql_query("INSERT INTO empData.backup_emp SELECT * FROM empData.emp");
But it did not work as both the databases are not in the same server...(as in our case one is in local and another is in web server)..
please help..
views:
43answers:
4Dump your data out using mysqldump
and then pipe that file into mysql
to import the data somewhere else.
On server1:
mysqldump empData > empData.sql
On server2:
mysql < empData.sql
If you want to get fancy, you could use pipes and ssh to pipe the data directly from server1 to server2.
this isn't a PHP answer, but maybe it gets you started:
mysqldump --host=db1.example.com --user=db1user --password=db1pass --single-transaction myschema mytable | \
mysql --host=db2.example.com --user=db2user --password=db2pass myschema
If you can't access both hosts from one machine, you could use ssh, e.g.
ssh db1.example.com 'mysqldump --user=db1user --password=db1pass --single-transaction myschema mytable' | \
mysql --user=db2user --password=db2pass myschema
or
mysqldump --user=db1user --password=db1pass --single-transaction myschema mytable | \
ssh db2.example.com 'mysql --user=db2user --password=db2pass myschema'
You have to dump the database on the original server, then copy it to the other server, and restore it.
For each of these actions, there is a "server side" way, and a "PHPside" way. If you can, use the first one. If you can't use the second, but you need to pay extra attention to security.
Server side: 1. and 3.: use mysqldump. 2. use scp to transfer the file.
PHP side: 1. use a script to dump databases, like this one. 2. Copy the file using CURL, or place it in an available directory, protecting it somehow. 3. Retrieve the file from the second server, and just run it through a mysql_exec to restore the DB.
There's always the option of straight MySQL Replication rathe rthan relying on your PHP script to keep the databases synchronised