MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
当前回答
似乎没有人提到这一点,但这里有另一种方式:
create database NewDatabaseName like OldDatabaseName;
然后对每个表执行:
create NewDatabaseName.tablename like OldDatabaseName.tablename;
insert into NewDataBaseName.tablename select * from OldDatabaseName.tablename;
然后,如果你想,
drop database OldDatabaseName;
这种方法的优点是可以在几乎为零的网络流量的情况下在服务器上完成整个传输,因此它比转储/恢复快得多。
如果你有存储过程/视图等,你可能也想要传输它们。
其他回答
ALTER DATABASE是MySQL建议的解决方法,RENAME DATABASE被删除。
从13.1.32 RENAME数据库语法:
RENAME {DATABASE | SCHEMA} db_name TO new_db_name;
此语句是在MySQL 5.1.7中添加的,但被发现是危险的,在MySQL 5.1.23中被删除。
简单的方法
切换到数据库目录:
cd /var/lib/mysql/
关闭MySQL…这很重要!
/etc/init.d/mysql stop
好吧,这种方法不适用于InnoDB或BDB-Databases。
重命名数据库:
mv old-name new-name
...或者桌子……
cd database/
mv old-name.frm new-name.frm
mv old-name.MYD new-name.MYD
mv old-name.MYI new-name.MYI
重新启动MySQL
/etc/init.d/mysql start
做……
好吧,这种方法不适用于InnoDB或BDB数据库。在这种情况下,您必须转储数据库并重新导入它。
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.
实际上,最简单的答案是导出旧数据库,然后将其导入到您创建的新数据库中以替换旧数据库。当然,您应该使用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老手,我说过。 现在,你的状态应该很好。如果你还有任何问题,请到网上来找我。
对于InnoDB,下面的方法似乎是可行的:创建新的空数据库,然后依次将每个表重命名为新的数据库:
RENAME TABLE old_db.table TO new_db.table;
之后,您将需要调整权限。
对于在shell中编写脚本,您可以使用以下任何一种:
mysql -u username -ppassword old_db -sNe 'show tables' | while read table; \
do mysql -u username -ppassword -sNe "rename table old_db.$table to new_db.$table"; done
OR
for table in `mysql -u root -ppassword -s -N -e "use old_db;show tables from old_db;"`; do mysql -u root -ppassword -s -N -e "use old_db;rename table old_db.$table to new_db.$table;"; done;
注:
选项-p和密码之间没有空格。如果您的数据库没有密码,请删除-u username -ppassword部分。 如果某个表有触发器,则不能使用上述方法将其移动到另一个数据库(将导致trigger in wrong schema错误)。如果是这种情况,使用传统的方法克隆一个数据库,然后删除旧的: Mysqldump old_db | mysql new_db 如果你有存储过程,你可以在之后复制它们: mysqldump -R old_db | mysql的new_db