📄 mysqld_multi
字号:
#!/usr/bin/perluse Getopt::Long;use POSIX qw(strftime);$|=1;$VER="2.15";$opt_config_file = undef();$opt_example = 0;$opt_help = 0;$opt_log = undef();$opt_mysqladmin = "/usr/local/bin/mysqladmin";$opt_mysqld = "/usr/local/libexec/mysqld";$opt_no_log = 0;$opt_password = undef();$opt_tcp_ip = 0;$opt_user = "root";$opt_version = 0;$opt_silent = 0;$opt_verbose = 0;my $my_print_defaults_exists= 1;my $logdir= undef();my ($mysqld, $mysqladmin, $groupids, $homedir, $my_progname);$homedir = $ENV{HOME};$my_progname = $0;$my_progname =~ s/.*[\/]//;main();######## main sub routine####sub main{ my $flag_exit= 0; if (!defined(my_which(my_print_defaults))) { # We can't throw out yet, since --version, --help, or --example may # have been given print "WARNING: my_print_defaults command not found.\n"; print "Please make sure you have this command available and\n"; print "in your path. The command is available from the latest\n"; print "MySQL distribution.\n"; $my_print_defaults_exists= 0; } if ($my_print_defaults_exists) { foreach my $arg (@ARGV) { if ($arg =~ m/^--config-file=(.*)/) { if (!length($1)) { die "Option config-file requires an argument\n"; } elsif (!( -e $1 && -r $1)) { die "Option file '$1' doesn't exists, or is not readable\n"; } else { $opt_config_file= $1; if (!($opt_config_file =~ m/\//)) { # No path. Use current working directory $opt_config_file= "./" . $opt_config_file; } } } } my $com= "my_print_defaults "; $com.= "--config-file=$opt_config_file " if (defined($opt_config_file)); $com.= "mysqld_multi"; my @defops = `$com`; chop @defops; splice @ARGV, 0, 0, @defops; } if (!GetOptions("help","example","version","mysqld=s","mysqladmin=s", "config-file=s","user=s","password=s","log=s","no-log", "tcp-ip", "silent","verbose")) { $flag_exit= 1; } if (defined($opt_config_file) && !($opt_config_file =~ m/\//)) { # No path. Use current working directory $opt_config_file= "./" . $opt_config_file; } usage() if ($opt_help); if ($opt_verbose && $opt_silent) { print "Both --verbose and --silent has been given. Some of the warnings "; print "will be disabled\nand some will be enabled.\n\n"; } init_log() if (!defined($opt_log)); $groupids = $ARGV[1]; if ($opt_version) { print "$my_progname version $VER by Jani Tolonen\n"; exit(0); } example() if ($opt_example); if ($flag_exit) { print "Error with an option, see $my_progname --help for more info.\n"; exit(1); } if (!defined(my_which(my_print_defaults))) { print "ABORT: Can't find command 'my_print_defaults'.\n"; print "This command is available from the latest MySQL\n"; print "distribution. Please make sure you have the command\n"; print "in your PATH.\n"; exit(1); } usage() if (!defined($ARGV[0]) || (!($ARGV[0] =~ m/^start$/i) && !($ARGV[0] =~ m/^stop$/i) && !($ARGV[0] =~ m/^report$/i))); if (!$opt_no_log) { w2log("$my_progname log file version $VER; run: ", "$opt_log", 1, 0); } else { print "$my_progname log file version $VER; run: "; print strftime "%a %b %e %H:%M:%S %Y", localtime; print "\n"; } if ($ARGV[0] =~ m/^start$/i) { if (!defined(($mysqld= my_which($opt_mysqld))) && $opt_verbose) { print "WARNING: Couldn't find the default mysqld binary.\n"; print "Tried: $opt_mysqld\n"; print "This is OK, if you are using option \"mysqld=...\" in "; print "groups [mysqldN] separately for each.\n\n"; } start_mysqlds(); } else { if (!defined(($mysqladmin= my_which($opt_mysqladmin))) && $opt_verbose) { print "WARNING: Couldn't find the default mysqladmin binary.\n"; print "Tried: $opt_mysqladmin\n"; print "This is OK, if you are using option \"mysqladmin=...\" in "; print "groups [mysqldN] separately for each.\n\n"; } if ($ARGV[0] =~ m/^report$/i) { report_mysqlds(); } else { stop_mysqlds(); } }}######## Quote option argument. Add double quotes around the argument#### and escape the following: $, \, "#### This function is needed, because my_print_defaults drops possible#### quotes, single or double, from in front of an argument and from#### the end.####sub quote_opt_arg{ my ($option)= @_; if ($option =~ m/(\-\-[a-zA-Z0-9\_\-]+)=(.*)/) { $option= $1; $arg= $2; $arg=~ s/\\/\\\\/g; # Escape escape character first to avoid doubling. $arg=~ s/\$/\\\$/g; $arg=~ s/\"/\\\"/g; $arg= "\"" . $arg . "\""; $option= $option . "=" . $arg; } return $option;}######## Init log file. Check for appropriate place for log file, in the following#### order my_print_defaults mysqld datadir, /usr/local/share, /var/log, /tmp####sub init_log{ if ($my_print_defaults_exists) { @mysqld_opts= `my_print_defaults mysqld`; chomp @mysqld_opts; foreach my $opt (@mysqld_opts) { if ($opt =~ m/^\-\-datadir[=](.*)/) { if (-d "$1" && -w "$1") { $logdir= $1; } } } } if (!defined($logdir)) { $logdir= "/usr/local/share" if (-d "/usr/local/share" && -w "/usr/local/share"); } if (!defined($logdir)) { # Log file was not specified and we could not log to a standard place, # so log file be disabled for now. if (!$opt_silent) { print "WARNING: Log file disabled. Maybe directory or file isn't writable?\n"; } $opt_no_log= 1; } else { $opt_log= "$logdir/mysqld_multi.log"; }}######## Report living and not running MySQL servers####sub report_mysqlds{ my (@groups, $com, $i, @options, $pec); print "Reporting MySQL servers\n"; if (!$opt_no_log) { w2log("\nReporting MySQL servers","$opt_log",0,0); } @groups = &find_groups($groupids); for ($i = 0; defined($groups[$i]); $i++) { $com= get_mysqladmin_options($i, @groups); $com.= " ping >> /dev/null 2>&1"; system($com); $pec = $? >> 8; if ($pec) { print "MySQL server from group: $groups[$i] is not running\n"; if (!$opt_no_log) { w2log("MySQL server from group: $groups[$i] is not running", "$opt_log", 0, 0); } } else { print "MySQL server from group: $groups[$i] is running\n"; if (!$opt_no_log) { w2log("MySQL server from group: $groups[$i] is running", "$opt_log", 0, 0); } } } if (!$i) { print "No groups to be reported (check your GNRs)\n"; if (!$opt_no_log) { w2log("No groups to be reported (check your GNRs)", "$opt_log", 0, 0); } }}######## start multiple servers####sub start_mysqlds(){ my (@groups, $com, $tmp, $i, @options, $j, $mysqld_found, $info_sent); if (!$opt_no_log) { w2log("\nStarting MySQL servers\n","$opt_log",0,0); } else { print "\nStarting MySQL servers\n"; } @groups = &find_groups($groupids); for ($i = 0; defined($groups[$i]); $i++) { $com = "my_print_defaults"; $com.= defined($opt_config_file) ? " --config-file=$opt_config_file" : ""; $com.= " $groups[$i]"; @options = `$com`; chop @options; $mysqld_found= 1; # The default $mysqld_found= 0 if (!length($mysqld)); $com= "$mysqld"; for ($j = 0, $tmp= ""; defined($options[$j]); $j++) { if ("--mysqladmin=" eq substr($options[$j], 0, 13)) { # catch this and ignore } elsif ("--mysqld=" eq substr($options[$j], 0, 9)) { $options[$j]=~ s/\-\-mysqld\=//; $com= $options[$j]; $mysqld_found= 1; } else { $options[$j]= quote_opt_arg($options[$j]); $tmp.= " $options[$j]"; } } if ($opt_verbose && $com =~ m/\/safe_mysqld$/ && !$info_sent) { print "WARNING: safe_mysqld is being used to start mysqld. In this case you "; print "may need to pass\n\"ledir=...\" under groups [mysqldN] to "; print "safe_mysqld in order to find the actual mysqld binary.\n"; print "ledir (library executable directory) should be the path to the "; print "wanted mysqld binary.\n\n"; $info_sent= 1; } $com.= $tmp; $com.= " >> $opt_log 2>&1" if (!$opt_no_log); $com.= " &"; if (!$mysqld_found) { print "\n"; print "FATAL ERROR: Tried to start mysqld under group [$groups[$i]], "; print "but no mysqld binary was found.\n"; print "Please add \"mysqld=...\" in group [mysqld_multi], or add it to "; print "group [$groups[$i]] separately.\n"; exit(1); } system($com); } if (!$i && !$opt_no_log) { w2log("No MySQL servers to be started (check your GNRs)", "$opt_log", 0, 0); }}######## stop multiple servers####sub stop_mysqlds(){ my (@groups, $com, $i, @options); if (!$opt_no_log) { w2log("\nStopping MySQL servers\n","$opt_log",0,0); } else { print "\nStopping MySQL servers\n"; } @groups = &find_groups($groupids); for ($i = 0; defined($groups[$i]); $i++) { $com= get_mysqladmin_options($i, @groups); $com.= " shutdown"; $com.= " >> $opt_log 2>&1" if (!$opt_no_log); $com.= " &"; system($com); } if (!$i && !$opt_no_log) { w2log("No MySQL servers to be stopped (check your GNRs)", "$opt_log", 0, 0); }}######## Sub function for mysqladmin option parsing####sub get_mysqladmin_options{ my ($i, @groups)= @_; my ($mysqladmin_found, $com, $tmp, $j); $com = "my_print_defaults"; $com.= defined($opt_config_file) ? " --config-file=$opt_config_file" : ""; $com.= " $groups[$i]"; @options = `$com`; chop @options; $mysqladmin_found= 1; # The default $mysqladmin_found= 0 if (!length($mysqladmin)); $com = "$mysqladmin"; $tmp = " -u $opt_user"; if (defined($opt_password)) { my $pw= $opt_password; # Protect single quotes in password $pw =~ s/'/'"'"'/g;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -