MySQL、Redis通过Python脚本进行资源迁移
MySQL迁移,需要注意从低版本mysql迁移到高版本mysql,collation和charset的变化。
def databaseMove(srcDatabase,targetDatabase):
try:
src_conn=pymysql.connect(**srcDatabase)
target_conn=pymysql.connect(**targetDatabase)
with src_conn.cursor() as src_cur,target_conn.cursor() as target_cur:
src_cur.execute("show tables")
tables=src_cur.fetchall()
for (tableName,) in tables:
#复制表结构
src_cur.execute(f"show create table `{tableName}`")
create_table_sql=src_cur.fetchone()[1]
target_cur.execute(f"drop table if exists `{tableName}`")
target_cur.execute(create_table_sql)
#复制表数据
src_cur.execute(f"select * from `{tableName}`")
rows=src_cur.fetchall()
columns = ', '.join([f"`{desc[0]}`" for desc in src_cur.description])
placeholders=', '.join(['%s']*len(src_cur.description))
insert_sql=f"insert into `{tableName}` ({columns}) values ({placeholders})"
for row in rows:
target_cur.execute(insert_sql,row)
print(f"{tableName} migrated successfully.")
target_conn.commit()
except Exception as e:
print(f"Error:{e}")
finally:
src_conn.close()
target_conn.close()
Redis,Redis的迁移只要使用migrate函数就行了。
def databasemove():
for src_db in range(1, max_db_index):
#获取原始数据库的所有键值对
target_db=src_db
source_redis = redis.StrictRedis(
host=src_host,
port=src_port,
password=src_password,
db=src_db,
decode_responses=True
)
keys = []
cursor = 0
while True:
cursor, keys = source_redis.scan(cursor=cursor, match='*',count=1000)
for key in keys:
try:
source_redis.migrate(
host=target_host,
port=target_port,
keys=key,
destination_db=target_db,
timeout=timeout,
copy=True,
replace=True,
auth=target_password
)
print(f"Successfully migrated key: {key}")
except Exception as e:
print(f"Error migrating key {key}: {e}")
if cursor == 0:
break
print("Data migration completed.")