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.

其他回答

这是我在Windows上写的重命名数据库的批处理脚本:

@echo off
set olddb=olddbname
set newdb=newdbname
SET count=1
SET act=mysql -uroot -e "select table_name from information_schema.tables where table_schema='%olddb%'"
mysql -uroot -e "create database %newdb%"
echo %act%
 FOR /f "tokens=*" %%G IN ('%act%') DO (
  REM echo %count%:%%G
  echo mysql -uroot -e "RENAME TABLE %olddb%.%%G to %newdb%.%%G"
  mysql -uroot -e "RENAME TABLE %olddb%.%%G to %newdb%.%%G"
  set /a count+=1
 )
mysql -uroot -e "drop database %olddb%"

为了方便起见,下面是一个小shell脚本,必须使用两个参数执行:db-name和new db-name。

如果您不使用主目录中的.my.cnf文件,则可能需要在mysql行中添加登录参数。请在执行此脚本之前进行备份。


#!/usr/bin/env bash

mysql -e "CREATE DATABASE $2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
for i in $(mysql -Ns $1 -e "show tables");do
    echo "$1.$i -> $2.$i"
    mysql -e "rename TABLE $1.$i to $2.$i"
done
mysql -e "DROP DATABASE $1"

最简单的方法是使用HeidiSQL软件。它是免费的,开源的。它可以在Windows和任何带有Wine的Linux上运行(在Linux、BSD、Solaris和Mac OS X上运行Windows应用程序)。

这是HeidiSQL的下载,转到 http://www.heidisql.com/download.php。

下载Wine,请登录http://www.winehq.org/。

要在HeidiSQL中重命名数据库,只需右键单击数据库名称并选择“Edit”。然后输入一个新名称并按“OK”。

就是这么简单。

这是我所使用的:

$ mysqldump -u root -p olddb >~/olddb.sql
$ mysql -u root -p
mysql> create database newdb;
mysql> use newdb
mysql> source ~/olddb.sql
mysql> drop database olddb;

在MySQL管理员中执行以下操作:

在Catalogs下,创建一个新的数据库模式。 转到备份并创建的备份 旧的模式。 执行备份。 转到恢复并打开文件 在步骤3中创建。 在目标下选择“另一个模式” 模式并选择新的数据库 模式。 开始恢复。 验证新模式,如果看起来 很好,删除旧的。