[MySQL] 数据库改名

#!/bin/bash
mysqlconn=" mysql -uuser -ppass  -hhost -Pportold_db=”old_db ”
new_db=”new_db ”
$mysqlconn -e “CREATE DATABASE $new_dbparams=$($mysqlconn -N -e “SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=’$old_db’”)
for name in $params; do
$mysqlconn -e “RENAME TABLE $old_db.$name to $new_db.$name;
done;
$mysqlconn -e “DROP DATABASE $old_db

假设将sakila数据库名改为new_sakila

MyISAM直接更改数据库目录下的文件即可

#!/bin/bash
mysql -uroot -p123456 -e 'create database if not exists new_sakila'
list_table=$(mysql -uroot -p123456 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='sakila'")

for table in $list_table
do
    mysql -uroot -p123456 -e "rename table sakila.$table to new_sakila.$table"
done

你可能感兴趣的:(数据库,mysql)