简体   繁体   English

如何使用PHP将数据库从一台服务器复制到另一台服务器?

[英]How to copy a Database from one server to another server in PHP?

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'. 我在本地服务器上有一个名为'empData'的数据库,并且我希望将该数据库中的所有数据复制到另一个数据库中,该数据库位于另一个名为'empDataBackup'的服务器(Web服务器)中。 I have tried this code mysql_query("INSERT INTO empData.backup_emp SELECT * FROM empData.emp"); 我已经尝试过这段代码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.. 但这没有用,因为两个数据库都不在同一服务器上...(例如,在我们的例子中,一个在本地,另一个在Web服务器中)..请帮助..

Dump your data out using mysqldump and then pipe that file into mysql to import the data somewhere else. 使用mysqldump转储您的数据,然后将该文件通过管道传输到mysql以将数据导入其他地方。

On server1: 在server1上:

mysqldump empData > empData.sql

On server2: 在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. 如果想花哨的话,可以使用管道和ssh将数据直接从server1管道传输到server2。

this isn't a PHP answer, but maybe it gets you started: 这不是PHP的答案,但也许它可以帮助您入门:

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, eg 如果无法从一台计算机访问两个主机,则可以使用ssh,例如

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'

始终可以选择直接MySQL复制,而不是依靠PHP脚本来保持数据库同步

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. 对于每种操作,都有一种“服务器端”方式和一种“ PHPside”方式。 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. 服务器端:1.和3 .:使用mysqldump。 2. use scp to transfer the file. 2.使用scp传输文件。

PHP side: 1. use a script to dump databases, like this one . PHP方面:1.使用脚本来转储数据库,就像这样 2. Copy the file using CURL, or place it in an available directory, protecting it somehow. 2.使用CURL复制文件,或将其放置在可用目录中,以某种方式对其进行保护。 3. Retrieve the file from the second server, and just run it through a mysql_exec to restore the DB. 3.从第二台服务器检索文件,然后通过mysql_exec运行它以恢复数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP MYSQL中将数据库表和每条记录从一个数据库服务器复制到另一个数据库服务器?&gt; - How to copy database tables and each record from one database server to another database server in PHP MYSQL ?> 使用PHP将映像从一台服务器复制到另一台服务器 - copy images from one server to another in PHP 将文件从一台服务器复制到 php 中的另一台服务器 - Copy file from one server to another server in php 如何在PHP中将tgz文件从一台服务器复制到另一台Linux服务器? - How to copy tgz file one server to another linux server in php? 将mysql数据库从一台服务器自动复制到另一台PHP cron Job - Copy a mysql database from one server to another PHP cron Job automated 如何在PHP和MySQL中将几条记录从一台服务器上的表复制到另一台服务器上具有相同结构的表? - How to copy few records from a table on one server to the table with same structure on another server in PHP and MySQL? 使用php将数据库从服务器复制到服务器 - copy database from server to server with php PHP-从一个服务器到另一个服务器的内存缓存复制键/值 - PHP - memcached copy key/value from one server to another 将文件从一台DV服务器复制到另一台PHP - Copy files from one DV server to another PHP PHP从一个服务器复制文件到另一个服务器(收到错误) - PHP Copy File(s) From One Server to Another (getting an error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM