mysql4.0是老版本了,但是有些早期使用的企业依然在用,在创建主从时特别是线上服务器创建主从时,保证数据的一致性是个大问题:比如创建完从库同步时出现重复数据重复执行(虽然数据条数一致,但数据有可能会不一致)等。在mysql5.0以上版本中,此时备份主库只用在mysqldump时加上-F、master-data=2,single-transaction参数,从库同步时导入备份,在取备份文件开头的bin-log和pos位置进行同步即可,不会出现数据重复执行等问题,他能确保同步时的一致性。比较悲剧的是,本人所用的数据库还没有升级,是4.0的版本,经过测试,写了一个专一用于4.0主从同步时主库备份的脚本,原理就是模拟5.0以上的备份过程来做的。也可以用于5.0以上的版本,但是5.0以上的版本没有必要这么做。大家可以参考。

 

  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3. #last update:2012-10-25  
  4.  
  5. import os,sys,time,MySQLdb  
  6. import subprocess,threading  
  7.  
  8. class mysql_dump():  
  9.  
  10.     def __init__(self):  
  11.         self.STAT_IP = "192.168.1.110" 
  12.         self.logfile = "/data/script/db_back/mysql_dump.log" 
  13.         self.user = "dump" 
  14.         self.passwd = "123456" 
  15.         self.distid = "2" 
  16.  
  17.     def log_w(self,text):  
  18.         now = time.strftime("%Y-%m-%d %H:%M:%S")  
  19.         tt = str(now) + "\t" + str(text) + "\n" 
  20.         f = open(self.logfile,'a+')  
  21.         f.write(tt)  
  22.         f.close()  
  23.  
  24.     def dump(self,dumpname,now):  
  25.         cmd = "/usr/local/mysql/bin/mysqldump -A -Q -e --add-drop-table --add-locks --extended-insert --quick --no-autocommit --single-transaction -u%s -p%s | bzip2 -2 > %s" % (self.user,self.passwd,dumpname)  
  26.         print time.strftime("%Y-%m-%d %H:%M:%S")  
  27.         text = "Start mysqldump,Please wait ..." 
  28.         print text  
  29.         self.log_w(text)  
  30.         a = subprocess.Popen(cmd,shell=True)  
  31.         while 1:  
  32.             b = subprocess.Popen.poll(a)  
  33.             if b == 0:  
  34.                 text = "Mysqldump complete" 
  35.                 print text  
  36.                 self.log_w(text)  
  37.                 break 
  38.             elif b is None:  
  39.                 print  'Mysqldump running' 
  40.                 time.sleep(30)  
  41.             else:  
  42.                 print a.pid,'term' 
  43.                 break 
  44.         self.rsync(dumpname)  
  45.  
  46.     def rsync(self,dumpname):  
  47.         cmd = "rsync -az %s %s::asktao_db/db_back/" % (dumpname,self.STAT_IP)  
  48.         text = "Start rsync to server(%s) ,Please wait ..." % self.STAT_IP  
  49.         print text  
  50.         self.log_w(text)  
  51.         a = subprocess.Popen(cmd,shell=True)  
  52.         while 1:  
  53.             b = subprocess.Popen.poll(a)  
  54.             if b == 0:  
  55.                 text = "Rsync complete" 
  56.                 print text  
  57.                 self.log_w(text)  
  58.                 break 
  59.             elif b is None:  
  60.                 print  'Rsync running' 
  61.                 time.sleep(30)  
  62.             else:  
  63.                 print a.pid,'term' 
  64.                 break 
  65.  
  66.     def bin_log(self):  
  67.         try:  
  68.             conn = MySQLdb.connect(host = '127.0.0.1',user = 'repl_monitor',passwd = '123456',connect_timeout=5)  
  69.             cursor = conn.cursor()  
  70.             cursor.execute("show master status")  
  71.             alldata = cursor.fetchall()  
  72.             cursor.close()  
  73.             conn.close()  
  74.             file = alldata[0][0]  
  75.             Position = alldata[0][1]  
  76.             text = "show master status: %s  %s" % (file,Position)  
  77.             print text  
  78.             self.log_w(text)  
  79.             return file,Position  
  80.         except MySQLdb.Error,e:  
  81.             text = e.args  
  82.             print text  
  83.             self.log_w(text)  
  84.             sys.exit()  
  85.  
  86.     def lock(self):  
  87.         try:  
  88.             conn = MySQLdb.connect(host = 'localhost',user = self.user,passwd = self.passwd,connect_timeout=5)  
  89.             cursor = conn.cursor()  
  90.             text = "flush tables with read lock" 
  91.             print text  
  92.             self.log_w(text)  
  93.             cursor.execute("flush tables with read lock")  
  94.             text = "flush logs" 
  95.             print text  
  96.             self.log_w(text)  
  97.             cursor.execute("flush logs")  
  98.             file,Position = self.bin_log()  
  99.             now = time.strftime("%Y%m%d%H%M")  
  100.             dumpname = "/data/script/db_back/wd_%s_%s_%s_%s.bz2" % (self.distid,now,file,Position)  
  101.             d = threading.Thread(target=self.dump, args=(dumpname,now))  
  102.             d.start()  
  103.             while 1:  
  104.                 if os.path.isfile(dumpname) and os.path.getsize(dumpname) > 0:  
  105.                     text = "UNLOCK TABLES" 
  106.                     print text  
  107.                     self.log_w(text)  
  108.                     cursor.execute("UNLOCK TABLES")  
  109.                     cursor.close()  
  110.                     conn.close()  
  111.                     break 
  112.         except MySQLdb.Error,e:  
  113.             text = e.args  
  114.             print text  
  115.             self.log_w(text)  
  116.             sys.exit()  
  117.  
  118.     def work(self):  
  119.         t = threading.Thread(target=self.lock, args=())  
  120.         t.start()  
  121.  
  122.  
  123. if __name__ == "__main__":  
  124.     boss = mysql_dump()   
  125.     boss.work()