联系方式

文章目录
前言
前提条件
示例代码
验证代码
前言
无论是mysql mha或是percona toolkit,都是通过perl编写。为了更深入掌握这些运维工具的原理, 需要了解perl的一些基本语法及使用事项。开源时代的到来,对于数据库从业者的技术素质越来越广。本文普及下通过perl的数据库驱动接口DBI访问mysql数据库。
前提条件
[root@three57 ~]# rpm -qa|grep -i --color dbd-mysqlperl-DBD-MySQL-4.013-3.el6.x86_64[root@three57 ~]# perl -vThis is perl, v5.10.1 (*) built for x86_64-linux-thread-multi[root@three57 ~]# mysql -Vmysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using EditLine wrapper
示例代码
[root@three57 perl_dir]# more perl_con_db.pl#!/usr/bin/perl -w#use strict;use DBI; #使用包my $host = "localhost"; #主机名称my $driver = "mysql"; #驱动为mysqlmy $database = "zxydb"; #连接的数据库#数据库驱动对象句柄my $dsn = "DBI:$driver:database=$database:$host";my $userid = "root"; #数据库用户my $password = "system"; #数据库用户密码#连接数据库my $dbh = DBI->connect($dsn,$userid,$password);my $state = $dbh->prepare("select count(*) from t_go"); #预处理SQL语句$state->execute(); #执行SQL语句$state->finish();my $state2 = $dbh->prepare("create table t_nbb(a int);"); #准备SQL语句$state2->execute(); #执行SQL语句#完成处理$state->finish();#关闭连接$dbh->disconnect();[root@three57 perl_dir]#
验证代码
mysql> show tables;+-----------------+| Tables_in_zxydb |+-----------------+| t_current || t_go || t_run || t_self || t_slow || t_slow2 |+-----------------+6 rows in set (0.00 sec)[root@three57 perl_dir]# perl perl_con_db.plmysql> show tables;+-----------------+| Tables_in_zxydb |+-----------------+| t_current || t_go || t_nbb || t_run || t_self || t_slow || t_slow2 |+-----------------+7 rows in set (0.00 sec)
文章转载自lovedb,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




