`
thinke365
  • 浏览: 50977 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

mogdbsetup命令

阅读更多
#!/usr/bin/perl
use strict;
use Getopt::Long;
use lib 'lib';
use MogileFS::Store; // 解压文件夹对应的位置是lib\MogileFS,直接使用模块吗?对应于Store.pm, perl module?
use MogileFS::Config;

# Rename binary in process list to make init scripts saner
$0 = $_ = $0;

my %args = (                      // Perl这里定义的是映射类型吗?Pythony也不怎么用,很快就搞混了。
            dbhost => "localhost",
            dbport => undef,      // 这种变量可以设置为undef的?
            dbname => "mogilefs",
            dbrootuser => undef,
            dbrootpass => "",
            dbuser => "mogile",
            dbpass => "",
            );

my $opt_help;
my $opt_verbose = 0;
my $opt_yes = 0;
my $opt_noschemabump;
my $dbtype = "MySQL"; // 默认是MySQL数据库
my $plugins;

usage()
    unless GetOptions(
                      "dbhost=s"         => \$args{dbhost},
                      "dbport=s"         => \$args{dbport},
                      "dbname=s"         => \$args{dbname},
                      "dbrootuser=s"     => \$args{dbrootuser},
                      "dbrootpassword:s" => \$args{dbrootpass},
                      "dbuser=s"         => \$args{dbuser},
                      "dbpassword:s"     => \$args{dbpass},
                      "help"             => \$opt_help,
                      "verbose"          => \$opt_verbose,
                      "yes"              => \$opt_yes,
                      "noschemabump"     => \$opt_noschemabump,
                      "type=s"           => \$dbtype,
                      "plugins=s"        => \$plugins,
                      );  // 这个GetOptions定义在何处的?

usage() if $opt_help;

# Be nice about what the default admin user is called.
if(!defined($args{dbrootuser})) {  // 上面出现过这个变量,但是是udef
    $args{dbrootuser} = 'root' if $dbtype =~ /MySQL/i;  // ... 原来使用正则表达式了,默认是使用MySQL
    $args{dbrootuser} = 'postgres' if $dbtype =~ /Postgres/i;
}
# Saner port management.
# This should default to the UNIX sockets on localhost
if(!defined($args{dbport}) and $args{dbhost} != "localhost" and $args{dbhost} != "127.0.0.1") {
    $args{dbport} = '3306' if $dbtype =~ /MySQL/i;
    $args{dbport} = '5432' if $dbtype =~ /Postgres/i;
}

sub usage {  // 这个sub usage的sub用来干什么的?
    die <<USAGE;
// mogdbsetup --help才会显示如下信息
Usage: mogdbsetup [opts]

Options:

                  Default      Description
                  ============ ===========================================
 --verbose        <off>        Be verbose about what\'s happening.

 --dbhost=        localhost    hostname or IP to database server.

 --dbport=        dbd default  port number to database server.

 --dbname=        mogilefs     database name to create/upgrade.

 --dbrootuser=    root         Database administrator username.  Only needed
                               for initial setup, not subsequent upgrades.

 --dbrootpass=    <blank>      Database administrator password.  Only needed
                               for initial setup, not subsequent upgrades.

 --dbuser=        mogile       Regular database user to create and/or use
                               for MogileFS database.  This is what the
                               mogilefsd trackers connect as.

 --dbpass=        <blank>      You should change this, especially if your
                               database servers are accessible to other users
                               on the network.  But they shouldn't be
                               if you're running MogileFS, because MogileFS
                               assumes your network is closed.

  --type=         MySQL        Which MogileFS::Store implementation to use.
                               Available: MySQL, Postgres

  --yes                        Run without questions.
// 这个USAGE是什么,提示时并不显示?
USAGE
}

my $sclass = "MogileFS::Store::$dbtype";
eval "use $sclass; 1;" or die "Failed to load $sclass: $@";

foreach my $plugin (split /\s*,\s*/, $plugins) {
    eval "use MogileFS::Plugin::$plugin; 1;" or die "Failed to load plugin $plugin: $@";
}

// 运行mogdbsetup,首先出来的是下面这段话
confirm("This will attempt to setup or upgrade your MogileFS database.\nIt won't destroy existing data.\nRun with --help for more information.  Run with --yes to shut up these prompts.\n\nContinue?", 0);

// 如果运行命令不带任何参数,现在执行显示报错信息 @error_msg1

$sclass->on_status(\&status);
$sclass->on_confirm(\&confirm);

MogileFS::Config->load_config;  // 这种是什么写法?

// 下面是生成数据库连接对象吗?
my $sto = $sclass->new_from_mogdbsetup(
                                       map { $_ => $args{$_} }
                                       qw(dbhost dbport dbname
                                          dbrootuser dbrootpass
                                          dbuser dbpass)
                                       ); // qw用于生成数据库连接串?
my $dbh = $sto->dbh;

$sto->setup_database
    or die "Database upgrade failed.\n";

my $latestver = MogileFS::Store->latest_schema_version;
if ($opt_noschemabump) {
    warn "\n*\n* Per your request, NOT UPGRADING to $latestver.  I assume you understand why.\n*\n";
} else {
    $sto->set_schema_vesion($latestver);
}


warn "Done.\n" if $opt_verbose;
exit 0;

############################################################################

sub confirm {
    my $q = shift;
    my $def = shift;
    $def = 1 unless defined $def;

    return 1 if $opt_yes;
    my $deftext = $def ? "[Y/n]" : "[N/y]";

    print "\n$q $deftext: ";
    my $ans = <STDIN>;
    if ($ans =~ /^\s*$/) {
        die "Stopped.\n" unless $def;
        return 0;
    }
    return 1 if $ans =~ /^y/i;
    die "Stopped.\n";
}

sub status {
    warn "$_[0]\n" if $opt_verbose;
}





@error_msg1:Failed to connect to DBI:mysql:mysql;host=localhost as specified root user (root): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]#

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics