Code Monkey home page Code Monkey logo

ruby-binlog's Introduction

NOTE

This is a fork (converted to git) from https://bitbucket.org/winebarrel/ruby-binlog

Description

ruby-binlog is Ruby binding for MySQL Binary log API.

Install

gem install ruby-binlog

Required Privileges

  • SUPER
  • REPLICATION SLAVE
  • EVENT

Example

  #!/usr/bin/env ruby
  require "rubygems"
  require "binlog"
  require "pstore"
  
  $db = PStore.new("/tmp/foo")
  
  #master_log_file = "mysql-bin.000001"
  #master_log_pos = 4
  
  master_log_file = nil
  master_log_pos = nil
  
  $db.transaction do
    master_log_file = $db["master_log_file"]
    master_log_pos = $db["master_log_pos"]
  end
  
  def save_position(master_log_file, master_log_pos)
    $db.transaction do
      $db["master_log_file"] = master_log_file
      $db["master_log_pos"] = master_log_pos
    end
  end
  
  begin
    # XXX: Do not reuse a client instance, after connection goes out.
    client = Binlog::Client.new("mysql://repl:[email protected]")
    sleep 0.3 until client.connect
  
    if master_log_file and master_log_pos
      client.set_position(master_log_file, master_log_pos)
    elsif master_log_pos
      client.position = master_log_pos
    end
  
    while event = client.wait_for_next_event
      puts "(#{event.event_type})"
      master_log_pos = event.next_position
  
      case event
      when Binlog::QueryEvent
        puts event.db_name
        puts event.query
        save_position(master_log_file, master_log_pos)
      when Binlog::RowEvent
        puts event.event_type
        puts event.db_name
        puts event.table_name
        p event.columns
        p event.rows
        save_position(master_log_file, master_log_pos)
      when Binlog::RotateEvent
        master_log_file = event.binlog_file
        master_log_pos = event.binlog_pos
        save_position(master_log_file, master_log_pos)
      end
    end
  rescue Binlog::Error => e
    puts e
    retry if client.closed?
    raise e
  end

Notice

The following type are not supported in row mode.

  • ENUM
  • SET
  • GEOMETRY

ruby-binlog's People

Contributors

simonkro avatar daedric avatar brunograsselli avatar rosic avatar carlosmoutinho avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.