DBI安裝:DBI詳細信息參考:http://dbi.perl.org/
1.下載DBI包:
wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.620.tar.gz
2 解壓
tar xzf DBI-1.620.tar.gz
3 進入解壓包內,
perl Makefile.PL
4 make test
5 make
6 make install (如果不是root用戶,要sudo一下)
DBD安裝:
1.下載DBD
wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.021.tar.gz
2. 3. 同上
在進行第三步是可能會出現mysql_config找不到的情況,應該是沒有安裝開發工具包
sudo apt-get install libmysqld-dev
sudo apt-get install libmysqlclient-dev
來安裝這兩個包。
其余同上。
完成安裝後就可以利用perl腳本進行mysql數據的操作了。
編寫perl腳本操作數據庫
復制代碼 代碼如下:
#! /usr/bin/perl
use DBI;
my $driver="DBI:mysql";
my $database="perl_test";
my $user="root";
my $host="localhost";
my $passwd="root";
my $rules="alert_rules";
my $dbh = DBI->connect("$driver:database=$database;host=$host;user=$user;password=$passwd")
or die "Can't connect: " . DBI->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
# 打印獲取的數據
while(@recs=$sth->fetchrow_array){
print $recs[0].":".$recs[1]."\n";
}
$sth->finish();
$dbh->disconnect();