Category: | Database Programming |
Author/Contact Info | Roman Chemisov gangabass@gmail.com |
Description: | Sometimes you need to create several databases and upload same SQL dump to each database. So this little script help you complete this task. You can run it like so: $batch_databases.pl --host="localhost" --user="root" --password="secretpass" --create_db="test_db" --create_user="test_db_user" --create_password="test_user_password" --dumpfile="test_dump2.sql" |
#!/usr/bin/perl use strict; use warnings; use DBI; use Getopt::Long; my ( $DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $create_db_name, $create_ +user_name, $create_password ); my ( $batch_file, $BLOG_PASS ); my $result = GetOptions ( "host=s" => \$DB_HOST, "user=s" => \$DB_USER, "password=s" => \$DB_PASS, "create_db=s" => \$create_db_name, "create_user=s" => \$create_user_name, "create_password=s" => \$create_password, "dumpfile=s" => \$batch_file, ); my $drh = DBI->install_driver("mysql"); my $rc = $drh->func('createdb', $create_db_name, $DB_HOST, $DB_USER, $ +DB_PASS, 'admin') or die $drh->errstr; my $dbh = connect_to_db( $create_db_name, $DB_USER, $DB_PASS, $DB_HOST + ); my $sth = $dbh->prepare(qq{ GRANT ALL on $create_db_name.* TO ?\@'localhost' IDENTIFIED BY ? }); $sth->execute( $create_user_name, $create_password ) or die $dbh->errs +tr; my @statements = read_batch_file($batch_file); foreach my $statement (@statements) { $statement =~ s{[^[:ascii:]]}{ }g; $dbh->do($statement) or die $dbh->errstr; } sub read_batch_file { my ($filename) = @_; my @statements; my $sql_dump; unless ( -e $filename ) { warn "Could't find such batch file. Something wrong with filen +ame?\n"; } else { open my $source, "<", $filename or die $!; { local $/; $sql_dump = <$source>; } close $source; remove_sqlyog_comments(\$sql_dump); remove_phpmyadmin_comments(\$sql_dump); } @statements = split ";\n", $sql_dump; return @statements; } sub remove_sqlyog_comments { my ($sql_dump_ref) = @_; ${$sql_dump_ref} =~ s{/\*.+?\*/;?\n}{}gs; } sub remove_phpmyadmin_comments { my ($sql_dump_ref) = @_; ${$sql_dump_ref} =~ s{^--.*?\n}{}gm; } sub connect_to_db { my ( $database, $user, $password, $hostname ) = @_; my $port = 3306; my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" +; my $dbh = DBI->connect( $dsn, $user, $password ); $dbh->do(qq{SET NAMES 'utf8'}) or die $dbh->errstr; return $dbh; } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Batch database creation
by graff (Chancellor) on Aug 10, 2008 at 22:10 UTC | |
Re: Batch database creation
by alexm (Chaplain) on Aug 10, 2008 at 20:30 UTC |
Back to
Code Catacombs