2016-06-30 17 views
-2

MySQLからDB2にデータを移行する必要があります。両方のDBが稼動しています。MySQLからDB2にデータを移行する最も簡単な方法は何ですか

私が望む理想的には私は--no-create-info --extended-insert=FALSE --complete-insertmysqldumpしようとしたと出力にいくつかの変更("に例えば変動 `)で、私は満足のいく結果を得るが、時には、私は奇妙な例外を持って、 does not have an ending string delimiter. SQLSTATE=42603

のようなできるだけ一般的なようであるルーチンを持っていますが、ここでは一例として、のは、私が見えDB2テーブルを持っているとしましょうたい:

db2 => describe table "mytable" 

           Data type      Column 
Column name      schema Data type name  Length  Scale Nulls 
------------------------------- --------- ------------------- ---------- ----- ------ 
id        SYSIBM BIGINT      8  0 No  
name       SYSIBM VARCHAR     512  0 No  

    2 record(s) selected. 

され、そのMySQLの対応

mysql> describe mytable; 
+-------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+-------+--------------+------+-----+---------+----------------+ 
| id | bigint(20) | NO | PRI | NULL | auto_increment | 
| name | varchar(512) | NO |  | NULL |    | 
+-------+--------------+------+-----+---------+----------------+ 
2 rows in set (0.01 sec) 

DB2データベースとMySQLデータベースをmydbとします。私は

mysqldump -uroot mydb mytable --no-create-info --extended-insert=FALSE --complete-insert | # mysldump, with options (see below): # do not output table create statement # one insert statement per record# ouput table column names 
sed -n -e '/^INSERT/p' |  # only keep lines beginning with "INSERT" 
sed 's/`/"/g' |    # replace ` with " 
sed 's/;$//g' |    # remove `;` at end of insert query 
sed "s/\\\'/''/g"   # replace `\'` with `''` , see http://stackoverflow.com/questions/2442205/how-does-one-escape-an-apostrophe-in-db2-sql and http://stackoverflow.com/questions/2369314/why-does-sed-require-3-backslashes-for-a-regular-backslash 

をすれば

は今、私が取得:

INSERT INTO "mytable" ("id", "name") VALUES (1,'record 1') 
INSERT INTO "mytable" ("id", "name") VALUES (2,'record 2') 
INSERT INTO "mytable" ("id", "name") VALUES (3,'record 3') 
INSERT INTO "mytable" ("id", "name") VALUES (4,'record 4') 
INSERT INTO "mytable" ("id", "name") VALUES (5,'" "" '' '''' \"\" ') 

この出力に含まには、DB2のクエリとして使用することができ、それがうまく動作します。

これをより効率的に/一般的に解決する方法はありますか?その他の提案はありますか?

+2

、いくつかのデータサンプルおよびCREATE TABLEまたはスキーマをファイルをアップロード私たちが助けるために –

+0

@data_henrikがいくつかの追加情報を追加しました。助けてくれるといいですか? – John

+2

.csvにダンプしてから、DB2でインポートまたはロードを使用してみませんか? –

答えて

0

少し遊んだ後、私はかなり一般的で、堅牢でスケーラブルであると信じている次のルーチンに来ました。

1次のコマンドを実行します。

mysqldump -uroot mydb mytable --no-create-info --extended-insert=FALSE --complete-insert | # mysldump, with options (see below): # do not output table create statement # one insert statement per record# ouput table column names 
sed -n -e '/^INSERT/p' |  # only keep lines beginning with "INSERT" 
sed 's/`/"/g' |    # replace ` with " 
sed -e 's/\\"/"/g' |   # replace `\"` with `#` (mysql escapes double quotes) 
sed "s/\\\'/''/g" > out.sql # replace `\'` with `''` , see http://stackoverflow.com/questions/2442205/how-does-one-escape-an-apostrophe-in-db2-sql and http://stackoverflow.com/questions/2369314/why-does-sed-require-3-backslashes-for-a-regular-backslash 

注:ここで問題;とは異なりは削除されていません。

2 DB2サーバーにあなたがDB2にデータをインポートする方法を共有する必要がありますファイルから scp out.sql [email protected]:out.sql

3ランクエリ db2 -tvsf /path/to/query/file/out.sql

関連する問題