Operate mysql database bitsCN.com using perl in linux
Assume that the perl language and mysql database are installed.
DBI installation: DBI detailed information reference: http://dbi.perl.org/
1. download the DBI package:
Wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.620.tar.gz
2. decompress
Tar xzf DBI-1.620.tar.gz
3. enter the decompression package,
Perl Makefile. PL
4 make test
5 make
6 make install (if not the root user, sudo it)
DBD installation:
1. download DBD
Wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.021.tar.gz
2. 3. same as above
In the third step, mysql_config may not be found. The Development Kit should not be installed.
Sudo apt-get install libmysqld-dev
Sudo apt-get install libmysqlclient-dev
To install these two packages.
The rest are the same as above.
After installation, you can use the perl script to operate mysql data.
Write perl scripts to operate databases
#! /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 $ Something = $ dbh-> prepare ("select app_name, receivers from $ rules ");
$ Something-> execute () or die "Can't prepare SQL statement". $ Something-> errstr;
My $ Something = $ dbh-> prepare ("select app_name, receivers from $ rules ");
$ Something-> execute () or die "Can't prepare SQL statement". $ Something-> errstr;
# Print the obtained data
While (@ recs = $ Something-> fetchrow_array ){
Print $ recs [0]. ":". $ recs [1]. "/n ";
}
$ Something-> finish ();
$ Dbh-> disconnect ();
BitsCN.com