Get the App
SLTechnology News&Howtos  ›  Database  › 

How does Perl connect to the database mysql

Shulou Source: shulou.com Published: 2022-05-31 18:40:31 10月03日 Update

This article mainly explains "how to connect Perl to database mysql". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Perl how to connect to the database mysql"!

Command line parameters

1.1 Code

#! / usr/bin/env perluse Getopt::Long;use Data::Dumper;use Pod::Usage;my $Debug = 1 MAIN dprint (@) {return unless $Debug; chomp (my @ m = @ _); print STDERR 'DEBUG:', join ("", @ m), "\ n";} MAIN: {my $db_user; my $db_pass GetOptions (\ my% opt, 'debug | dumped' = >\ $Debug, 'help | h |?' = > sub {pod2usage (- verbose= > 1)}, 'db_user | utiliss' = >\ $db_user, 'db_pass | xSecrets' = >\ $db_pass,) or pod2usage () Pod2usage (- message= > "must requires db_user, db_pass!") Unless $db_user & & $db_pass; dprint "db_user: $db_user"; dprint "db_pass: $db_pass";}

1.2 use

Command: "-" or "-"

. / test.pl-db_user testuser-db_pass testpass

. / test.pl-- u testuser-- x testpass

Second, connect to database mysql

2.1 Code

#! / usr/bin/env perluse DBI;MAIN: {my $dbh = DBI- > connect ("DBI:mysql:clouxns:202.85.222.121:3306", "root", "zxc1758 percent percent *"); $query_sql = "SELECT zone_id, zone_name FROM xns_zone"; $query_sth = $dbh- > prepare ($query_sql); $query_sth- > execute () or die "can't run query sql:$dbh- > errstr" While (my @ data = $query_sth- > fetchrow_array ()) {my $zone_id = $data [0]; my $zone_name = $data [1]; print "$zone_id\ t$zone_name\ n";} $dbh- > disconnect ();}

3. DNS query

Dns queries A record, NS, SOA.

#! / usr/bin/env perluse Net::DNS::Resolver;use Net::DNS::Packet;MAIN: {my $resolver = Net::DNS::Resolver- > new;#Find a host's address my $query = $resolver- > search ("www.guowenyan.cn") If ($query) {foreach my $rr (grep {$_ > type eq "A"} $query- > answer) {print $rr- > address, "\ n";}} else {warn "query failed:", $resolver- > errorstring, "\ n" } # Find the nameservers for a domain. My $query = $resolver- > search ("baidu.com", "NS"); if ($query) {foreach my $rr (grep {$_-> type eq "NS"} $query- > answer) {print $rr- > nsdname, "\ n" }} else {warn "query failed:", $resolver- > errorstring, "\ n";} # Find a domain's SOA record in zone file format. My $query = $resolver- > search ("baidu.com", "SOA"); if ($query) {($query- > answer) [0]-> print;} else {warn "query failed:", $resolver- > errorstring, "\ n";}}

4. Send mail

4.1 Code

#! / usr/bin/env perluse Net::SMTP;sub send_mail () {my @ mail_to = ('480160531roomqq.compose,' 2135361204roomqq.com'); my $mail_from = 'kan_001@163.com'; my $mail_pass = "123zxc"; my $mail_message = "aanbb"; my $smtp = Net::SMTP- > new ("smtp.163.com") $smtp- > auth ($mail_from, $mail_pass) | | die "Auth Error! $!"; foreach my $mail_to (@ mail_to) {$smtp- > mail ($mail_from); $smtp- > to ($mail_to); # Start the mail $smtp- > data () # Send the header $smtp- > datasend ("From: $mail_from\ n"); $smtp- > datasend ("To: $mail_to\ n"); $smtp- > datasend ("Subject: the mail is sended by perl.\ n"); $smtp- > datasend ("\ n") # send the message $smtp- > datasend ("$mail_message\ n"); # send the termination string $smtp- > dataend ();} $smtp- > quit ();}

4.2 points for attention

1. Authen::SASL:perl-MCPAN-e shell cpan > install Authen::SASL needs to be installed.

two。 The sender and recipient must be single quotation marks ('kan_001.163.com').

3. There must be before and after the sender, recipient, subject and body in the header. (otherwise the letter will be returned by the system)

At this point, I believe you have a deeper understanding of "Perl how to connect to the database mysql". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data database code query content sender command recipient mail learning practical deeper subject item interest parameter practicality practice quotation marks simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Huawei macOS Xiaomi