Assume that the Perl language and MySQL database are installed.
DBI Installation: DBI Detailed information reference: http://dbi.perl.org/
1. Download DBI Package:
wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.620.tar.gz
2 Decompression
Tar xzf dbi-1.620.tar.gz
3 into the unpack package,
Perl makefile.pl
4 Make Test
5 Make
6 make install (if not root user, sudo)
DBD Installation:
1. Download DBD
wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.021.tar.gz
2.3. Ditto
In the third step is likely to appear mysql_config can not find the situation, should be not installed development kit
sudo apt-get install Libmysqld-dev
sudo apt-get install Libmysqlclient-dev
To install the two packages.
The rest is ditto.
After the installation, you can use the Perl script to do the operation of MySQL data.
Write the Perl scripting operations database
The code is as follows |
Copy Code |
#! /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:d atabase= $database; host= $host; user= $user;p assword= $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; # Print the Acquired data while (@recs = $sth->fetchrow_array) { Print $recs [0]. ":". $recs [1]. " n "; } $sth->finish (); $DBH->disconnect (); |