Migrate the collection data in MongoDB to the MySQL database

Source: Internet
Author: User
Tags install mongodb

Migrate the collection data in MongoDB to the MySQL database

1. Export data on mongodb and write expmongo. sh. The shell script is as follows:

#! /Bin/sh
Datestr = 'date' + % Y-% m-% d''
/Usr/local/mongodb/mongodb-linux-x86_64-2.4.4/bin/export-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_1_datestr.txt & sz/tmp/im_1_datestr.txt

The imported im data,/tmp/im_1_datestr.txt, contains more than 6000 records.

Similar data:
TxnType, issNo, sn, merchantorder, paymentType, paymentTime, authNo, expDate, cardNo, amount, batchNo, refNo, merchantName, voucher, acqNo
"0", "", "wp1452000000342", "", "bank card", "1440050366667", "", "," 6216615625 "," 10 "," 2 ", "891094135928", "thick flavor Hall", "000235 ",""
"1", "wp1452000000342", "10 ",,,,,
"0", "", "wp1452000000422", "," cash "," 1440050722137 "," 0.01 ",", "", "old tailor", "","

2. Create a table for storing mongodb data in the mysql database:
The table creation statement is as follows:
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. Import it to mysql.
Upload im_1_datestr.txt to the mysql server and change the exported file to the table name.
Mv/tmp/im_detail_2015-11-12.txt/tmp/sap_im.csv

Start import:
Mysql> load data infile "/tmp/sap_im.csv" replace into table sap_im fields terminated ', '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>
Strange: only the first line of record is imported here. It is estimated that it is a line break problem. Just remove \ r from the command line and replace '\ r \ N' with' \ n, as follows:

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>
More than 6000 records are imported normally.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.