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

What is the simplest way to migrate data from MySQL to DB2

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

我尝试 mysqldump--no-create-info --extended-insert=FALSE --complete-insert 并对输出进行一些更改(例如将 ` 更改为 "),我得到了令人满意的结果,但有时我有奇怪的例外情况,例如 does not have an ending string delimiter. SQLSTATE=42603

理想情况下,我希望有一个尽可能通用的例程,但作为这里的示例,假设我有一个 DB2 table,它看起来像:

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  and  

,我得到:

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 查询,并且运行良好。

关于如何解决这个问题的更多想法efficiently/generally?还有其他建议吗?

经过一番尝试后,我得出了以下例程,我认为它相当通用、健壮且可扩展。

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  and  

注意:这里不像问题;没有被删除。

2上传文件到DB2服务器 scp out.sql user@myserver:out.sql

3 运行 个来自文件的查询 db2 -tvsf /path/to/query/file/out.sql