MySQL複製

MySQL數據同步方式:

Synchronous Replication 同步複製

Asynchronous Replication 異步複製

Semisynchronous Replication 半同步複製 

    

    同步複製:指的是客戶端連接到MySQL主服務器寫入一段數據,MySQL主服務器同步給MySQL從服務器需要等待從服務器發出同步完成的響應才返回客戶端OK, 這其中等待同步的過程是阻塞的, 如果有N臺從服務器, 效率極低 

    異步複製: 指的是客戶端連接到MySQL主服務器寫入一段數據,MySQL主服務器將寫入的數據發送給MySQL從服務器, 然後直接返回客戶端OK, 可能從服務器的數據會和主服務不一致 

    半同步複製:指的是客戶端連接到MySQL主服務器寫入一段數據, MySQL主服務器只將數據同步複製給其中一臺從服務器, 半同步複製給其他的從服務器, 來達到其中一臺從服務器完全同步的效果



MySQL複製原理

wKiom1g7oYaTW1zxAACoZKVTYhE643.png

建立主從關係後, 主服務器如果有數據修改之後, BINLOG會更新, 從服務通過IO-Thread讀取主服務器的BINLOG到本地的Relay Log, 再通過SQL-Thread對其進行重放(replay), 從而同步到本地




MySQL簡單複製

拓撲:

wKiom1g7pLSzqQywAAA2VyrfGxc968.png


Master配置文件

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0
skip_name_resolve = ON
innodb_file_per_table = ON

server_id=1                 #主服務器ID
log_bin=master-bin             #開啓二進制日誌

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d


Slave配置文件

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

skip_name_resolve = ON
innodb_file_per_table = ON

server_id=2         #從服務器ID
relay_log=relay-log #開啓中繼日誌

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d


在主Master上面查看當前正在使用的二進制日誌,並記錄下position

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 |      245 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)


主Master上創建用戶並賦予權限

MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.%.%' IDENTIFIED BY '
userpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)


配置Slave

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.100',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> SLAVE START;               #啓動IO和SQL  Thread
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G        #查看相應信息
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.72.100
                  Master_User: cpuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes       #IO Thread已啓動
            Slave_SQL_Running: Yes       #SQL Thread已啓動
              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: 245
              Relay_Log_Space: 818
              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: 1
1 row in set (0.00 sec)


測試:

在Master上創建一個數據庫

wKiom1g7rUqjZZZsAAA1YJOxDOo413.png


Slave驗證

wKioL1g7rXjCdt7dAAAiK3ngZj4822.png




主主複製配置

拓撲

wKiom1g73tGwhHfbAAA3_klsVmM499.png

互爲主從:兩個節點各自都要開啓binlog和relay log


配置文件

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

skip_name_resolve = ON
innodb_file_per_table = ON

server_id=1           #服務器ID
log_bin=mysql-bin     #開啓二進制日誌
relay_log=relay-log   #開啓中繼日誌

auto_increment_offset=1
auto_increment_increment=2

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d


都授權有複製全的用戶賬號,並把對方指定爲主節點


Master1的配置

MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.72.200' IDENTIFIED BY 'userpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      506 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.200',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=506;
Query OK, 0 rows affected (0.03 sec)


Master2的配置

MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.72.100' IDENTIFIED BY 'userpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.100',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=506;
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      506 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)


在兩個節點上同時啓動IO和SQL Thread

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.72.100
                  Master_User: cpuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 506
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             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: 506
              Relay_Log_Space: 817
              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: 1
1 row in set (0.00 sec)


驗證

在Master1上創建數據庫test1,可同步至Master2上,相反在Master2上創建test2也可同步至Master1

wKioL1g74JDCfUAuAAAzxp7FLW4235.png




半同步複製配置

拓撲

wKiom1g7402CWfW7AACPcKQ--sk722.png


半同步的插件在/usr/lib64/mysql/plugin下, master用的semisync_master.so,slave用的semisync_slave.so

配置Master

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_timeout=2000;
Query OK, 0 rows affected (0.00 sec)


配置Slave

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)


驗證

wKiom1g76hqAp-koAABSZ78o1lM480.png



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章