Operate mysql database _ MySQL in linux using perl

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

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.