我對Perl很陌生。我正在嘗試編寫一個腳本,它將獲取一個mysqldump並將其恢復到一個新的數據庫中。主要想法是將數據庫從一臺服務器遷移到另一臺服務器。未定義的子程序和main :: promt
下面是我寫的紙條:
#!/usr/bin/perl
use warnings;
print "Starting the migration process!\n";
print "Enter the source server address, make sure you enter the FQDN of the server";
$source_address = promt ("Source server address: ");
check_string($source_address);
print "Enter the destination server address, make sure you enter the FQDN of the server";
$destination_address = promt ("Destination server address:");
check_string($destination_address);
print "Enter the Source server password for the root user";
$source_password = promt ("Source server address:");
check_string($source_password);
print "Enter the destination server password for the root user";
$destination_password = promt ("Destination server address:");
check_string($destination_password);
$current_dir = cwd();
system("mysqldump --single-transaction -u root -p$source_password --force -h $source_address -A -R -E --triggers
--routines --max_allowed_packet=512M | gzip -c >$current_dir/old_db_dump.sql") or die "system call to create Mysqldump failed: $?";
system("pt-show-grants -uroot -p$source_password -h $source_address > $current_dir/old_grants.sql") or die "system call to create grant failed: $?";
system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_db_dump.sql") or die "System call to import the sqldump failed: $?";
system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_grants.sql") or die "System call to import the grants failed: $?";
# A function that checks if the passed string is null or not
sub check_string{
$string_to_check = $_[0];
if ($string_to_check eq '') {
print "The entered value is empty, the program will exit now, re-run the program";
exit 0;
}
}
sub prompt {
my ($text) = @_;
print $text;
my $answer = <STDIN>;
chomp $answer;
return $answer;
}
但是,當我嘗試執行代碼,我結束了以下錯誤:
Starting the migration process!
Undefined subroutine &main::promt called at migrate_mysql.pl line 26.
Enter the source server address, make sure you enter the FQDN of the server
寫作的提示功能,我也跟着在這裏發佈的教程在這裏:http://perlmaven.com/subroutines-and-functions-in-perl
我不知道爲什麼我在這裏得到這個錯誤。我必須包含一些包嗎?
而且這將是很好,如果你可以在代碼的系統塊上發表評論:
system("mysqldump --single-transaction -u root -p$source_password --force -h $source_address -A -R -E --triggers
--routines --max_allowed_packet=512M | gzip -c >$current_dir/old_db_dump.sql") or die "system call to create Mysqldump failed: $?";
system("pt-show-grants -uroot -p$source_password -h $source_address > $current_dir/old_grants.sql") or die "system call to create grant failed: $?";
system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_db_dump.sql") or die "System call to import the sqldump failed: $?";
system("mysql -u root -p$destination_password -h $destination_address < $current_dir/old_grants.sql") or die "System call to import the grants failed: $?";
上午我以正確的方式做這件事?我是否以正確的方式傳遞變量值?
錯字:比較'子提示符'到'promt(「源服務器地址:」);'(你錯過了'p')。 – ThisSuitIsBlackNot
另外,除了使用警告外,還應該使用strict;'' – ThisSuitIsBlackNot