[英]Inserting only new records from one db server to another
We have a phone system database on one server but we want an updated clone on our production server. 我们在一台服务器上有一个电话系统数据库,但是我们希望在生产服务器上有一个更新的克隆。
I've dumped the original and imported it to our local production server, which is great, but now I have to go about keeping it updated with any daily changes on the other server. 我已经将原始文件转储并导入到我们的本地生产服务器中,这很棒,但是现在我必须继续进行更新,以确保其他服务器上的任何日常更改都可以进行更新。
I'm trying to create a php script that will simply check each table on the other server and then insert any new records in each table into the cloned table on our production database. 我正在尝试创建一个php脚本,该脚本将简单地检查另一台服务器上的每个表,然后将每个表中的任何新记录插入生产数据库中的克隆表中。
Below is a test select/insert block. 下面是一个测试选择/插入块。 The select query works with no errors but my insert returns an error as I expected, because I believe I'm using
NOT EXISTS
incorrectly. 选择查询没有错误,但我的插入操作返回了我所期望的错误,因为我认为我使用了错误的
NOT EXISTS
。 I don't know if that's even the best route to take. 我不知道这是否是最好的选择。
The session table has about 35 columns so I'm looking for the best way to go about this without having to declare every column if possible. 会话表大约有35列,因此我正在寻找最好的方法,而不必在可能的情况下声明每一列。 The only thing I though of is the
SESSION.SESSIONID
field which is the primary key. 我唯一要注意的是
SESSION.SESSIONID
字段,它是主键。 So my logic below was, if the sessionid from the phone server doesn't exist yet in the prod server, insert the whole record
. 所以我的逻辑是,
if the sessionid from the phone server doesn't exist yet in the prod server, insert the whole record
。
So obviously my syntax is wrong on the insert, just looking for some guidance on how to insert all new records from server1.session to server2.session when this script is run every 15 minutes. 因此,显然我的语法在插入上是错误的,只是在每15分钟运行一次此脚本时寻找有关如何将所有新记录从server1.session插入到server2.session的指导。
//Defining credentials
$servername = "";
$username = "";
$password = "";
$servername2 = "";
$username2 = "";
$password2 = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
$conn2 = new mysqli($servername2, $username2, $password2);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Check connection2
if ($conn2->connect_error) {
die("Connection failed: " . $conn2->connect_error);
}
echo "Connected successfully";
//Query to select * from Session table on server 1
$query1 = "select * from cdrdb.session";
$results = mysqli_query($conn1, $query1);
foreach ($results as $r => $result) {
}
$stmt1 = mysqli_prepare($conn2, "insert into ambition.session a where not
exists(a.SESSIONID)");
mysqli_stmt_execute($stmt1) or die(mysqli_error($conn2));
I think the most appropriate way would have to be with a "log" table containing IDs. 我认为最合适的方法是使用包含ID的“日志”表。
However, I imagine the rows IDs are primary keys. 但是,我认为行ID是主键。
So, you want to do a script where you : 因此,您想在以下位置执行脚本:
SELECT id FROM your_table WHERE id = $id
SELECT id FROM your_table WHERE id = $id
Edit : Basically, you should do something like this : 编辑:基本上,您应该这样做:
$source_db = array(
'host' => 'source_host',
'name' => 'source_name',
'user' => 'source_user',
'pass' => 'source_pass'
);
$dest_db = array(
'host' => 'dest_host',
'name' => 'dest_name',
'user' => 'dest_user',
'pass' => 'dest_pass'
);
$source_conn = new mysqli($source_db['host'], $source_db['user'], $source_db['pass'], $source_db['name']);
$dest_conn = new mysqli($dest_db['host'], $dest_db['user'], $dest_db['pass'], $dest_db['name']);
// Get all rows from source database
$source_data = $source_conn->query("SELECT * FROM source_table");
// Loop on the results
while($source_item = $source_data->fetch_assoc()) {
// Check if row exists in dest database
$row_exists = $dest_conn->query("SELECT id FROM dest_table WHERE id = '".$source_item['id']."' ");
// If the query returns FALSE, the row does not exist
if(!$row_exists) {
// Insert the new row in dest database
$dest_conn->query("INSERT INTO dest_table (id, name) VALUES ('".$source['id']."', '".$source['name']."' ");
}
}
In SQL the EXISTS clause (?) is a subquery and looks like this (...WHERE EXISTS(SELECT ... FROM table ...)) Also you didn't state in the insert statement what to insert. 在SQL中,EXISTS子句(?)是一个子查询,看起来像这样(... WHERE EXISTS(SELECT ... FROM table ...))此外,您没有在insert语句中声明要插入的内容。 So i would do something like this:
所以我会做这样的事情:
INSERT ambition.session a (col_a, col_b, ...) SELECT col_a, col_b, ... FROM cdrdb.session c WHERE NOT EXISTS (SELECT 1 FROM ambition.session a2 WHERE a2.SESSIONID = c.SESSIONID);
Or you can do an INSERT IGNORE ... which will ignore the insertion if it would result in duplicate key and omit the whole WHERE clause, but i don't know if this would bring any performance issue. 或者,您可以执行INSERT IGNORE ...,如果将导致重复的键并忽略整个WHERE子句,则将忽略插入,但是我不知道这是否会带来任何性能问题。
I read that you may leave out the column names, but i don't know if it is true. 我读到您可能会省略列名,但是我不知道这是不是真的。 Then:
然后:
INSERT ambition.session a SELECT * FROM cdrdb.session c WHERE NOT EXISTS (SELECT 1 FROM ambition.session a2 WHERE a2.SESSIONID = c.SESSIONID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.