我有多个数据库的mysqldump文件(5)。其中一个数据库需要很长时间才能加载,是否可以通过数据库拆分mysqldump文件,或者只是告诉mysql仅加载指定的数据库之一?

曼尼什

最佳答案

这个Perl脚本应该可以解决问题。

#!/usr/bin/perl -w
#
# splitmysqldump - split mysqldump file into per-database dump files.

use strict;
use warnings;

my $dbfile;
my $dbname = q{};
my $header = q{};

while (<>) {

    # Beginning of a new database section:
    # close currently open file and start a new one
    if (m/-- Current Database\: \`([-\w]+)\`/) {
    if (defined $dbfile && tell $dbfile != -1) {
        close $dbfile or die "Could not close file!"
    }
    $dbname = $1;
    open $dbfile, ">>", "$1_dump.sql" or die "Could not create file!";
    print $dbfile $header;
    print "Writing file $1_dump.sql ...\n";
    }

    if (defined $dbfile && tell $dbfile != -1) {
    print $dbfile $_;
    }

    # Catch dump file header in the beginning
    # to be printed to each separate dump file.
    if (! $dbname) { $header .= $_; }
}
close $dbfile or die "Could not close file!"

对包含所有数据库的转储文件运行此命令
./splitmysqldump < all_databases.sql

10-06 04:02