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();