MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
当前回答
I posted this How do I change the database name using MySQL? today after days of head scratching and hair pulling. The solution is quite simple export a schema to a .sql file and open the file and change the database/schema name in the sql CREAT TABLE section at the top. There are three instances or more and may not be at the top of the page if multible schemas are saved to the file. It is posible to edit the entire database this way but I expect that in large databases it could be quite a pain following all instances of a table property or index.
其他回答
我能给出的最快和最简单的解决办法是……在MySql Workbench中右键单击您的模式->单击创建模式->输入该模式的名称。
删除旧名称的旧模式。
你已经准备好摇滚....
注意:仅用于本地目的。不要尝试生产数据库表。创建了模式,但其中没有数据。所以要小心。
这是一个快速生成重命名sql脚本的方法,如果你有很多表要移动。
SELECT DISTINCT CONCAT('RENAME TABLE ', t.table_schema,'.', t.table_name, ' TO ',
t.table_schema, "_archive", '.', t.table_name, ';' ) as Rename_SQL
FROM information_schema.tables t
WHERE table_schema='your_db_name' ;
当您在PHPMyAdmin中重命名数据库时,它会创建一个转储,然后删除并使用新名称重新创建数据库。
你可以使用这个shell脚本:
参考:如何重命名MySQL数据库?
#!/bin/bash
set -e # terminate execution on command failure
mysqlconn="mysql -u root -proot"
olddb=$1
newdb=$2
$mysqlconn -e "CREATE DATABASE $newdb"
params=$($mysqlconn -N -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES \
WHERE table_schema='$olddb'")
for name in $params; do
$mysqlconn -e "RENAME TABLE $olddb.$name to $newdb.$name";
done;
$mysqlconn -e "DROP DATABASE $olddb"
工作:
$ sh rename_database.sh oldname newname
对于mac用户,你可以使用Sequel Pro(免费),它只提供重命名数据库的选项。虽然它不会删除旧的DB。
一旦打开相关的数据库,只需点击:数据库——>重命名数据库…