首先需要安装cpanm模块
wget https://www.cpan.org/src/5.0/perl-5.30.0.tar.gz
tar -xzf perl-5.30.0.tar.gz
cd perl-5.30.0
./Configure -des -Dprefix=$HOME/localperl
make
make test
make install
然后通过cpanm来安装DBI:
cpan 然后进入交互式界面
>install DBI;
连接数据库的操作就是有一个驱动句柄(mysql|Pg),数据库名 d a t a b a s e = " d b n a m e " , 数 据 源 D B I : 驱 动 : d b n a m e = database = "dbname",数据源DBI: 驱动:dbname= database="dbname",数据源DBI:驱动:dbname=database;host=127.0.0.1;port=5432;
d s n = " D B I : dsn="DBI: dsn="DBI:driver:dbname= d a t a b a s e ; h o s t = 127.0.0.1 ; p o r t = 5432 " 用 户 名 , 密 码 , 连 接 数 据 库 返 回 数 据 库 句 柄 D B I − > c o n n e c t ( database;host=127.0.0.1;port=5432" 用户名,密码,连接数据库返回数据库句柄DBI->connect( database;host=127.0.0.1;port=5432"用户名,密码,连接数据库返回数据库句柄DBI−>connect(dsn, $userid, $password, { RaiseError => 1 })
my $driver = "Pg";
my $database = "vmtest";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "postgres";
my $password = "postgres";
my $dbh = DBI->connect($dsn, $userid, $password