MySQL手册中有介绍。

通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。

这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。


当前回答

TodoInTx的解决方案和user757945的改编解决方案都不适合我的MySQL 5.5.16,所以这里是我的改编版本:

DELIMITER //
DROP PROCEDURE IF EXISTS `rename_database`;
CREATE PROCEDURE `rename_database` (IN `old_name` VARCHAR(20), IN `new_name` VARCHAR(20))
BEGIN
  DECLARE `current_table_name` VARCHAR(20);
  DECLARE `done` INT DEFAULT 0;
  DECLARE `table_name_cursor` CURSOR FOR SELECT `table_name` FROM `information_schema`.`tables` WHERE (`table_schema` = `old_name`);
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done` = 1;

  SET @sql_string = CONCAT('CREATE DATABASE IF NOT EXISTS `', `new_name` , '`;');
  PREPARE `statement` FROM @sql_string;
  EXECUTE `statement`;
  DEALLOCATE PREPARE `statement`;

  OPEN `table_name_cursor`;
  REPEAT
    FETCH `table_name_cursor` INTO `current_table_name`;
    IF NOT `done` THEN

      SET @sql_string = CONCAT('RENAME TABLE `', `old_name`, '`.`', `current_table_name`, '` TO `', `new_name`, '`.`', `current_table_name`, '`;');
      PREPARE `statement` FROM @sql_string;
      EXECUTE `statement`;
      DEALLOCATE PREPARE `statement`;

    END IF;
  UNTIL `done` END REPEAT;
  CLOSE `table_name_cursor`;

  SET @sql_string =  CONCAT('DROP DATABASE `', `old_name`, '`;');
  PREPARE `statement` FROM @sql_string;
  EXECUTE `statement`;
  DEALLOCATE PREPARE `statement`;
END//
DELIMITER ;

希望它能帮助到像我这样处境的人!注意:@sql_string之后将在会话中逗留。不使用它,我就无法写出这个函数。

其他回答

这适用于所有数据库,通过使用maatkit mysql工具包重命名每个表

使用mk-find打印并重命名每个表。手册页有更多选项和示例

mk-find --dblike OLD_DATABASE --print --exec "RENAME TABLE %D.%N TO NEW_DATABASE.%N"

如果您安装了maatkit(这非常简单),那么这是最简单的方法。

MySQL目前不支持通过其命令界面重命名数据库,但如果您可以访问MySQL存储数据库的目录,则可以重命名数据库。对于默认的MySQL安装,它通常在MySQL安装目录下的Data目录中。在Data目录下找到要重命名的数据库的名称并重命名它。不过,重命名目录可能会导致一些权限问题。请注意。

注意:在重命名数据库之前,必须停止MySQL

我建议创建一个新数据库(使用您想要的名称),并将所需的数据从旧数据库导出/导入到新数据库。很简单。

实际上,最简单的答案是导出旧数据库,然后将其导入到您创建的新数据库中以替换旧数据库。当然,您应该使用phpMyAdmin或命令行来做到这一点。

重命名和操纵数据库是一个坏主意!不要这样做。(除非你是那种“黑客类型”的人,在黑暗中坐在妈妈的地下室里,吃着披萨,白天睡觉。)

你最终会遇到比你想要的更多的问题和工作。

So,

Create a new_database and name it the correct way. Go to your phpMyAdmin and open the database you want to export. Export it (check the options, but you should be OK with the defaults. You will get a file like or similar to this. The extension on this file is .sql -- phpMyAdmin SQL Dump -- version 3.2.4 -- http://www.phpmyadmin.net -- Host: localhost -- Generation Time: Jun 30, 2010 at 12:17 PM -- Server version: 5.0.90 -- PHP Version: 5.2.6 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /; /!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /; /!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /; /!40101 SET NAMES utf8 */; -- -- Database: mydatab_online -- -- Table structure for table user CREATE TABLE IF NOT EXISTS user ( timestamp int(15) NOT NULL default '0', ip varchar(40) NOT NULL default '', file varchar(100) NOT NULL default '', PRIMARY KEY (timestamp), KEY ip (ip), KEY file (file) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table user INSERT INTO user (timestamp, ip, file) VALUES (1277911052, '999.236.177.116', ''), (1277911194, '999.236.177.116', '');

这将是您的.sql文件。你刚刚导出的那个。

在你的硬盘上找到它;通常在/temp中。选择具有正确名称(阅读本文的原因)的空数据库。 说:导入-开始

通过将程序输入到通常是configuration.php文件中,将程序连接到正确的数据库。刷新服务器(两个。为什么?因为我是UNIX老手,我说过。 现在,你的状态应该很好。如果你还有任何问题,请到网上来找我。

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.

可以使用SQL生成SQL脚本,将源数据库中的每个表传输到目标数据库。

在运行该命令生成的脚本之前,必须先创建目标数据库。

您可以使用这两个脚本中的任何一个(我最初建议使用前者,有人“改进”了我的回答,使用GROUP_CONCAT。随你挑,但我更喜欢原版):

SELECT CONCAT('RENAME TABLE $1.', table_name, ' TO $2.', table_name, '; ')
FROM information_schema.TABLES 
WHERE table_schema='$1';

or

SELECT GROUP_CONCAT('RENAME TABLE $1.', table_name, ' TO $2.', table_name SEPARATOR '; ')
FROM information_schema.TABLES 
WHERE table_schema='$1';

($1和$2分别是源和目标)

这将生成一个SQL命令,然后必须运行该命令。

注意,GROUP_CONCAT有一个默认的长度限制,对于包含大量表的数据库可能会超过这个长度限制。您可以通过执行SET SESSION group_concat_max_len = 100000000来更改该限制;(或其他较大的数字)。