MySQL手册中有介绍。

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

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


当前回答

如果你更喜欢GUI工具,并且碰巧安装了MySQL Workbench,你可以使用内置的迁移向导

其他回答

你们这些家伙会为此开枪打死我的,很可能这不会每次都管用,当然,这违反了所有的逻辑……但我刚试过的是…停止MySQL引擎,以root身份登录,并在文件系统级别上简单地重命名数据库....

我在OSX上,只改变了情况,从bedbf到bedbf。令我惊讶的是,它竟然起作用了……

我不建议在生产DB上使用它。我只是做了个实验……

祝你好运! -)

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.

这里已经有很多非常好的答案,但我没有看到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);

如果您使用分层视图(视图从其他视图中提取数据),从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

你不能这么做是有原因的。(尽管有很多尝试的答案)

基本的答案在许多情况下都有效,但在其他情况下会导致数据损坏。 需要根据对数据库的启发式分析来选择策略。 这就是实现这个特性,然后又删除它的原因。(医生)

您需要转储该数据库中的所有对象类型,创建新命名的对象类型,然后导入转储。如果这是一个正常的系统,你得把它关掉。如果不能,则需要设置从此数据库到新数据库的复制。

如果您想查看可以执行此操作的命令,@satishD提供了详细信息,其中传达了一些挑战,您需要围绕这些挑战构建与目标数据库匹配的策略。