Python中MySQL數(shù)據(jù)遷移到MongoDB腳本的方法
MongoDB簡介
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫。由 C++ 語言編寫。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。
MongoDB是一個(gè)文檔數(shù)據(jù)庫,在存儲(chǔ)小文件方面存在天然優(yōu)勢(shì)。隨著業(yè)務(wù)求的變化,需要將線上MySQL數(shù)據(jù)庫中的行記錄,導(dǎo)入到MongoDB中文檔記錄。
一、場(chǎng)景:線上MySQL數(shù)據(jù)庫某表遷移到MongoDB,字段無變化。
二、Python模塊:
使用Python的torndb,pymongo和time模塊。
*注釋:首先安裝setup.py,pip,MySQLdb
執(zhí)行如下命令即可:
pip install torndb
pip install pymongo
三、腳本內(nèi)容如下:
[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)
*注釋:按照自己的需求更改上述代碼中的數(shù)據(jù)庫地址,用戶,密碼,庫名,表名以及字段名等。
四、執(zhí)行遷移腳本:
[root ~]#python nmytomongo.py &> /tmp/migratelog.txt &
腳本執(zhí)行完成后查看/tmp/migratelog.txt數(shù)據(jù)遷移消耗的時(shí)間。
- 關(guān)于MySQL數(shù)據(jù)遷移--data目錄直接替換注意事項(xiàng)的詳解
- Oracle數(shù)據(jù)遷移MySQL的三種簡單方法
- 如何把sqlserver數(shù)據(jù)遷移到mysql數(shù)據(jù)庫及需要注意事項(xiàng)
- MySQL數(shù)據(jù)遷移使用MySQLdump命令
- mysql中用于數(shù)據(jù)遷移存儲(chǔ)過程分享
- MySQL 億級(jí)數(shù)據(jù)導(dǎo)入導(dǎo)出及遷移筆記
- mysql數(shù)據(jù)庫如何實(shí)現(xiàn)億級(jí)數(shù)據(jù)快速清理
- MySQL億級(jí)數(shù)據(jù)平滑遷移雙寫方案實(shí)戰(zhàn)
相關(guān)文章
python pip如何手動(dòng)安裝二進(jìn)制包
這篇文章主要介紹了python pip如何手動(dòng)安裝二進(jìn)制包,幫助大家更好的進(jìn)行python開發(fā),感興趣的朋友可以了解下2020-09-09
Django admin model 漢化顯示文字的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Django admin model 漢化顯示文字的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python實(shí)現(xiàn) 多進(jìn)程導(dǎo)入CSV數(shù)據(jù)到 MySQL
本文給大家分享的是使用python實(shí)現(xiàn)多進(jìn)程導(dǎo)入CSV文件數(shù)據(jù)到MySQL的思路方法以及具體的代碼分享,有相同需求的小伙伴可以參考下2017-02-02
Python中實(shí)現(xiàn)遠(yuǎn)程調(diào)用(RPC、RMI)簡單例子
說白了,遠(yuǎn)程調(diào)用就是將對(duì)象名、函數(shù)名、參數(shù)等傳遞給遠(yuǎn)程服務(wù)器,服務(wù)器將處理結(jié)果返回給客戶端2014-04-04
使用Python將Exception異常錯(cuò)誤堆棧信息寫入日志文件
這篇文章主要介紹了使用Python將Exception異常錯(cuò)誤堆棧信息寫入日志文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放案例詳解
pygame是Python的第三方庫,里面提供了使用Python開發(fā)游戲的基礎(chǔ)包。本文將介紹如何通過Pygame實(shí)現(xiàn)鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放,感興趣的可以關(guān)注一下2021-12-12

