實時將MongoDB數據復制至MySQL中:Momy
Momy 是一個簡單的cli工具用于實時將MongoDB數據復制至MySQL中。
- 實現在NoSQL數據庫進行SQL查詢
- 實現通過Excel / Access訪問
安裝
$ 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
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!