Operate mysql databases using perl in linux (DBI needs to be installed)

Source: Internet
Author: User
Tags perl script
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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.