首页 > python教程

python 实现mysql自动增删分区的方法

时间:2021-05-27 python教程 查看: 840

连接mysql

#!/usr/bin/python
#-*- coding:utf-8 -*-

import time
import pymysql

class connect_mysql(object):
  def __init__(self, host, dbname):
    self.mysql_config = {
      'host': host,
      'port': 33071,
      'user': 'sysbench',
      'passwd': '970125',
      'db': dbname,
      'charset': 'utf8mb4',
    }
    self.dbname = dbname


  def select_db(self, sql):
    mysql_conn = pymysql.connect(**self.mysql_config)
    try:
      query = "%s" %(sql)
      cur = mysql_conn.cursor()
      cur.execute(query)
      results = cur.fetchall()
      cur.close()
      mysql_conn.close()
      return results
    except Exception as err:
      print(err)

  def excute_db(self, sql):
    mysql_conn = pymysql.connect(**self.mysql_config)
    try:
      cur = mysql_conn.cursor()
      cur.execute(sql)
      mysql_conn.commit()
      cur.close()
      mysql_conn.close()
      return 0
    except Exception as err:
      mysql_conn.rollback()
      print(err)

增删分区

#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys
import pymysql
import importlib
import logging
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from connect_db_forbatch import connect_mysql

def incr_partition():
  print("新增分区...")
  max_partition_sql = "SELECT REPLACE(partition_name,'p','') FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='%s' and table_name='%s' ORDER BY partition_ordinal_position DESC LIMIT 1;" %(db_name,table_name)
#  print(max_partition_sql)
  max_partition = connect_mysql(host,db_name).select_db(max_partition_sql)
  max_date = str(max_partition[0][0])
  max_partition_name = (datetime.strptime(max_date, "%Y%m%d") + relativedelta(days=1)).strftime("%Y%m%d")
  max_partition_value = (datetime.strptime(max_date, "%Y%m%d") + relativedelta(days=2)).strftime("'%Y-%m-%d'")
  alter_max_partition_sql = "ALTER TABLE %s.%s ADD PARTITION (PARTITION p%s VALUES LESS THAN (to_days(%s)) ENGINE = InnoDB);" %(db_name,table_name,max_partition_name,max_partition_value)
  print(alter_max_partition_sql)
  connect_mysql(host,db_name).excute_db(alter_max_partition_sql)

def del_partition():
  print("删除分区...")
  min_partition_sql = "SELECT REPLACE(partition_name,'p','') FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='sbtest' and table_name='t1' ORDER BY partition_ordinal_position LIMIT 1;"
#  print(min_partition_sql)
  min_partition = connect_mysql(host,db_name).select_db(min_partition_sql)
  min_date = str(min_partition[0][0])
  min_partition_name = (datetime.strptime(min_date, "%Y%m%d") + relativedelta(days=0)).strftime("%Y%m%d")
  alter_min_partition_sql = "ALTER TABLE %s.%s DROP PARTITION p%s;" %(db_name,table_name,min_partition_name)
  print(alter_min_partition_sql)
  connect_mysql(host,db_name).excute_db(alter_min_partition_sql)

if __name__ == "__main__":
  host = sys.argv[1]
  db_name = sys.argv[2]
  table_name = sys.argv[3]
  incr_partition()
  del_partition()

到此这篇关于python 实现mysql自动增删分区的方法的文章就介绍到这了,更多相关python mysql自动增删分区内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

展开全文
上一篇:python3整数反转的实现方法
下一篇:详解如何利用tushare、pycharm和excel三者结合进行股票分析
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下