Python中MySQL資料移轉到MongoDB指令碼的方法

來源:互聯網
上載者:User
MongoDB簡介

MongoDB 是一個基於分布式檔案儲存體的資料庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴充的高效能資料儲存解決方案。

MongoDB 是一個介於關聯式資料庫和非關聯式資料庫之間的產品,是非關聯式資料庫當中功能最豐富,最像關聯式資料庫的。

MongoDB是一個文檔資料庫,在儲存小檔案方面存在天然優勢。隨著業務求的變化,需要將線上MySQL資料庫中的行記錄,匯入到MongoDB中文檔記錄。

一、情境:線上MySQL資料庫某表遷移到MongoDB,欄位無變化。

二、Python模組:

使用Python的torndb,pymongo和time模組。

*注釋:首先安裝setup.py,pip,MySQLdb

執行如下命令即可:

pip install torndb
pip install pymongo

三、指令碼內容如下:

[root ~]#cat nmytomongo.py

#!/usr/bin/env python#fielName: mytomongo.py#Author:xkops#coding: utf-8import torndb,pymongo,time# connect to mysql databasemysql = torndb.Connection(host='127.0.0.1', database='database', user='username', password='password')#connect to mongodb and obtain total lines in mysqlmongo = pymongo.MongoClient('mongodb://ip').databasemongo.authenticate('username',password='password')countlines = mysql.query('SELECT max(table_field) FROM table_name')count = countlines[0]['max(table_field)']#count = 300print counti = 0 j = 100start_time = time.time()#select from mysql to insert mongodb by 100 lines.for i in range(0,count,100): #print a,b #print i #print 'SELECT * FROM quiz_submission where quiz_submission_id > %d and quiz_submission_id <= %d' %(i,j) submission = mysql.query('SELECT * FROM table_name where table_field > %d and table_field <= %d' %(i,j)) #print submission if submission: #collection_name like mysql table_name mongo.collection_name.insert_many(submission) else: i +=100 j +=100 continue i +=100 j +=100end_time = time.time()deltatime = end_time - start_timetotalhour = int(deltatime / 3600)totalminute = int((deltatime - totalhour * 3600) / 60)totalsecond = int(deltatime - totalhour * 3600 - totalminute * 60)#print migrate data total time consuming.print "Data Migrate Finished,Total Time Consuming: %d Hour %d Minute %d Seconds" %(totalhour,totalminute,totalsecond)

*注釋:按照自己的需求更改上述代碼中的資料庫地址,使用者,密碼,庫名,表名以及欄位名等。

四、執行遷移指令碼:

[root ~]#python nmytomongo.py &> /tmp/migratelog.txt &

指令碼執行完成後查看/tmp/migratelog.txt資料移轉消耗的時間。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.