阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Python迁移MySQL数据到MongoDB脚本

99次阅读
没有评论

共计 2390 个字符,预计需要花费 6 分钟才能阅读完成。

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-8
import torndb,pymongo,time
# connect to mysql database
mysql = torndb.Connection(host=’127.0.0.1′, database=’database’, user=’username’, password=’password’)
#connect to mongodb and obtain total lines in mysql
mongo = pymongo.MongoClient(‘mongodb://ip’).database
mongo.authenticate(‘username’,password=’password’)
countlines = mysql.query(‘SELECT max(table_field) FROM table_name’)
count = countlines[0][‘max(table_field)’]

#count = 300
print count

i = 0
j = 100
start_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 +=100
end_time = time.time()
deltatime = end_time – start_time
totalhour = 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 数据迁移消耗的时间。

更多 MongoDB 相关教程见以下内容

CentOS 编译安装 MongoDB 与 mongoDB 的 php 扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm

CentOS 6 使用 yum 安装 MongoDB 及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm

Ubuntu 13.04 下安装 MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm

MongoDB 入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm

Ubunu 14.04 下 MongoDB 的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm

Nagios 监控 MongoDB 分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm

基于 CentOS 6.5 操作系统搭建 MongoDB 服务 http://www.linuxidc.com/Linux/2014-11/108900.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130787.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2390字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中