MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
当前回答
我认为这个解决方案更简单,是一些开发人员提出的。phpMyAdmin对此有一个操作。
从phpMyAdmin中选择要选择的数据库。在选项卡中有一个叫做Operations,转到rename部分。这是所有。
正如许多人建议的那样,它使用新名称创建一个新数据库,将旧数据库的所有表转储到新数据库中,并删除旧数据库。
更新2022-09-22:MySQL 8.0+增加了一个更简单的解决方案:
不知道从什么时候开始添加RENAME TO关键字,但肯定更简单。不过在尝试之前,我会先把桌子备份一下,尤其是如果你的桌子很大的话。无论如何,它可以实现如下:
ALTER TABLE `schema_name`.`table_name`
RENAME TO `schema_name`.`new_table_name` ;
为了便于阅读,我把它分成了两行,但也可以写成单行,如下:
ALTER TABLE `schema_name`.`table_name` RENAME TO `schema_name`.`new_table_name` ;
其他回答
可以使用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来更改该限制;(或其他较大的数字)。
实际上,最简单的答案是导出旧数据库,然后将其导入到您创建的新数据库中以替换旧数据库。当然,您应该使用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.
执行完整重命名的最简单且万无一失的方法(包括在最后删除旧数据库,因此它是重命名而不是复制):
mysqladmin -uroot -pmypassword create newdbname
mysqldump -uroot -pmypassword --routines olddbname | mysql -uroot -pmypassword newdbname
mysqladmin -uroot -pmypassword drop olddbname
步骤:
复制行到记事本。 替换所有对"olddbname", "newdbname", "mypassword"(+可选的"root")的引用。 在命令行上逐一执行(在提示时输入“y”)。
这里已经有很多非常好的答案,但我没有看到PHP版本。这将在大约一秒钟内复制一个800M DB。
$oldDbName = "oldDBName";
$newDbName = "newDBName";
$oldDB = new mysqli("localhost", "user", "pass", $oldDbName);
if($oldDB->connect_errno){
echo "Failed to connect to MySQL: (" . $oldDB->connect_errno . ") " . $oldDB->connect_error;
exit;
}
$newDBQuery = "CREATE DATABASE IF NOT EXISTS {$newDbName}";
$oldDB->query($newDBQuery);
$newDB = new mysqli("localhost", "user", "pass");
if($newDB->connect_errno){
echo "Failed to connect to MySQL: (" . $newDB->connect_errno . ") " . $newDB->connect_error;
exit;
}
$tableQuery = "SHOW TABLES";
$tableResult = $oldDB->query($tableQuery);
$renameQuery = "RENAME TABLE\n";
while($table = $tableResult->fetch_array()){
$tableName = $table["Tables_in_{$oldDbName}"];
$renameQuery .= "{$oldDbName}.{$tableName} TO {$newDbName}.{$tableName},";
}
$renameQuery = substr($renameQuery, 0, strlen($renameQuery) - 1);
$newDB->query($renameQuery);