1. MySQLレプリケーション双方向
Master
Slave
2. Masterで設定
2.1. MySQLに接続
# mysql --user=ユーザ名 --password=パスワード DB名
2.2. 権限追加
mysql>grant replication slave on *.* to 'repl'@'172.16.80.%' identified by 'hogehoge';
mysql>quit;
2.3. my.cnfの設定
#vi /etc/my.cnf
[mysqld]部に2行追加
log-bin=mysql-bin
server-id=1
2.4. 再起動
#service mysqld restart
3. Slaveで設定
3.1. MySQLに接続
# mysql --user=ユーザ名 --password=パスワード DB名
3.2. 権限追加
mysql>grant replication slave on *.* to 'repl'@'172.16.80.%' identified by 'hogehoge';
mysql>quit;
3.3. my.cnfの設定
#vi /etc/my.cnf
[mysqld]部に2行追加
log-bin=mysql-bin
server-id=2
3.4. 再起動
#service mysqld restart
4. ステータスの確認
4.1. master
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | |
+------------------+----------+--------------+------------------+
4.2. Slave
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 106 | | |
+------------------+----------+--------------+------------------+
5. 設定(順番重要)
4.1. Slaveで設定(MasterのIPアドレス,MasterのLOG_FILE,MasterのPosition)
mysql> CHANGE MASTER TO MASTER_HOST='172.16.80.20',
MASTER_USER='repl',
MASTER_PASSWORD='oracle',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=106;
4.2. Masterで設定(SlaveのIPアドレス,MasterのLOG_FILE,MasterのPosition)
mysql> CHANGE MASTER TO MASTER_HOST='172.16.80.21',
MASTER_USER='repl',
MASTER_PASSWORD='oracle',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=106;
4.3. Slaveで保存
mysql> START SLAVE;
4.4. Masterで保存
mysql> START SLAVE;
6. 確認
6.1. Masterで確認
mysql> show Master status\G
この項目がYESだったらほぼOK
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
6.2. Slaveで確認
mysql> show slave status\G
この項目がYESだったらほぼOK
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
7. テスト
7.1. Masterでテスト
mysql> create database rep;
mysql> use rep;
mysql> create table test(aaa int,bbb varchar(10));
mysql> insert into test values ('a',1);
mysql> insert into test values ('b',2);
mysql> select * from test;
7.2. Slaveでテスト
mysql> create database rep;
mysql> use rep;
mysql> create table test(aaa int,bbb varchar(10));
mysql> insert into test values ('a',1);
mysql> insert into test values ('b',2);
mysql> select * from test;
7.3. Masterで確認
mysql> select * from test;