Momy is a simple cli tool for replicating MongoDB to MySQL in realtime.
- Enable SQL query on data in NoSQL database
- Enable to be accessed by Excel / Access
$ npm install -g momy
or install it within the project locally:
$ npm install --save momy
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.
Launch MySQL instance, and create the new database to use. The tables will be created or updated when syncing. You'll see mongo_to_mysql
, too. This is needed to store the information for syncing. (don't remove it)
Create a new momyfile.json
file like this:
{
"src": "mongodb://localhost:27017/dbname",
"dist": "mysql://root@localhost:3306/dbname",
"prefix": "t_",
"case": "camel",
"collections": {
"collection1": {
"_id": "number",
"createdAt": "DATETIME",
"field1": "number",
"field2": "string",
"field3": "boolean",
"field4.subfield": "string"
},
"collection2": {
"_id": "string",
"createdAt": "DATETIME",
"field1": "number",
"field2": "string",
"field3": "boolean"
}
}
}
src
: the URL of the MongoDB serverdist
: the URL of the MySQL serverprefix
: optional prefix for table name. The name of the table would bet_collection1
in the example above.case
: optional.snake
orcamel
collections
: set the collections and fields to sync
_id
field is required for each collection and should be string
or number
.
"<field_name>": "<field_tipe>"
or, field_name could be dot-concatenated:
"<field_name>.<sub_name>": "<field_tipe>"
For example, if you have { a: { b: { c: 'hey!' } } }
then "a.b.c": "string"
Currently these native types are supported:
BIGINT
TINYINT
VARCHAR
DATE
DATETIME
TIME
There're also some aliases:
number
=>BIGINT
boolean
=>TINYINT
string
=>VARCHAR
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
MIT
This library was originally made by @doubaokun as MongoDB-to-MySQL and rewritten by @cognitom.