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;
            };
    
    
    

  • 相关阅读:
    .NET 4.0 System.Threading.Tasks学习笔记
    c#初学-多线程中lock用法的经典实例
    Spring3 Schedule Task之注解实现 (两次起动Schedule Task 的解决方案)
    支持向量机中的函数距离的理解
    Sqlserver 2016 R Service环境安装的各种错误(坑)解决办法
    Sql server的Merge语句,源表中如果有重复数据会导致执行报错
    对IIS7经典模式和集成模式的理解(转载)
    VisualStudio编译项目时,提示bin目录和obj目录下的文件不能写的错误处理的解决办法
    SQL Server 全文索引介绍(转载)
    sql server全文索引使用中的小坑 (转载)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350502.html
Copyright © 2011-2022 走看看