zoukankan      html  css  js  c++  java
  • 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 "----------------
    ";
        print  $created_path;
        print "----------------
    ";
        ###获取数据
        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'}."
    ";
            print "事件状态.";
            print $event->{'state'}."
    ";
            $cv->send($event) });
            print "------------------
    ";
            print $zk_value;
            print "
    ";
            ###检测3306端口
        sub check_port {
                            ( $server, $port ) = ('127.0.0.1','3306');
                                  $sock = IO::Socket::INET->new(PeerAddr => $server,
                                  PeerPort => $port,
                                  Proto => 'tcp');
                                  print "$sock
    ";
                                 if ($sock)
                                    {return 1}
                                 else
                                    {$zk->close; print "close zk
    "; 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 "----------------
    ";
        print  $created_path;
        print "----------------
    ";
        ###获取数据
        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'}."
    ";
            print "事件状态.";
            print $event->{'state'}."
    ";
            $cv->send($event) });
            print "------------------
    ";
            print $zk_value;
            print "
    ";
            ###检测3306端口
        sub check_port {
                            ( $server, $port ) = ('127.0.0.1','3306');
                                  $sock = IO::Socket::INET->new(PeerAddr => $server,
                                  PeerPort => $port,
                                  Proto => 'tcp');
                                  print "$sock
    ";
                                 if ($sock)
                                    {return 1}
                                 else
                                    {$zk->close; print "close zk
    "; 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."
    ";
                 }
             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
    " );
             };
            print "@arr2 is @arr2
    ";
            $dbh->disconnect;
            };
    
    
    

  • 相关阅读:
    ASP.NET MVC与RAILS3的比较
    ASP.NET状态管理详解,让你明明白白
    Javascript在页面加载时的执行顺序【转】
    ASP.NET登录控件延伸(个性化)
    ASP.NET中读取excel内容并显示
    javascript 最常用的技巧整理
    ASP.NET用户控件事件的定义和实践
    百度 WebUploader 分片上传
    前端 WebUploader 分片上传
    vue WebUploader 分片上传
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350502.html
Copyright © 2011-2022 走看看