python 實(shí)現(xiàn)mysql自動增刪分區(qū)的方法
更新時(shí)間:2021年04月01日 10:59:47 作者:_雪輝_
這篇文章主要介紹了python 實(shí)現(xiàn)mysql自動增刪分區(qū)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
連接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)
增刪分區(qū)
#!/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("新增分區(qū)...")
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("刪除分區(qū)...")
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()
到此這篇關(guān)于python 實(shí)現(xiàn)mysql自動增刪分區(qū)的方法的文章就介紹到這了,更多相關(guān)python mysql自動增刪分區(qū)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- MySQL分庫分表與分區(qū)的入門指南
- MySql分表、分庫、分片和分區(qū)知識深入詳解
- MySQL學(xué)習(xí)記錄之KEY分區(qū)引發(fā)的血案
- Mysql臨時(shí)表及分區(qū)表區(qū)別詳解
- 詳解MySQL分區(qū)表
- MySQL最佳實(shí)踐之分區(qū)表基本類型
- MySQL分區(qū)表的最佳實(shí)踐指南
- MySql分表、分庫、分片和分區(qū)知識點(diǎn)介紹
- MySQL分表和分區(qū)的具體實(shí)現(xiàn)方法
- MySQL分區(qū)表的正確使用方法
- MySQL高級特性——數(shù)據(jù)表分區(qū)的概念及機(jī)制詳解
相關(guān)文章
Python備份目錄及目錄下的全部內(nèi)容的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狿ython備份目錄及目錄下的全部內(nèi)容的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
Python中uuid模塊生成唯一標(biāo)識符的方法詳解
這篇文章主要給大家介紹了關(guān)于Python中uuid模塊生成唯一標(biāo)識符的相關(guān)資料,uuid庫是Python標(biāo)準(zhǔn)庫中的一個(gè)功能強(qiáng)大的庫,可以用于生成全局唯一標(biāo)識符(UUID),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Python利用fastapi實(shí)現(xiàn)上傳文件
FastAPI是一個(gè)現(xiàn)代的,快速(高性能)python?web框架。本文將利用fastapi實(shí)現(xiàn)上傳文件功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06

