1.方法一
mysql5.1有改名数据库的命令,但是没过多久就被取消了因为这个命令不稳定,可能导致数据丢失。这个命令就是:
rename database db_old_name to db_new_name
2.方法二
用脚本实现,包括获取表名,将旧库的表名rename到新库中(rename时自动移动到新库)。
#!/bin/bash
mysql -uroot -p'2ZXCVBNM<>?@' --host=192.168.30.42 -e 'create database if not exists statanalyse_bak;'
list_table=$(mysql -uroot -p'2ZXCVBNM<>?@' --host=192.168.30.42 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='statanalyse'")
for table in $list_table ; do
echo $table
mysql -uroot -p'2ZXCVBNM<>?@' --host=192.168.30.42 -e "rename table statanalyse.$table to statanalyse_bak.$table"
done