脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python实现数据库跨服务器迁移

python实现数据库跨服务器迁移

2021-01-31 00:13Stupid_Sparrow Python

这篇文章主要为大家详细介绍了Python实现数据库之间的数据迁移,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

 基于Python2.7的版本环境,Python实现的数据库跨服务器(跨库)迁移, 每以5000条一查询一提交,代码中可以自行更改每次查询提交数目.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
 
import MySQLdb
import time
import warnings
 
warnings.filterwarnings("ignore")
 
 
class ConnectMysql(object):
  def __init__(self):
#     这里设置分页查询, 每页查询多少数据
    self.page_size = 5000
 
  def getTable(self):
    conn = MySQLdb.connect(
      host="***.***.**.**",
      user="****",
      passwd="*************",
      db='****',
      charset='utf8'
    )
    conn_local = MySQLdb.connect(
      host="********************************",
      user="**********",
      passwd="********",
      db='*******',
      charset='utf8'
    )
    cur = conn.cursor()
    cur_local = conn_local.cursor()
    cur.execute('show tables')
    tables = cur.fetchall()
    for table in tables:
      print str(table[0]).lower()
      # 需要迁移的数据库查询表的列数
      cur.execute("SELECT COUNT(*) FROM information_schema.COLUMNS WHERE table_schema='china' AND table_name='" + table[0] + "'")
      table_col_count = cur.fetchone()
      # print table_col_count[0]
      # 需要迁移的数据库查询表的结构
      cur.execute('show create table ' + table[0])
      result = cur.fetchall()
      create_sql = result[0][1]
      # 查询需要迁移的数据库表的数据条数
      cur.execute('select count(*) from ' + table[0])
      total = cur.fetchone()
      page = total[0] / self.page_size
      page1 = total[0] % self.page_size
      if page1 != 0:
        page = page + 1
 
      # 阿里云数据库创建表
      cur_local.execute("SELECT table_name FROM information_schema.`TABLES` WHERE table_schema='user' AND table_name='" + str(table[0]).lower() + "'")
      table_name = cur_local.fetchone()
      if table_name is None:
        cur_local.execute(create_sql)
      for p in range(0, page):
        while True:
          try:
            print '开始', table[0], '的第', p + 1, '页查询'
            if p == 0:
              limit_param = ' limit ' + str(p * self.page_size) + ',' + str(self.page_size)
            else:
              limit_param = ' limit ' + str(p * self.page_size + 1) + ',' + str(self.page_size)
            cur.execute('select * from ' + table[0] + limit_param)
            inserts = cur.fetchall()
            print '查询成功'
            param = ''
            for i in range(0, table_col_count[0]):
              param = param + '%s,'
            print '开始插入'
            cur_local.executemany('replace into ' + table[0] + ' values (' + param[0:-1] + ')', inserts)
            print table[0], '的第', p + 1, '页, 插入完成, 还有', page - p - 1, '页, 任重而道远'
            conn_local.commit()
            break
          except Exception as e:
            print e
            time.sleep(60)
            cur = conn.cursor()
            cur_local = conn_local.cursor()
        print table[0], ' 插入完成'
        print '\n \n ======================================================================== \n\n'
    cur_local.close()
    conn_local.close()
    cur.close()
    conn.close()
 
 
if __name__ == '__main__':
  conn_mysql = ConnectMysql()
  conn_mysql.getTable()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_27631797/article/details/79410277

延伸 · 阅读

精彩推荐