概述
介绍:略
思路:借助rename这个命令
基本操作:rename olddb.tables to newdb.tables
直接脚本执行:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
import time
class m_s:
def __init__(self,host,user,password,port):
self.host=host
self.user=user
self.passowrd=password
self.port=port
print self.host,self.user,self.passowrd,self.port
def getConn(self,db="mysql"):
try:
conn=MySQLdb.connect(host=self.host, user=self.user, passwd=self.passowrd, db=db, port=self.port, charset="utf8")
cur = conn.cursor()
d_b=["db_cloud_s_006_33","db_cloud_s_007_33","db_cloud_s_008_33","db_cloud_s_009_33"] #定义要修改的数据库
for i in range(len(d_b)):sql='''SELECT TABLE_SCHEMA,table_name FROM information_schema.tables WHERE TABLE_SCHEMA="'''+str(d_b[i])+'''" GROUP BY TABLE_SCHEMA,table_name''' ##获取要修改库下对应的表信息
print sql
cur.execute(sql)
db_list=cur.fetchall()
# print db_list
sql2 = '''create database ''' + str(d_b[i]) + "_bak" ##创建新的数据库
print sql2
cur.execute(sql2)
conn.commit()
new_db = str(d_b[i]) + "_bak" ##新数据库名字
for j in range(len(db_list)):
sql3="rename table "+str(db_list[j][0])+"."+str(db_list[j][1])+" to " +new_db+"."+str(db_list[j][1])
print sql3
cur.execute(sql3)
conn.commit()
print sql3
time.sleep(5)
return "OK"
except Exception as e:
return e
if __name__ =="__main__":
M=m_s("192.168.21.31","username","passoword",6330)
print M.getConn()
最后
以上就是可耐芒果为你收集整理的MySQL在线修改数据库名称的全部内容,希望文章能够帮你解决MySQL在线修改数据库名称所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复