STEP1) レプリケーション用ユーザの作成 ━ セグメントを指定してReplicationユーザーを作成
mysql> GRANT REPLICATION SLAVE ON *.* TO replication@’192.168.11.0/255.255.255.0′ IDENTIFIED BY ‘rePlicaTion’;
Query OK, 0 rows affected (0.00 sec)
mysql> use mysql
Database changed
mysql> select user,host,password from user where user = ‘replication’;
+————-+—————————-+——————————————-+
| user | host | password |
+————-+—————————-+——————————————-+
| replication | 192.168.11.0/255.255.255.0 | *B3484EFDD867D04EDCF7CBA08D9554BA5E8FE684 |
+————-+—————————-+——————————————-+
1 row in set (0.01 sec)
STEP2) マスタのデータをスレーブにコピー ━ 整合性を保つ為にまずはMY SQLサービスを停止
[root@desktop ~]# /etc/init.d/mysql.server stop
Shutting down MySQL. [ OK ]
↓
停止後にtarでまとめる, tar cpf /var/tmp/mysql.tar /usr/local/mysql
↓
[root@desktop data]# scp For_repli.tar admin@192.168.11.4:/home/admin/For_repli.tar
admin@192.168.11.4’s password:
For_repli.tar 100% 20KB 20.0KB/s 00:00
[root@desktop data]#
3) Slaveのデータを入れ替える (Slave停止)
[root@notepc local]# /etc/init.d/mysql.server stop
↓
※マスターで取得してスレーブにコピーしたファイルを,/usr/local/mysqlに展開
4) マスタの設定
バイナリログとserver-idの設定のため, マスタの/etc/my.cnfに追加します。
[mysqld]
log-bin
server-id=1
5) マスター設定確認
[root@desktop local]# /etc/init.d/mysql.server start
Starting MySQL.. [ OK ]
[root@desktop data]# ls -l
合計 28820
drwx—— 2 mysql mysql 4096 5月 24 14:44 RPLI
-rw-rw—- 1 mysql mysql 117 5月 24 15:18 desktop-bin.000001
-rw-rw—- 1 mysql mysql 98 5月 24 15:38 desktop-bin.000002
-rw-rw—- 1 mysql mysql 82 5月 24 15:38 desktop-bin.index
-rw-rw—- 1 mysql mysql 5 5月 24 15:38 desktop.localdomain.pid
-rw-rw—- 1 mysql mysql 5242880 5月 24 15:38 ib_logfile0
-rw-rw—- 1 mysql mysql 5242880 5月 11 00:20 ib_logfile1
-rw-rw—- 1 mysql mysql 18874368 5月 24 15:18 ibdata1
drwxr-x— 2 mysql root 4096 5月 11 00:38 mysql
mysql> show master status;
+——————–+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————–+———-+————–+——————+
| desktop-bin.000001 | 98 | | |
+——————–+———-+————–+——————+
1 row in set (0.00 sec)
mysql>
mysql> SHOW VARIABLES LIKE ‘server\_id’;
+—————+——-+
| Variable_name | Value |
+—————+——-+
| server_id | 1 |
+—————+——-+
1 row in set (0.02 sec)
mysql>
━━━━━━━ ここでマスターの設定は終了 ━━━━━━━━━
6) slaveのmy.cnf変更してmysqlを開始
########## Replication Slave設定 #############
server-id=2
master-host=192.168.11.5
master-user=replication
master-password=rePlicaTion
[root@notepc local]# /etc/init.d/mysql.server start
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.11.5
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: desktop-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: notepc-relay-bin.000002
Relay_Log_Pos: 237
Relay_Master_Log_File: desktop-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: 98
Relay_Log_Space: 237
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
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
7) マスターのデータ更新をしたら,SLAVEにデータが反映されるか確認。
insert into name_book(firstname,lastname,comment) values(‘鈴木’,’啓太郎’,’レプリケーション確認’);
insert into name_book(firstname,lastname,comment) values(‘斉藤’,’あどみん’,’レプリケーション確認済み’);
mysql> select * from name_book;
+———–+————–+————————————–+
| firstname | lastname | comment |
+———–+————–+————————————–+
| 山田 | 太郎 | HELLO WORLD |
| 鈴木 | 啓太郎 | レプリケーション確認 |
| 斉藤 | あどみん | レプリケーション確認済み |
+———–+————–+————————————–+
3 rows in set (0.00 sec)
━━━━━━━━━━━━ 青(MASTER) / 黒(SLAVE) ━━━━━━━━━━━━━━
■データを新規でインサートする前の状態を確認。(既存のデータ1件はコピーが問題なくされている)
■ MasterにてデータのINSERTしてSlaveにてデータの同期確認。
■再度確認して完了。