LoginSignup
3
3

More than 5 years have passed since last update.

mysqlレプリケーション設定

Last updated at Posted at 2016-03-07

前提

activeのデータをslaveにデータを丸ごとコピーし、レプリケーションの設定をする。

activeのmysqlを止める事が可能であり、且つslaveはデータが何も入っていない状態。エンジンはinnoDBとする。
環境情報は以下のとおり。

種別 設定値 備考
データベース testdb コピー&レプリケーション対象
テーブル maintable 動作確認用
種別 サーバー名 IP 備考
オリジナル active 192.168.0.1 コピー元のサーバ
スレーブ slave 192.168.0.2 空の状態
種別 設定値
レプリケーションユーザ repli
レプリケーションパスワード pass

前準備(データ転送)

activeの MySQL停止し、/data/mysql 以下をslave にrsync

## MySQLサーバを停止
ps -ef | grep -i mysql
/etc/init.d/mysql stop
ps -ef | grep -i mysql

対象のデータベースをディレクトリごと転送

rsync -avz /data/mysql/testdb 192.168.0.2:/data/mysql/.

activeの MySQL起動し、ポジション確認

# mysqlを起動
/etc/init.d/mysql start
mysql -u root -p

# ポジション確認
mysql> show master status\G
*************************** 1. row ***************************
            File: mysqld-bin.001234★ひかえる
        Position: 123456789★ひかえる
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

mysql>


#各サーバで以下のコマンドを実行し、レコード数をカウントする。復旧時、件数確認で使用する。

use testdb;
select count(*) from maintable;

mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select count(*) from maintable;
+----------+
| count(*) |
+----------+
| 1234     |
+----------+
1 row in set (0.93 sec)

mysql>


# レプリケーション接続許可設定
# この設定をすることにより、slaveでレプリケーションの設定が可能になる。
mysql> grant file,replication slave on *.* to repli@'192.168.0.2' identified by 'pass';

slaveレプリ開始

# レプリケーションが停止していることを確認。
mysql -uroot
reset slave;

# レプリ再開で、ポジションが進み始めることを確認
mysql> change master to master_host='192.168.0.1', 
        master_port=3306,master_user='repli',
        master_password='repl',
        master_log_file='mysqld-bin.001234',★ひかえた値を設定
        master_log_pos = 123456789;★ひかえた値を設定

# レプリケーション再開
start slave;

# activeに更新があった場合、ポジションが進むことを確認
show slave status\G

事後確認

各サーバで以下のコマンドを実行し、レコード数をカウントする。

mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select count(*) from maintable;
+----------+
| count(*) |
+----------+
| 1234     |
+----------+
1 row in set (0.93 sec)

mysql>
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3