实时将MongoDB数据复制至MySQL中:Momy

jopen 8年前

Momy 是一个简单的cli工具用于实时将MongoDB数据复制至MySQL中。

  • 实现在NoSQL数据库进行SQL查询
  • 实现通过Excel / Access访问

Momy

安装

$ npm install -g momy

or install it within the project locally:

$ npm install --save momy

准备

MongoDB

Momy uses Replica Set feature in MongoDB. But you don't have to replicate between MongoDB actually. Just follow the steps below.

Start a new mongo instance with no data:

$ mongod --replSet "rs0" --oplogSize 100

Open another terminal, and go to MongoDB Shell:

$ mongo  .... > rs.initiate()

rs.initiate()command prepare the collections that is needed for replication.

MySQL

Launch MySQL instance, and create the new database to use. The tables will be created or updated when syncing. You'll seemongo_to_mysql, too. This is needed to store the information for syncing. (don't remove it)

配置

Create a newmomyfile.jsonfile like this:

{    "src": "mongodb://localhost:27017/dbname",    "dist": "mysql://root@localhost:3306/dbname",    "prefix": "t_",    "collections": {      "collection1": {        "_id": "number",        "field1": "number",        "field2": "string",        "field3": "boolean"      },      "collection2": {        "_id": "number",        "field1": "number",        "field2": "string",        "field3": "boolean"      }    }  }

  • src: the URL of the MongoDB server
  • dist: the URL of the MySQL server
  • prefix: optional prefix for table name. The name of the table would bet_collection1in the example above.
  • collections: set the collections and fields to sync

用法

At the first run, we need to import all the data from MongoDB:

$ momy --config momyfile.json --import

Then start the daemon to streaming data:

$ momy --config momyfile.json

or

$ forever momy --config momyfile.json

项目主页:http://www.open-open.com/lib/view/home/1445519642085