#!/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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.