博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql zk切换整个过程
阅读量:5167 次
发布时间:2019-06-13

本文共 8375 字,大约阅读时间需要 27 分钟。

mysql master:test:/root/zk# cat zk.pluse ZooKeeper;use AnyEvent;use AE;use Data::Dumper;use IO::Socket;   my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181, 120.55.118.6:2182, 120.55.118.6:2183');   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;        print Dumper($cv);       my $child_event = $cv->recv;test:/root/zk# mysql slave:slave:/root/zk# cat zk.pluse ZooKeeper;use AnyEvent;use AE;use Data::Dumper;use IO::Socket;   my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181,120.55.118.6:2182,120.55.118.6:2183');   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;	  	  	测试脚本:zjtest7-redis:/root/zk# cat test_zk.pl use ZooKeeper;use AnyEvent;use AE;use Data::Dumper;use IO::Socket;     sub check_port {                        ( $server, $port ) = ('127.0.0.1','3306');                              $sock = IO::Socket::INET->new(PeerAddr => $server,                              PeerPort => $port,                              Proto => 'tcp');                             if ($sock)                                {return 1}                             else                                {return 0 };                   };     my @ip_list=();   my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181,120.55.118.6:2182,120.55.118.6:2183') ;   print Dumper($zk);   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');            print $mysql_ip."\n";             };       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;        };第一步测试mysql 高可用;zjtest7-redis:/root/zk# perl test_zk.pl $VAR1 = bless( {                 'default_acl' => [                                    {                                      'id' => 'anyone',                                      'perms' => 31,                                      'scheme' => 'world'                                    }                                  ],                 'hosts' => '120.55.118.6:2181,120.55.118.6:2182,120.55.118.6:2183',                 'timeout' => 10000,                 'dispatcher' => bless( {                                          'ae_watcher' => bless( do{\(my $o = '¸¢Yʂ¿X秂9')}, 'EV::IO' ),                                          'watchers' => {},                                          'ignore_session_events' => 1,                                          'dispatch_cb' => sub { "DUMMY" },                                          'channel' => bless( {}, 'ZooKeeper::Channel' )                                        }, 'ZooKeeper::Dispatcher::AnyEvent' ),                 'buffer_length' => 2048               }, 'ZooKeeper' );192.168.32.6@arr2 is 1  aaabbb   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe   2  cccddeqe 此时连接的是192.168.32.6节点:观察zk 节点信息:[zk: 127.0.0.1(CONNECTED) 85] ls /mysql[0002, 0001] 关闭32.6的mysql   此时32.6上的zk : IO::Socket::INET=GLOB(0x220a300)IO::Socket::INET=GLOB(0x22ed950)IO::Socket::INET=GLOB(0x203ea60)IO::Socket::INET=GLOB(0x2131bd0)IO::Socket::INET=GLOB(0x215fc88)close zkYou have mail in /var/spool/mail/root观察zk信息:[zk: 127.0.0.1(CONNECTED) 90] ls /mysql[0002]zjtest7-redis:/root/zk# perl test_zk.pl $VAR1 = bless( {                 'default_acl' => [                                    {                                      'id' => 'anyone',                                      'scheme' => 'world',                                      'perms' => 31                                    }                                  ],                 'buffer_length' => 2048,                 'hosts' => '120.55.118.6:2181,120.55.118.6:2182,120.55.118.6:2183',                 'timeout' => 10000,                 'dispatcher' => bless( {                                          'channel' => bless( {}, 'ZooKeeper::Channel' ),                                          'watchers' => {},                                          'ae_watcher' => bless( do{\(my $o = 'Һ࡯¹|%{')}, 'EV::IO' ),                                          'dispatch_cb' => sub { "DUMMY" },                                          'ignore_session_events' => 1                                        }, 'ZooKeeper::Dispatcher::AnyEvent' )               }, 'ZooKeeper' );192.168.32.116@arr2 is 99  dadsgdf243  已切换;
 

转载于:https://www.cnblogs.com/zhaoyangjian724/p/6199367.html

你可能感兴趣的文章
nginx 的提升多个小文件访问的性能模块
查看>>
set&map
查看>>
集合类总结
查看>>
4.AE中的缩放,书签
查看>>
给一次重新选择的机会_您还会选择程序员吗?
查看>>
Mysql MHA高可用集群架构
查看>>
心急的C小加
查看>>
编译原理 First,Follow,select集求法
查看>>
iOS开发 runtime实现原理以及实际开发中的应用
查看>>
android 学习资源网址
查看>>
qt安装遇到的错误
查看>>
java:Apache Shiro 权限管理
查看>>
objective c的注释规范
查看>>
FreeNas安装配置使用
查看>>
Django(一)框架简介
查看>>
Python操作SQLite数据库的方法详解
查看>>
菜单和工具条(二)
查看>>
hadoop17---RPC和Socket的区别
查看>>
使用JMeter代理录制app测试脚本
查看>>
Linq to Object实现分页获取数据
查看>>