我正在嘗試在perl腳本中構建一些函數來創建,刪除和授予mysql數據庫的權限。檢查試圖授予權限時數據庫是否存在
此刻,我嘗試授予權限的用戶指定的數據庫,如:
use DBI;
my $dbname=$_[1];
my $dbh = DBI->connect("dbi:mysql:", $sqluser,$sqlpass) or die "Unable to connect: $DBI::errstr\n";
.....
my [email protected][0];
my [email protected][1];
my $db_com="GRANT ALL ON ".$dbname.".* TO '".$username."'\@'localhost'";
my $sth = $dbh->prepare($db_com);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
print "Permissions granted\n";
這工作該用戶是否存在。
在爲CREATE USER和DROP USER嘗試類似的代碼時,它可以高效地工作,根據請求創建或刪除它們,或者在用戶不存在時拋出mysql語法錯誤。但是,在授予權限的同時,即使數據庫不存在,該命令也會執行,但我卻置之不理。
其實mysql的提示,如果執行的SQL查詢,它返回:
Query OK, 0 rows affected (0.00 sec)
我需要一種方法來發現數據庫是否不存在,並提示用戶提供正確的數據庫名稱。
你看過嗎? http://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-peform-action-based-on-result – Populus
我已經包括了幾行解釋我在做什麼。我想使用perl DBD :: mysql驅動程序來執行此操作。 – Droidzone
你看看這裏:http://stackoverflow.com/questions/838978/how-to-check-if-mysql-database-exists#838993 – Axeman