zk mysql 主从自动切换

zookeeper测试;

DBI	版本:
/DBI-1.616#

zjtest7-redis:/root/DBD-mysql-4.031# perl Makefile.PL
Can't exec "mysql_config": No such file or directory at Makefile.PL line 73.

Cannot find the file 'mysql_config'! Your execution PATH doesn't seem
not contain the path to mysql_config. Resorting to guessed values!


PLEASE NOTE:

For 'make test' to run properly, you must ensure that the
database user 'root' can connect to your MySQL server
and has the proper privileges that these tests require such
as 'drop table', 'create table', 'drop procedure', 'create procedure'
as well as others.

mysql> grant all privileges on test.* to 'root'@'localhost' identified by 's3kr1t';

You can also optionally set the user to run 'make test' with:

perl Makefile.PL --testuser=username

Can't exec "mysql_config": No such file or directory at Makefile.PL line 481.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Failed to determine directory of mysql.h. Use

  perl Makefile.PL --cflags=-I<dir>

to set this directory. For details see DBD::mysql::INSTALL,
section "C Compiler flags" or type

  perl Makefile.PL --help

安装mysql-devel包

 yum install *mysql-devel*




安装zookeeper:
zjtest7-redis:/zookeeper-3.4.8/src/c# cpan ZooKeeper


mysql 主:
master:/root/zk# cat zk.pl
use ZooKeeper;
use AnyEvent;
use AE;
use Data::Dumper;
use IO::Socket;
   my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181');
   my $stat = $zk->exists('/mysql');
   unless ($stat){
              $zk->create('/mysql');
    }
   my $created_path = $zk->create('/mysql/0001', 
                                   ephemeral => 1
                                   );    
    print "----------------\n";
    print  $created_path;
    print "----------------\n";
    ###获取数据
    print Dumper($zk->get("/mysql/0001"));

   ###修改数据
    $zk->set('/mysql/0001' =>'192.168.32.6');
    print Dumper($zk->get("/mysql/0001")); 
    my $cv = AE::cv;
    ##所有子节点数组
    my $zk_value = $zk->get('/mysql/0001', watcher => sub { 
        ###事件状态
        my $event = shift;
        print "触发了事件.";
        print $event->{'type'}."\n";
        print "事件状态.";
        print $event->{'state'}."\n";
        $cv->send($event) });
        print "------------------\n";
        print $zk_value;
        print "\n";
        ###检测3306端口
    sub check_port {
                        ( $server, $port ) = ('127.0.0.1','3306');
                              $sock = IO::Socket::INET->new(PeerAddr => $server,
                              PeerPort => $port,
                              Proto => 'tcp');
                              print "$sock\n";
                             if ($sock)
                                {return 1}
                             else
                                {$zk->close; print "close zk\n"; exit 0 };  
                   };
        ##定义watch  
        my $t = AnyEvent->timer(  
        after    => 0,  
        interval => 5,  
        cb       =>  \&check_port
                             );
      ##不要再每秒打印时间  
      ##undef $t;  
        
      my $child_event = $cv->recv;
test:/root/zk# 


mysql 从:

slave:/root/zk# cat zk.pl
use ZooKeeper;
use AnyEvent;
use AE;
use Data::Dumper;
use IO::Socket;
   my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181');
   my $stat = $zk->exists('/mysql');
   unless ($stat){
              $zk->create('/mysql');
    }
   my $created_path = $zk->create('/mysql/0002', 
                                   ephemeral => 1
                                   );    
    print "----------------\n";
    print  $created_path;
    print "----------------\n";
    ###获取数据
    print Dumper($zk->get("/mysql/0002"));

   ###修改数据
    $zk->set('/mysql/0002' =>'192.168.32.116');
    print Dumper($zk->get("/mysql/0002")); 
    my $cv = AE::cv;
    ##所有子节点数组
    my $zk_value = $zk->get('/mysql/0002', watcher => sub { 
        ###事件状态
        my $event = shift;
        print "触发了事件.";
        print $event->{'type'}."\n";
        print "事件状态.";
        print $event->{'state'}."\n";
        $cv->send($event) });
        print "------------------\n";
        print $zk_value;
        print "\n";
        ###检测3306端口
    sub check_port {
                        ( $server, $port ) = ('127.0.0.1','3306');
                              $sock = IO::Socket::INET->new(PeerAddr => $server,
                              PeerPort => $port,
                              Proto => 'tcp');
                              print "$sock\n";
                             if ($sock)
                                {return 1}
                             else
                                {$zk->close; print "close zk\n"; exit 0 };  
                   };
        ##定义watch  
        my $t = AnyEvent->timer(  
        after    => 0,  
        interval => 5,  
        cb       =>  \&check_port
                             );
      ##不要再每秒打印时间  
      ##undef $t;  
        
      my $child_event = $cv->recv;



测试zk 脚本:
use ZooKeeper;
use AnyEvent;
use AE;
use Data::Dumper;
use IO::Socket;
   my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181');
   eval {
   my $stat = $zk->exists('/mysql/0001');
   if  ($stat){
         $mysql_ip =   $zk->get('/mysql/0001');
         print $mysql_ip."\n";
             }
         else{
            $mysql_ip =   $zk->get('/mysql/0002');
             };
  
     use DBI;
     my $database='zjzc';  
     my $user="zjzc_app";  
     my $passwd="1234567"; 
     my @arr2=();  
     my $dbh  = DBI->connect("dbi:mysql:database=$database;host=$mysql_ip;port=3306",$user,$passwd,{  
                          RaiseError => 1,  
                          AutoCommit => 0
                           } ) or die "can't connect to database ". DBI-errstr;
    my $hostSql = qq{select  id,name from scan; }; 
    my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);  
    my $selStmt = $dbh->prepare($hostSql);  
    $selStmt->execute();  
    $selStmt->bind_columns(undef, \$a1, \$a2);  
    $selStmt->execute();  
    while( $selStmt->fetch() )
         { push (@arr2, "$a1  $a2  $a3\n" );
         };
        print "\@arr2 is @arr2\n";
        $dbh->disconnect;
        };


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scan724

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值