将数据从MySQL迁移到DB2的最简单方法是什么?

时间:2016-06-30 07:07:25

标签: db2 mysqldump db2-luw

我需要将数据从MySQL迁移到DB2。两个DB都已启动并运行。

我尝试使用mysqldump --no-create-info --extended-insert=FALSE --complete-insert并对输出进行一些更改(例如将`更改为"),我得到了满意的结果,但有时我有奇怪的例外情况,喜欢 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查询,并且运行良好。

如何更有效/一般地解决这个问题?还有其他建议吗?

1 个答案:

答案 0 :(得分: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服务器 scp out.sql user@myserver:out.sql

从文件中运行3个查询 db2 -tvsf /path/to/query/file/out.sql