Sometimes perl is required to operate mysql databases, which can be implemented through DBI. For more information, see
Sometimes perl is required to operate mysql databases, which can be implemented through DBI. For more information, see
DBI installation: For more information about DBI, see:
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
The Code is as follows:
#! /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 ();