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服務
MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡
本文永久更新連結地址: