MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
MySQL手册中有介绍。
通常我只是转储数据库并用一个新名称重新导入它。这不是非常大的数据库的一个选项。重命名数据库| SCHEMA} db_name TO new_db_name做坏事,只存在于少数版本中,总的来说是个坏主意。
这需要与InnoDB一起工作,InnoDB存储的东西与MyISAM非常不同。
当前回答
这是一个快速生成重命名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' ;
其他回答
这是我所使用的:
$ 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;
对于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
执行完整重命名的最简单且万无一失的方法(包括在最后删除旧数据库,因此它是重命名而不是复制):
mysqladmin -uroot -pmypassword create newdbname
mysqldump -uroot -pmypassword --routines olddbname | mysql -uroot -pmypassword newdbname
mysqladmin -uroot -pmypassword drop olddbname
步骤:
复制行到记事本。 替换所有对"olddbname", "newdbname", "mypassword"(+可选的"root")的引用。 在命令行上逐一执行(在提示时输入“y”)。
如果您使用分层视图(视图从其他视图中提取数据),从mysqldump导入原始输出可能无法工作,因为mysqldump不关心视图的正确顺序。因此,我编写了脚本,重新排序视图,以纠正飞行中的顺序。
它是这样的:
#!/usr/bin/env perl
use List::MoreUtils 'first_index'; #apt package liblist-moreutils-perl
use strict;
use warnings;
my $views_sql;
while (<>) {
$views_sql .= $_ if $views_sql or index($_, 'Final view structure') != -1;
print $_ if !$views_sql;
}
my @views_regex_result = ($views_sql =~ /(\-\- Final view structure.+?\n\-\-\n\n.+?\n\n)/msg);
my @views = (join("", @views_regex_result) =~ /\-\- Final view structure for view `(.+?)`/g);
my $new_views_section = "";
while (@views) {
foreach my $view (@views_regex_result) {
my $view_body = ($view =~ /\/\*.+?VIEW .+ AS (select .+)\*\/;/g )[0];
my $found = 0;
foreach my $view (@views) {
if ($view_body =~ /(from|join)[ \(]+`$view`/) {
$found = $view;
last;
}
}
if (!$found) {
print $view;
my $name_of_view_which_was_not_found = ($view =~ /\-\- Final view structure for view `(.+?)`/g)[0];
my $index = first_index { $_ eq $name_of_view_which_was_not_found } @views;
if ($index != -1) {
splice(@views, $index, 1);
splice(@views_regex_result, $index, 1);
}
}
}
}
用法: mysqldump -u username -v olddatabase -p | ./mysqldump_view_reorder.pl | mysql -u username -p -D newdatabase .pl
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.