日向夏特殊応援部隊

俺様向けメモ

MySQL::Sandbox (2)

MySQL::Sandbox - Yet Another Hackadelic の続きです。

make_replication_sandbox でレプリ環境をサクっと作る

レプリ環境もサクっと作れます。前と同じバイナリがあるとしたら、

$ make_replication_sandbox /home/zigorou/tmp/mysql-5.0.82-linux-i686-glibc23.tar.gz
executing "clear" on slave 1
executing "clear" on slave 2
executing "clear" on master
installing and starting master
installing slave 1
installing slave 2
starting slave 1
. sandbox server started
starting slave 2
.. sandbox server started
initializing slave 1
initializing slave 2
replication directory installed on /home/zigorou/sandboxes/rsandbox_5_0_82

んでもって、

$ tree -L 2 /home/zigorou/sandboxes/rsandbox_5_0_82/
/home/zigorou/sandboxes/rsandbox_5_0_82/
|-- check_slaves
|-- clear_all
|-- initialize_slaves
|-- m
|-- master
|   |-- USING
|   |-- change_paths
|   |-- change_ports
|   |-- clear
|   |-- data
|   |-- grants.mysql
|   |-- load_grants
|   |-- my
|   |-- my.sandbox.cnf
|   |-- proxy_start
|   |-- restart
|   |-- send_kill
|   |-- start
|   |-- stop
|   `-- use
|-- node1
|   |-- USING
|   |-- change_paths
|   |-- change_ports
|   |-- clear
|   |-- data
|   |-- grants.mysql
|   |-- load_grants
|   |-- my
|   |-- my.sandbox.cnf
|   |-- proxy_start
|   |-- restart
|   |-- send_kill
|   |-- start
|   |-- stop
|   `-- use
|-- node2
|   |-- USING
|   |-- change_paths
|   |-- change_ports
|   |-- clear
|   |-- data
|   |-- grants.mysql
|   |-- load_grants
|   |-- my
|   |-- my.sandbox.cnf
|   |-- proxy_start
|   |-- restart
|   |-- send_kill
|   |-- start
|   |-- stop
|   `-- use
|-- restart_all
|-- s1
|-- s2
|-- send_kill_all
|-- start_all
|-- stop_all
`-- use_all

m, s1, s2 はそれぞれ、master/use, node1/use, node2/use のショートカットって感じ。また *_all はノード全体に対する操作です。

make_multi_sandbox で自由に Sandbox を作る

make_sandbox, make_replication_sandbox ってバージョン固定でそのインスタンス一個しか立ち上げられないんですよね。

一方で、同じバージョンでも複数のインスタンスを立ち上げたいってシーンもあります。その時に make_multiple_sandbox を使います。

make_multiple_sandbox -r multi_msb_3 --how_many_nodes=3 /home/zigorou/tmp/mysql-5.0.82-linux-i686-glibc23.tar.gz

-r で名前を付けられます。multi_msb_3 ってのが $HOME/sandboxes/multi_msb3 に出来ます。ノード数は3つで。

$ tree -L 1 /home/zigorou/sandboxes/multi_msb_3/
/home/zigorou/sandboxes/multi_msb_3/
|-- check_slaves
|-- clear_all
|-- n1
|-- n2
|-- n3
|-- node1
|-- node2
|-- node3
|-- restart_all
|-- send_kill_all
|-- start_all
|-- stop_all
`-- use_all

まぁ、replication で作った場合と似てるんですけども。

この構成の時はレプリケーションは全然出来てません。node1 を master にするとして、実際にレプリ環境を作ってみましょう。

$ cat ~/sandboxes/multi_msb_3/node1/my.sandbox.cnf
#    The MySQL Sandbox
#    Copyright (C) 2006,2007,2008,2009 Giuseppe Maxia
#    Contacts: http://datacharmer.org
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; version 2 of the License
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

[mysql]
prompt='node1 [\h] {\u} (\d) > '
#

[client]
user            = msandbox
password        = msandbox
port            = 20283
socket          = /tmp/mysql_sandbox20283.sock

[mysqld]
user                            = zigorou
port                            = 20283
socket                          = /tmp/mysql_sandbox20283.sock
basedir                         = /home/zigorou/tmp/5.0.82
datadir                         = /home/zigorou/sandboxes/multi_msb_3/node1/data
pid-file                        = /home/zigorou/sandboxes/multi_msb_3/node1/data/mysql_sandbox20283.pid
#log-slow-queries               = /home/zigorou/sandboxes/multi_msb_3/node1/data/msandbox-slow.log
#log                            = /home/zigorou/sandboxes/multi_msb_3/node1/data/msandbox.log
#
# additional options passed through 'my_clause'
#
server-id=101
log-bin=mysql-bin
log-error=msandbox.err

という事をまず確認。ついでに、

$ ~/sandboxes/multi_msb_3/node1/use mysql -e "SELECT Repl_slave_priv FROM user WHERE User = 'msandbox';"
+-----------------+
| Repl_slave_priv |
+-----------------+
| Y               |
+-----------------+

つー訳でレプリユーザの準備も出来てる。

node2, node3 で同じ作業をするんですが、

  • CHANGE MASTER TO
  • START SLAVE
  • SHOW SLAVE STATUS

で確認すればおkですね。

$ ~/sandboxes/multi_msb_3/node2/use -e "CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_USER='msandbox', MASTER_PASSWORD='msandbox', MASTER_PORT=20283;"
$ ~/sandboxes/multi_msb_3/node2/use -e "START SLAVE;"
$ ~/sandboxes/multi_msb_3/node2/use -e "SHOW SLAVE STATUS\G;"
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 127.0.0.1
                Master_User: msandbox
                Master_Port: 20283
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 628
             Relay_Log_File: mysql_sandbox20284-relay-bin.000002
              Relay_Log_Pos: 765
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB:
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 0
                 Last_Error:
               Skip_Counter: 0
        Exec_Master_Log_Pos: 628
            Relay_Log_Space: 765
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: 0

ってな感じですね。

sbtool

MySQL Sandbox の管理ツールです。

sbtool -o info

Perlデータ形式で、存在するサンドボックスに関するデータを返してくれます。
まぁ実行すれば分かる。*1

sbtool -o ports

使用されているポート一覧とアクティブかどうか。

sbtool -o delete

こんな感じでサンドボックスを消します。

$ sbtool -o delete --source_dir ~/sandboxes/rsandbox_5_0_82/
executing "stop" on slave 1
executing "stop" on slave 2
executing "stop" on master
executing "clear" on slave 1
executing "clear" on slave 2
executing "clear" on master
sandbox at </home/zigorou/sandboxes/rsandbox_5_0_82/> has been removed

なるほどねー。

まとめ

物凄い駆け足で MySQL Sandbox について二回に渡って紹介してみましたよ。SQLite に比べたらそこまでお手軽では無い物の、気軽に環境が作れるのと好きな構成でレプリ環境も思い通りに作れます。また削除もコマンド一発で出来るので MySQL を使った単体テストなんかを行うにはいいんじゃないでしょうかねー。

*1:長くなっちゃったのでコピペ自粛