问题描述
不想本地继续Mysql占用大量内存。所以准备放到服务器提供服务,所以就有了迁移需求。但是mysqldump导出后在ubuntu上总是没法运行,source和mysqldump都行不通。所以自己写了个代码,直接改sql文件,做成命令行再来source
正文
def write():
with open('/home/zzb/Data/back.sql' ,'r', encoding='utf8') as f:
data= []
line_in = False
for line in f.readlines():
if line.find('CREATE TABLE `')!=-1 :
line_in = True
data.append(line)
continue
if line.find(') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;')!=-1:
line_in = False
data.append(line.replace('ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',''))
if line_in:
data.append(line)
if line.find("INSERT INTO")!=-1:
data.append(line)
with open('new.sql','w',encoding='utf8') as wr:
for line in data:
wr.write(line)