Perlを使用してPostgreSQLに接続するサンプルコードを以下に記します。
使用した環境は以下の通りです。
CentOSの場合は、rootユーザになり以下のコマンドでMySQLをアクセスするためのPerlモジュールをインストールすることができます。
yum -y install perl-DBI perl-DBD-MySQL
#!/usr/bin/perl
use DBI;
# MySQL
our $DB_NAME = "mysql";
our $DB_USER = "root";
our $DB_PASS = "sakura";
our $DB_HOST = "localhost";
our $DB_PORT = "3306";
my $dbh = DBI->connect("dbi:mysql:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT","$DB_USER","$DB_PASS") or die "$!\n Error: failed to connect to DB.\n";
my $sth = $dbh->prepare("SELECT now();");
$sth->execute();
while (my $ary_ref = $sth->fetchrow_arrayref) {
my ($row) = @$ary_ref;
print $row , "\n";
}
$sth->finish;
$dbh->disconnect;
$ ./mysql_connect.pl 2012-10-08 00:48:46
上記のサンプルコードは、ホスト名、ユーザ名、パスワードを設定しています。
みなさんの環境にあう設定に変更してください。