Mac + VirtualBox + Vagrant で MySQLのレプリケーション環境構築
MySQLのレプリケーションを手軽に試す環境が欲しいなぁと思い、Vagrant を使って複数のGuest OSを立ち上げてみることにした。
Environments
Host
- OS: 10.10.3(Yosemite)
- VirtualBox: 4.3.26
- Vagrant: 1.7.2
Guest
マスター・スレーブ 1台ずつで。
Installation
VirtualBox と Vagrant
それぞれ、Downloads – Oracle VM VirtualBox と Download - Vagrant by HashiCorp から ダウンロードしてインストールすれば OK。
Guest OS
Guest OS は 今回は Ubuntu を使ってみることにする。適当な box を拾ってきてもいいが、せっかくなので Veewee で 一から作成する。
Ryuzeeさんの記事を参考にさせてもらいました。 www.ryuzee.com
veeweeをインストールする。
mkdir sandbox && git clone https://github.com/jedi4ever/veewee.git cd veewee bundle install
ISOイメージをダウンロードし、boxをbuildする。
bundle exec veewee vbox templates | grep ubuntu veewee vbox define 'ubuntu-14.04-server-amd64' 'ubuntu-14.04-server-amd64' --workdir=/Users/kotaroito/sandbox/veewee curl -LO http://releases.ubuntu.com/14.04/ubuntu-14.04.2-server-amd64.iso mv ubuntu-14.04.2-server-amd64.iso iso/ veewee vbox build 'ubuntu-14.04-server-amd64'
boxをexportし、vagrantに追加する。
veewee vbox export 'ubuntu-14.04-server-amd64' vagrant box add 'ubuntu-14.04-server-amd64' '/Users/kotaroito/sandbox/veewee/ubuntu-14.04-server-amd64.box'
Configuration
Vagrant
blog.hello-world.jp.net を参考にさせてもらった。
master と slave としてそれぞれ1つずつOSを立ち上げる。 ネットワーク設定は vagrantのネットワークについて - Qiita が分かりやすい。
Vagrant.configure(2) do |config| config.vm.box = "ubuntu-14.04-server-amd64" config.vm.define :db_master do |node| node.vm.network "private_network", ip: "192.168.33.10" end config.vm.define :db_slave do |node| node.vm.network "private_network", ip: "192.168.33.11" end
かんたんにsshできるように、.ssh/config に登録しておく。
vagrant ssh-config db_master --host vagrantdbm >> ~/.ssh/config vagrant ssh-config db_slave --host vagrantdbs >> ~/.ssh/config
Guest OS(ここでは db_master と db_slave) を立ち上げる。
vagrant up
ssh vagrantdbm して、db_master に接続して、ネットワークをチェックする。
$ ifconfig eth1 Link encap:Ethernet HWaddr 08:00:27:c3:5a:0e inet addr:192.168.33.10 Bcast:192.168.33.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fec3:5a0e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:153 errors:0 dropped:0 overruns:0 frame:0 TX packets:106 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:21300 (21.3 KB) TX bytes:19440 (19.4 KB) $ ping 192.168.33.11 PING 192.168.33.11 (192.168.33.11) 56(84) bytes of data. 64 bytes from 192.168.33.11: icmp_seq=1 ttl=64 time=0.600 ms 64 bytes from 192.168.33.11: icmp_seq=2 ttl=64 time=0.485 ms
MySQL
同じく http://blog.hello-world.jp.net/centos/556/ が詳しい。Ubuntu での手順を記しておく。
Master DB
apt-get update apt-get install mysql-server
バイナリログを出力するよう設定する。
[mysqld] server-id = 1 log_bin bind-address = 0.0.0.0
レプリケーション権限を持つユーザ(repl)を設定する。
echo "GRANT REPLICATION SLAVE ON *.* TO repl@192.168.33.11 IDENTIFIED BY 'repl';" | mysql -uroot
my.cnfの設定を有効にするため、mysqld を再起動する。
service mysql restart
Slave DB
apt-get update apt-get install mysql-server
バイナリログを出力するよう設定する。
[mysqld] server-id = 2
my.cnfの設定を有効にするため、mysqld を再起動する。
service mysql restart
Replication
いよいよレプリケーションを試してみる。
まずは、マスターのポジションを確認する。 (クエリは自分以外には発行していない前提で)
mysql> SHOW MASTER STATUS \G *************************** 1. row *************************** File: mysqld-bin.000003 Position: 107 Binlog_Do_DB: Binlog_Ignore_DB:
次にスレーブでレプリケーションを設定する。
CHANGE MASTER TO MASTER_HOST='192.168.33.10', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_LOG_FILE='mysqld-bin.000003', MASTER_LOG_POS=107; START SLAVE;
レプリケーションの状態を確認する。
mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.33.10 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysqld-bin.000003 Read_Master_Log_Pos: 107 Relay_Log_File: mysqld-relay-bin.000004 Relay_Log_Pos: 254 Relay_Master_Log_File: mysqld-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB:
Slave_IO_Running / Slave_SQL_Running が Yes なら大丈夫。
ハマりポイント
バイナリーログのファイル名 当たり前だが、間違ってると動作しない。
bind-address デフォルトで 127.0.0.1 になってたりするので注意。
iptables 弾かれていないことを確認すべし。
確認
実際にレプリケーションされることを確認すべく、Master DB にデータを流し込む。 http://dev.mysql.com/doc/index-other.html の “world database” を使ってみる。
curl -LO http://downloads.mysql.com/docs/world.sql.gz gzip -d world.sql.gz mysql -uroot < world.sql
Slave DB で world データベースが見えれば、成功!
mysql> use world; Database changed mysql> show tables; +-----------------+ | Tables_in_world | +-----------------+ | City | | Country | | CountryLanguage | +-----------------+