MySQL如何修改数据库名称
在Innodb数据库引擎下修改数据库名的方式与MyISAM引擎下修改数据库的方式完全不一样,如果是MyISAM可以直接去数据库目录中mv就可以,Innodb如果用同样的方法修改会提示相关表不存在
第一种方法:
1、创建要改名的新数据库
2、mysqldump导出要改名的数据库
3、删除原来的数据库
4、导入数据到新的数据库
第二种方法:
利用脚本
#!/bin/bash
#假设将test数据库改名为txt
mysql -uroot -p123456 -e ‘create database if not exists txt’
list_table=$(mysql -uroot -p123456 -Nse “select table_name from information_schema.TABLES where TABLE_SCHEMA=‘test’”)
for table in $list_table
do
mysql -uroot -p123456 -e "rename table test.$table to txt.$table"
done