MongoDB里的集合数据迁移到MySQL库

1,在mongodb上导出数据,写个脚步expmongo.sh,shell脚本如下

#!/bin/sh
datestr=`date '+%Y-%m-%d'`
/usr/local/mongodb/mongodb-linux-x86_64-2.4.4/bin/mongoexport -im_user -pxxxeedd -d im -c message -f txnType,issNo,sn,merchantorder,paymentType,paymentTime,authNo,expDate,cardNo,amount,batchNo,refNo,merchantName,voucher,acqNo --csv -o /tmp/im_$datestr.txt && sz /tmp/im_$datestr.txt

导出来的im数据,/tmp/im_$datestr.txt ,大概有6000多条记录。

类似这样的数据:
txnType,issNo,sn,merchantorder,paymentType,paymentTime,authNo,expDate,cardNo,amount,batchNo,refNo,merchantName,voucher,acqNo
“0”,”“,”WP14521000000342”,”“,”银行卡”,”1440050366667”,”“,”“,”6216615625”,”10”,”2”,”891094135928”,”厚味香味馆”,”000235”,””
“1”,,”WP14521000000342”,,,,,,,”10”,,,,,
“0”,”“,”WP14521000000422”,”“,”现金”,”1440050722137”,”“,”“,”“,”0.01”,”“,”“,”老裁缝家纺”,”“,”“

2,在MySQL库里面,建一张存储mongodb的数据的表:
建表语句如下:
CREATE TABLE sap_im (
txnType varchar(8) DEFAULT NULL,
issNo varchar(128) DEFAULT NULL,
sn varchar(128) DEFAULT NULL,
merchantorder varchar(128) DEFAULT NULL,
paymentType varchar(128) DEFAULT NULL,
paymentTime varchar(128) DEFAULT NULL,
authNo varchar(128) DEFAULT NULL,
expDate varchar(128) DEFAULT NULL,
cardNo varchar(128) DEFAULT NULL,
amount varchar(128) DEFAULT NULL,
batchNo varchar(128) DEFAULT NULL,
refNo varchar(128) DEFAULT NULL,
merchantName varchar(128) DEFAULT NULL,
voucher varchar(128) DEFAULT NULL,
acqNo varchar(128) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

3,导入到mysql里面
上传im_$datestr.txt到mysql服务器,先然后把导出文件改成表名字
mv /tmp/im_detail_2015-11-12.txt /tmp/sap_im.csv

开始导入:
mysql> LOAD DATA INFILE “/tmp/sap_im.csv” REPLACE INTO TABLE sap_im FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”’ ESCAPED BY ‘”’ LINES TERMINATED BY ‘\r\n’;
Query OK, 1 row affected (0.02 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0

mysql>
奇怪,这里只导入了第一行记录,估计是换行符的问题,去掉命令行的\r,将’\r\n’换成’\n’即可,如下所示:

mysql>
mysql>
mysql> LOAD DATA INFILE “/tmp/sap_im.csv” REPLACE INTO TABLE sap_im FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”’ ESCAPED BY ‘”’ LINES TERMINATED BY ‘\n’;
Query OK, 6555 rows affected (1.81 sec)
Records: 6555 Deleted: 0 Skipped: 0 Warnings: 0

mysql>
最终正常导入了6000多条记录出来。

更多MongoDB相关教程见以下内容

CentOS 编译安装 MongoDB与mongoDB的php扩展

CentOS 6 使用 yum 安装MongoDB及服务器端配置

Ubuntu 13.04下安装MongoDB2.4.3

MongoDB入门必读(概念与实战并重)

Ubunu 14.04下MongoDB的安装指南

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios监控MongoDB分片集群服务实战

基于CentOS 6.5操作系统搭建MongoDB服务 uxidc.com/Linux/2014-11/108900.htm

MongoDB 的详细介绍请点这里
MongoDB 的下载地址请点这里

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/dd251eaa8da63bc9643fae5f582c2146.html