mysqlマスター、半同期、マスターレプリケーション

15632 ワード

MySQLがサポートする2つのレプリケーションスキーム:文ベースのレプリケーション、行ベースのレプリケーション.
どちらのレプリケーションも、プライマリ・サーバのバイナリ・ログにデータベース内のデータが変更される可能性のあるSQL文を中継ログに記録し、サーバから次の中継ログ内のSQL文を実行することで、プライマリ・サーバのデータと同じステップに達します.異なる点は、プライマリ・サーバで変数ベースのデータが実行され、now()関数などのデータベースに更新され、文ベースのレプリケーションではSQL文の構文全体が記録され、行ベースのレプリケーションではnow()がデータベースに更新された数値が記録されます.
 
現在は通常行コピーで、MYSQL 5から.1.5行のコピー、行のコピーをサポートすることを開始して、ORACLEのコピーに類似して、データのブロックをコピーして、スピードの性能は速いです.
 
たとえば、プライマリ・サーバで次の文を実行します.
mysql>update user set createtime=now() where sid=16;
このときnow()が返す値が2012-04-16 20:46:35である場合
文ベースのレプリケーションは、update user set createtime=now()where sid=16として記録されます.
ロー・ベースのレプリケーションは、update user set createtime='2012-04-16 20:46:35'where sid=16として記録されます.
 
プライマリ・レプリケーションから開始する3つのスレッド:
Binlog dumpスレッド:バイナリ・ログの内容をスレーブ・サーバに送信
I/Oスレッドから:受信したデータを中継ログに書き込む
SQLスレッド:中継ログからSQL文を一度に読み出してサーバから実行
 
一、主従複製:
準備:
1.プロファイルの変更(server_idは必ず変更)
2.レプリケーション・ユーザーの作成
3.サーバからのサービス開始プロセス
 
計画:
Master:IPアドレス:172.16.4.11バージョン:mysql-5.5.2 0
Slave:IPアドレス:172.16.4.12バージョン:mysql-5.5.20
ここで、mysqlレプリケーションの大部分は後方互換性があるため、サーバのバージョンからプライマリサーバのバージョン以上である必要があります.
1、Master
プロファイルを変更してmysqlプライマリサーバに設定
#vim /etc/my.cnf
server_id=11                #  server_id=11
log_bin=mysql-bin            #       
sync_binlog=1               #                         
innodb_flush_logs_at_trx_commit=1       #                        
    
#service mysql reload             #    mysql     

2、Masterにユーザーを作成し、コピー権限を付与する
mysql>grant replication client,replication slave on *.* to [email protected] identified by '135246';
mysql>flush privileges;

3、Slave
プロファイルを変更してmysqlサーバから
#vim /etc/my.cnf
server_id=12                #  server_id=12
#log-bin                #   log-bin,            ,      
relay-log=mysql-relay                #       ,          
relay-log-index=mysql-relay.index     #         ,          
read_only=1                    #             ,       

保存終了
#service mysql reload#mysqlのプロファイルを再読み込み
 
4、Slaveの中継ログとserver_を検証するidが有効かどうか
mysql>show variables like 'relay%';
+-----------------------+-----------------+
| Variable_name         | Value           |
+-----------------------+-----------------+
| relay_log             | relay-bin       |
| relay_log_index       | relay-bin.index |
| relay_log_info_file   | relay-log.info  |
| relay_log_purge       | ON              |
| relay_log_recovery    | OFF             |
| relay_log_space_limit | 0               |
+-----------------------+-----------------+
mysql>show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 12    |
+---------------+-------+

 
5、サーバーからのサービスプロセスの起動
 
シーン1.プライマリ・サーバとセカンダリ・サーバの両方が新しく作成され、追加のデータが追加されていない場合は、次のコマンドを実行します.
mysql>change master to \
master_host='172.16.4.11',
master_user='repl',
master_password='135246';
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
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: 25520
Relay_Log_Space: 2565465
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
mysql>start slave;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-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: 360
Relay_Log_Space: 300
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11
   、              ,         ,                     :
Master:
#mysqldump -uroot -hlocalhost -p123456 --all-databases --lock-all-tables --flush-logs --master-data=2 > /backup/alldatabase.
mysql>flush tables with read lock;
mysql>show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      360 |              |                  |
+------------------+----------+--------------+------------------+
mysql>unlock tables;
#scp /backup/alldatabase.sql 172.16.4.12:/tmp
Slave:
#mysql -uroot -p123456 < /tmp/alldatabase.sql
mysql>change master to \
master_host='172.16.4.11',
master_user='repl',
master_password='135246',
master_log_file='mysql-bin.000004',
master_log_pos=360;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 360
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
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: 360
Relay_Log_Space: 107
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
mysql>start slave;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 360
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
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: 360
Relay_Log_Space: 300
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11

 
MySQLのマスターコピーアーキテクチャが成功したことを説明します
 
注意1:MySQLのレプリケーションは、データベースまたはライブラリ内の暗記表に基づいてレプリケーションできます.この機能を実装するには、プロファイルに次の構成を追加します.
Master:
binlog-do-db=db_nameはdb_のみコピーnameデータベース
binlog-ignore-db=db_nameはdb_をコピーしませんnameデータベース
 
注意2:Masterでフィルタルールを定義すると、データベースに関連しない書き込みはバイナリ・ログに記録されません.したがって、Masterでフィルタルールを定義することは推奨されません.binlog-do-dbとbinlog-ignore-dbを同時に定義することは推奨されません.
 
Slave:
replicate_do_db=db_name               db_name   
replicate_ignore_db=db_name           db_name   
replicate_do_table=tb_name           tb_name 
replicate_ignore_table=tb_name           tb_name 
replicate_wild_do_table=test%            test                  
replicate_wild_ignore_table=test_        test                    

注意3:複数のdbまたはtableを指定する必要がある場合は、コマンドを複数回書き込むだけです.
 
二、半同期レプリケーション
 
Mysqlのレプリケーションはすべて非同期に基づいて行われるため、特殊な場合、データのレプリケーションの成功は保証されないため、mysql 5.5以降はgoogleパッチを使用し、Mysqlのレプリケーションを半同期モードに実現することができる.そのため、メインサーバに対応するプラグインをロードする必要があります.Mysqlのインストールディレクトリのlib/plugin/ディレクトリに対応するプラグインsemisync_があります.master.so,semisync_slave.so
 
MasterとSlaveのmysqlコマンドラインで次のコマンドを実行します.
  
Master:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
mysql> set global rpl_semi_sync_master_enabled = 1;
mysql> set global rpl_semi_sync_master_timeout = 1000;
mysql> show variables like '%semi%';
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | ON    |
| rpl_semi_sync_master_timeout       | 1000  |
| rpl_semi_sync_master_trace_level   | 32    |
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+
Slave:
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
mysql> set global rpl_semi_sync_slave_enabled = 1;
mysql> stop slave;
mysql> start slave;
mysql> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
         :
Master:
mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+
       。

半同期機能をMySQLで起動するたびに自動的に有効にします.MasterとSlaveのmyです.cnfで編集:
Master:
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000     #1 
Slave:
[mysqld]
rpl_semi_sync_slave_enabled=1
                         :
Master:
mysql> set global rpl_semi_sync_master_enabled=1
      
mysql> uninstall plugin rpl_semi_sync_master;
Slave:
mysql> set global rpl_semi_sync_slave_enabled = 1;
mysql> uninstall plugin rpl_semi_sync_slave;

 
 
三、主主なレプリケーションアーキテクチャ
1、                      ;
Master:
mysql>grant replication client,replication slave on *.* to [email protected] identified by '135246';
mysql>flush privileges;
Slave:
mysql>grant replication client,replication slave on *.* to [email protected] identified by '135246';
mysql>flush privileges;
2、      :
Master:
[mysqld]
server-id = 11
log-bin = mysql-bin
auto-increment-increment = 2
auto-increment-offset = 1
relay-log=mysql-relay
relay-log-index=mysql-relay.index
Slave:
[mysqld]
server-id = 12
log-bin = mysql-bin
auto-increment-increment = 2
auto-increment-offset = 2
relay-log=mysql-relay
relay-log-index=mysql-relay.index

 
3.この時点で2台のサーバが新しく確立され、その他の書き込み操作がない場合、各サーバは現在の自分のバイナリログファイルとイベント位置を記録し、それを別のサーバとしてコピーの開始位置とすればよい
Master:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      360 |              |                  |
+------------------+----------+--------------+------------------+
Slave:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |      107 |              |                  |
+------------------+----------+--------------+------------------+
4、                          :
Master:
mysql>change master to \
master_host='172.16.4.12',
master_user='repl',
master_password='135246',
master_log_file='mysql-bin.000005',
master_log_pos=107;
Slave:
mysql>change master to \
master_host='172.16.4.11',
master_user='repl',
master_password='135246',
master_log_file='mysql-bin.000004',
master_log_pos=360;

 
5、サーバーからスレッドを開始する:
Master:
mysql>start slave;
 
Slave:
mysql>start slave;