作成中/MySQLレプリケーション

Last-modified: 2011-03-24 (木) 11:16:11
最終更新:2011-03-24 (木) 11:16:11
アクセス数(合計):?
利用者; ? アクセス数(本日):? アクセス数(昨日):?

作成中。完了までしばらくお待ちください

データベース同期 MySQLレプリケーション

サーバ障害に備え複数サーバ間でMySQLデータベースの同期を行い、サーバ二重化を実施する。


MySQLでは、複数サーバ間のデータベースを同期する仕組みを有する。同仕組みを利用し、サーバの同期運用を行いサーバ傷害時もスムーズに切り替えられるよう構築を行う。


目的

マスターサーバからスレーブサーバにファイルを転送し、マスターサーバ障害に備える。
コピー元(マスターサーバ)でrsyncを動作させ、コピー先(スレーブサーバ)にファイルを送信する。

構築条件

構築機器

作成環境

OS    CentOS 5.5 64bit版

サーバ作成条件

マスター側サーバサーバIP192.168.0.10/24
ホスト名masterServer
user   hogehoge
userpasswdhogehogepass
バックアップ元/var
スレーブ側サーバサーバIP192.168.0.20/24
ホスト名slaveServer
user   fugafuga
userpasswdfugafugapass
バックアップ先/home/fugafuga/backup

手順記入ルール

  • コマンド行頭記号で 「$」はユーザ権限 「#」はスーパーユーザー権限 を表示
  • コマンドが長くなる場合は、行末に「\」を記述し複数行に記述。

前準備

  1. CentOSインストール 実施
  2. yumリポジトリ設定 実施
  3. LAMPサーバ構築 実施

マスター側構築

【Telnet】

  • rootでログイン
$ su
パスワード:

(1)my.cnf編集

#vi /etc/my.cnf
  • 最終行に追加
    [mysqld]
    server-id=1
    log-bin

(2)MySQL再起動

# /etc/rc.d/init.d/mysqld restart
MySQL を停止中:                                            [  OK  ]
MySQL を起動中:                                            [  OK  ]

(3)MySQLスレーブアクセス用ユーザ作成

  • MySQL起動
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  • Slaveからのアクセスユーザ登録
mysql> GRANT REPLICATION SLAVE ON *.* TO replication@'%' IDENTIFIED BY "replicationpass";
Query OK, 0 rows affected (0.40 sec)

★どのSlaveサーバからのアクセスも許容で設定

(3)MySQLテーブル固定

  • DBロック設定
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

(4)MySQLテーブル確認

mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| mysqld-bin.000001 |       98 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'server\_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+
1 row in set (0.35 sec)
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
            File: mysqld-bin.000001
        Position: 98
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

(4)後処理

  • DBアンロック設定
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
  • 終了
mysql> exit
Bye

(5)確認

# ls -l /var/lib/mysql/*-bin.*
-rw-rw---- 1 mysql mysql 98  3月 25 09:21 /var/lib/mysql/mysqld-bin.000001
-rw-rw---- 1 mysql mysql 20  3月 25 09:21 /var/lib/mysql/mysqld-bin.index

スレーブ側構築

【Telnet】

  • rootでログイン
$ su
パスワード:

(1)my.cnf編集

# vi /etc/my.cnf
  • 最終行に以下を追加
    [mysqld]
    server-id=2
    master_host=192.168.0.11
    master_user="replication"
    master_password="replicationpass"
    master_port=3306

(2)MySQL再起動

# /etc/rc.d/init.d/mysqld restart
MySQL を停止中:                                            [  OK  ]
MySQL を起動中:                                            [  OK  ]

(3)確認

mysql> show slave status\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.0.10
                Master_User: replication
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysqld-bin.000001
        Read_Master_Log_Pos: 98
             Relay_Log_File: mysqld-relay-bin.000004
              Relay_Log_Pos: 236
      Relay_Master_Log_File: mysqld-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: 236
            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 starting client-server protocol (code 5)」が発生

問題:
以下コマンドでエラーとなる

@ERROR: access denied to backup from hogehogeserver (192.168.0.10)
rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=2.6.8]

原因:
転送元 rsyncd.conf の hosts allow = の設定値が転送先サーバになってない

謝辞

テキスト作成にあたり、以下サイトを参考にさせて頂きました。ありがとうございます。

http://arena.hyogo-dai.ac.jp/~kawano/?Install%20Log%2FCentOS5%2FMySQL_repl
http://kozy.heteml.jp/pukiwiki/MySQL%2520%25A5%25EC%25A5%25D7%25A5%25EA%25A5%25B1%25A1%25BC%25A5%25B7%25A5%25E7%25A5%25F3/index.html
http://phpspot.net/php/pg%83%8C%83v%83%8A%83P%81%5B%83V%83%87%83%93%90%DD%92%E8.html
http://www.maruko2.com/mw/MySQL_%E3%83%AC%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%AE%E8%A8%AD%E5%AE%9A
http://www.irori.org/doc/mysql-rep.html