Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

MySQLのBinlog APIを試してみる on Mac

MySQL Binlog APIを試してみる」で知ったMySQLのBinlog APIが面白そうだったので、Macで試してみた。

インストール

$ brew install bzr cmake boost
$ bzr branch lp:mysql-replication-listener
$ cd mysql-replication-listener
$ cmake .
$ make
$ sudo make install

exampleをビルドしてみる

$ cd examples
$ cmake .
$ make
Linking CXX executable basic-1
Undefined symbols for architecture x86_64:
  "boost::system::system_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in basic-1.o
      boost::asio::error::get_system_category()    in basic-1.o
  "boost::system::generic_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in basic-1.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[2]: *** [basic-1] Error 1
make[1]: *** [CMakeFile

すると、上記のようなエラーが出たので、examples/CMakeLists.txtの以下の箇所に、boost_system-mtを追加する

TARGET_LINK_LIBRARIES(${prog} replication boost_system-mt)

んでもってもっかいmakeすると、うまくいったっぽい。

$ make
[ 50%] Building CXX object CMakeFiles/basic-1.dir/basic-1.o
Linking CXX executable basic-1
[ 50%] Built target basic-1
[100%] Building CXX object CMakeFiles/basic-2.dir/basic-2.o
Linking CXX executable basic-2
[100%] Built target basic-2

MySQL::Sandboxで実験

$ cpanm MySQL::Sandbox
$ make_replication_sandbox ~/Downloads/mysql-5.5.25a-osx10.6-x86_64.tar.gz
$ cat $HOME/sandboxes/rsandbox_mysql-5_5_25/master/my.sandbox.cnf
# 省略

[client]
user            = msandbox
password        = msandbox
port            = 21580

# 省略

上記で確認したuser/password/portで接続してみましょう。

$ ./basic-2 'mysql://msandbox:msandbox@127.0.0.1:21580'
SET PASSWORD FOR 'root''''localhost'='*6C387FC3893DBA1E3BA155E74754DA6682D04747'
grant all on *.* to msandbox'''127.%' identified by 'msandbox'
grant all on *.* to msandbox'''localhost' identified by 'msandbox'
grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER,
    SHOW DATABASES,CREATE TEMPORARY TABLES,LOCK TABLES, EXECUTE 
    on *.* to msandbox_rw'''localhost' identified by 'msandbox'
grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER,
    SHOW DATABASES,CREATE TEMPORARY TABLES,LOCK TABLES, EXECUTE 
    on *.* to msandbox_rw'''127.%' identified by 'msandbox'
grant SELECT,EXECUTE on *.* to msandbox_ro'''127.%' identified by 'msandbox'
grant SELECT,EXECUTE on *.* to msandbox_ro'''localhost' identified by 'msandbox'
grant REPLICATION SLAVE on *.* to rsandbox'''127.%' identified by 'rsandbox'
BEGIN
delete from user where password=''
COMMIT
BEGIN
delete from db where user=''
COMMIT
flush privileges

わお! なんか出てきましたね。別の端末からmysqlのコンソールに入ってみます。んでもって適当にあれこれしてみると……。

$ $HOME/sandboxes/rsandbox_mysql-5_5_25/master/use
master [localhost] {msandbox} (test) > create table t1 (id int unsigned not null auto_increment, value varchar(255), primary key (id)) engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

master [localhost] {msandbox} (test) > begin;
Query OK, 0 rows affected (0.00 sec)

master [localhost] {msandbox} (test) > insert into t1 (value) values ("aaaa");
Query OK, 1 row affected (0.00 sec)

master [localhost] {msandbox} (test) > insert into t1 (value) values ("bbbb");
Query OK, 1 row affected (0.00 sec)

master [localhost] {msandbox} (test) > commit;
Query OK, 0 rows affected (0.00 sec)

出た〜〜〜!!1 すごい!!1

create table t1 (id int unsigned not null auto_increment, value varchar(255), primary key (id)) engine=InnoDB
BEGIN
insert into t1 (value) values ("aaaa")
insert into t1 (value) values ("bbbb")

まとめ

  • ライブラリまわりが怪しいけど、一応動いた
  • Binlog APIヤバい感じ
  • もうちょっといじってみよう