1. 程式人生 > 其它 >mysql 不同伺服器中的資料表合併重建ID方法

mysql 不同伺服器中的資料表合併重建ID方法

專案中有遇到不同伺服器中的資料需要合併,但是發現id重複,正常方法匯入id就會出問題。
因為趕時間,沒有找到比較好的方法,暫時用最簡單的邏輯。
步驟

1.將伺服器1中的表匯出。
2.記錄伺服器2中該表的最大id數值
3.使用python指令碼修改伺服器1中匯出的表資料
4.將伺服器1中的表匯入到伺服器2中

python指令碼修改sql檔案中的id

import os
import re

file = open(r'D:\123.txt','r+',encoding = 'utf-8')
lines = file.readlines()
file.close()
file = open(r'D:\123.txt','w+',encoding = 'utf-8')
for k, v in enumerate(lines):
    wid=re.sub('^(.{30})',r'INSERT INTO `loginfo` VALUES (74279+',v)
    file.write(wid)
file.close()

資料修改前

INSERT INTO `loginfo` VALUES (1, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022046_6.337986.jpg', 200, '1', '0.99', '[263,80,377,132]', '031', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 76, 0, '2021-01-07 18:20:46', NULL);
INSERT INTO `loginfo` VALUES (2, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022046_6.337986.jpg', 200, '1', '0.99', '[203,205,439,315]', '0.65', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 76, 0, '2021-01-07 18:20:46', NULL);
INSERT INTO `loginfo` VALUES (3, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022748_22.419955.jpg', 200, '1', '0.99', '[263,80,377,132]', '031', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 77, 0, '2021-01-07 18:27:48', NULL);
INSERT INTO `loginfo` VALUES (4, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022748_22.419955.jpg', 200, '1', '0.99', '[203,205,439,315]', '0.65', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 77, 0, '2021-01-07 18:27:48', NULL);
INSERT INTO `loginfo` VALUES (5, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022847_24.483327.jpg', 200, '1', '0.99', '[263,80,377,132]', '031', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 78, 0, '2021-01-07 18:28:47', NULL);

資料修改後

INSERT INTO `loginfo` VALUES (74279+1, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022046_6.337986.jpg', 200, '1', '0.99', '[203,205,439,315]', '0.65', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 76, 0, '2021-01-07 18:20:46', NULL);
INSERT INTO `loginfo` VALUES (74279+2, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022046_6.337986.jpg', 200, '1', '0.99', '[203,205,439,315]', '0.65', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 76, 0, '2021-01-07 18:20:46', NULL);
INSERT INTO `loginfo` VALUES (74279+3, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022748_22.419955.jpg', 200, '1', '0.99', '[263,80,377,132]', '031', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 77, 0, '2021-01-07 18:27:48', NULL);
INSERT INTO `loginfo` VALUES (74279+4, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022748_22.419955.jpg', 200, '1', '0.99', '[203,205,439,315]', '0.65', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 77, 0, '2021-01-07 18:27:48', NULL);
INSERT INTO `loginfo` VALUES (74279+5, 'yjcam', 'info', '未配置定位資訊', '未配置定位資訊', 'test', '', '未配置定位資訊', 'http://192.168.2.20/imgdata/capture/yjcam/000000/20210107/20210107022847_24.483327.jpg', 200, '1', '0.99', '[263,80,377,132]', '031', '747.70421841487', '521.8703675881', '', 'OK', '0', 0, 78, 0, '2021-01-07 18:28:47', NULL);

然後在伺服器2中使用source匯入即可。
本次匯入的資料量在5萬條,sql語句花費的時間比較長。
10萬條以下的資料使用這種方法,不趕時間的話沒問題。
不清楚mysql有沒有相關的簡便操作。