简体   繁体   English

如何在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?

I've two same MySQL databases present at two different servers. 我在两个不同的服务器上有两个相同的MySQL数据库。 Initially both the databases have the exactly same structure and contained the same data. 最初,这两个数据库具有完全相同的结构并包含相同的数据。

But later on few new records(new users) got added to both the tables. 但是后来又有一些新记录(新用户)被添加到两个表中。 This created the difference in the data present in these tables. 这造成了这些表中数据的差异。

Now the issue is somehow the old data(which is present since when the structure and data of both the databases were same) from one table got tampered by some unauthorized means and in one field contains blank values. 现在的问题是,某个表中的旧数据(在两个数据库的结构和数据相同时就存在)由于某种未经授权的手段而被篡改,并且在一个字段中包含空白值。

Now I want that data back as of previous(like initial). 现在,我希望将这些数据恢复为先前的状态(例如初始值)。 For this I've to fire a query which would take the missing data from the same table from one server and add it to the same table on another server where the issue of data tampering occurred. 为此,我必须触发一个查询,该查询将从一台服务器的同一表中获取丢失的数据,并将其添加到发生数据篡改问题的另一台服务器的同一表中。

If there is some way by writing some script in PHP which could do this job that will also be fine for me. 如果可以通过某种方式用PHP编写一些脚本来完成这项工作,那对我也很好。

Can some one please suggest me how should I achieve this? 有人可以建议我如何实现这一目标吗?

If you want any more details regarding the issue I'm facing please do let me know. 如果您想了解我所面临的问题的更多详细信息,请告诉我。

Thanks. 谢谢。

Why not export the data from the other server? 为什么不从其他服务器导出数据?

Then import the table into the current server (with different name or make new database) 然后将表导入到当前服务器(使用其他名称或创建新数据库)

UPDATE UPDATE

No it will not replace the table. 不,它不会替代桌子。 You can create another database and import it into the new database and not the existing. 您可以创建另一个数据库并将其导入到新数据库中,而不是现有数据库中。

Or you edit the export file and changed the name then import it. 或者,您编辑导出文件并更改名称,然后将其导入。

Example : 范例

If this is your export file: 如果这是您的导出文件:

CREATE TABLE IF NOT EXISTS `Customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Change the name (notice I change customer to customer2) 更改名称(注意,我将客户更改为customer2)

THEN do the import. 然后执行导入。

CREATE TABLE IF NOT EXISTS `Customer2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

UPDATE 2 更新2

Now you have current table with a blank column, and a "backup" table2. 现在,您有一个带有空白列的当前表和一个“备份”表2。

Both tables should have a unique id or some sort. 两个表都应具有唯一的ID或某种形式。

This is the basic scenario: 这是基本方案:

Loop through all the records in table1 get the unique id column value. 遍历table1中的所有记录以获得唯一的id列值。

Use that id to look up the value of the blank column in table2. 使用该ID查找table2中空白列的值。

Update the blank column in table1 from the value in table2. 从表2中的值更新表1中的空白列。

SELECT `id` FROM `table1` WHERE 1

SELECT `blankcolumn` FROM `table2` WHERE `id` = $row['id']

UPDATE `table1` SET `blankcolumn` = `table2`.`blankcolumn`

I would create a RESTful API, allowing one server to GET, PUT, DELETE stuff to the other server using JSON. 我将创建一个RESTful API,允许一台服务器使用JSON将内容获取,放置,删除内容到另一台服务器。

So one server has a URL that you can submit some JSON (or XML if you really want) data to it. 因此,一台服务器具有一个URL,您可以向其提交一些JSON(或XML)数据。

Then the other server performs a CURL request and sends the data over. 然后,另一台服务器执行CURL请求并发送数据。

You probably want to wait until you have enough data to send at once so you are not constantly making requests between the servers. 您可能要等到一次有足够的数据发送后再进行操作,这样就不会在服务器之间不断发出请求。

You can password protect the system also so nobody else that figures this out can edit your database :P 您也可以使用密码保护系统,因此没有其他人可以编辑您的数据库:P

There is a lot to go into, but I think if you look up how to create a REST API in PHP, and also how to make CURL requests, you will find it is quite straightforward. 有很多事情要做,但是我认为,如果您查找如何在PHP中创建REST API,以及如何进行CURL请求,您会发现它非常简单。 You can also communicate to such a system using JavaScript AJAX calls which opens some more possibilities. 您还可以使用JavaScript AJAX调用与此类系统进行通信,这打开了更多可能性。

Some database types and database service providers (they run the db for you) can scale databases to multiple instances to do this sort of thing. 某些数据库类型和数据库服务提供程序(它们为您运行db)可以将数据库扩展到多个实例以执行此类操作。 That might be worth looking into as well. 这也许也值得研究。 There is usually a delay before the databases match each other - this explains a lot of weirdness on Social Media and Auction/Classifieds websites where a post might not show up for you for a little while. 数据库之间相互匹配通常会有所延迟-这解释了社交媒体和拍卖/分类网站上的很多怪异之处,这些帖子可能会在一段时间内不为您显示。

If the data has to do with something really really important like financial transactions, where having some money go missing for a while is unacceptable, you may need to get much more serious about this. 如果数据与诸如金融交易之类的真正重要的事情有关,那么有些钱会丢失一段时间是不可接受的,那么您可能需要对此更加认真。

暂无
暂无

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

相关问题 将多个记录从一个表复制到另一个(相同的结构)(PHP / MySQL) - copying multiple records from one table to another (same structure) (PHP/MySQL) 将mysql表内容从一台服务器复制到另一台服务器 - Copy a mysql table content from one server to another server 将数据库表从一台服务器发送到另一台服务器中的另一数据库表PHP MySQL - Send database table from one server to another database table in different server PHP MySQL 如何在mysql中使用php插入动态输入值将一张表同时复制到另一张表 - How to insert dynamic input values copy one table to another table at the same time in mysql using php 从一个表复制所有信息,然后使用PHP将其插入到不同服务器上的另一表中 - Copy all information from one table and insert it into another table on different server using PHP 使用PHP将服务器中的mysql复制表到本地服务器 - mysql copy table in server to local server with PHP 如何从一个表数据复制列到另一表并在mysql中同时插入更多列? - how to copy column from one table data to another table and insert more columns same time in mysql? 将具有1.3 MIL记录的表从一台MysQL服务器移动到另一台(在不同的物理服务器上) - Move Table with 1.3 MIL records from one MysQL Server to Another (on different physical servers) MySQL:将postid从一个表复制到同一数据库中的另一个表 - MySQL: Copy postid from one table to another in same database MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM