溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL--------基于半同步復(fù)制搭建主從

發(fā)布時(shí)間:2020-03-24 01:08:36 來源:網(wǎng)絡(luò) 閱讀:5573 作者:asd1123509133 欄目:MySQL數(shù)據(jù)庫

1. 背景

   * MySQL Replication默認(rèn)都是異步(asynchronous),當(dāng)主庫在執(zhí)行完一些事務(wù)后,是不會(huì)管備庫的進(jìn)度的。如果備庫不幸落后,而更不幸的是主庫此時(shí)又出現(xiàn)Crash(例如宕機(jī)),這時(shí)備庫中的數(shù)據(jù)就是不完整的。簡(jiǎn)而言之,在主庫發(fā)生故障的時(shí)候,我們無法使用備庫來繼續(xù)提供數(shù)據(jù)一致的服務(wù)了。

   * Semi sync Replication在一定程度上保證提交的事務(wù)已經(jīng)傳給了至少一個(gè)備庫。

   * Semi sync Replication僅僅保證事務(wù)的已經(jīng)傳遞到備庫上,但是并不確保已經(jīng)在備庫上執(zhí)行完成。

   * Semi sync Replication主庫等待超時(shí)(rpl_semi_sync_master_timeout)后,會(huì)自動(dòng)降級(jí)為默認(rèn)的異步(asynchronous)。

     * MySQL 5.5版本開始支持Semi sync Replication

MySQL--------基于半同步復(fù)制搭建主從


2. 環(huán)境

  * master 服務(wù)器環(huán)境

mysql> system cat /etc/redhat-release
CentOS release 6.8 (Final)
mysql> system ifconfig eth0  | sed -rn '2s#^.*addr:(.*)  Bca.*$#\1#gp'
172.18.0.1
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.36-log |
+---------------+------------+
1 row in set (0.00 sec)


   * master 服務(wù)器環(huán)境

mysql> system cat /etc/redhat-release
CentOS release 6.8 (Final)
mysql> system ifconfig eth0  | sed -rn '2s#^.*addr:(.*)  Bca.*$#\1#gp'
172.18.4.1
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.36-log |
+---------------+------------+
1 row in set (0.00 sec)


   * master服務(wù)器my.cnf配置文件

[mysqld]
########basic settings########
# 主從server-id一定要設(shè)置不同
server-id = 110
port = 3306
user = mysql
bind_address = 0.0.0.0    
character_set_server=utf8mb4
skip_name_resolve = 1
datadir = /data/mysql_data
log_error = error.log
#######replication settings########
master_info_repository = TABLE
relay_log_info_repository = TABLE
# MySQL復(fù)制是基于binlog日志的
log_bin = bin.log
sync_binlog = 1
log_slave_updates
# MySQL binlog格式搭建主從時(shí)必須設(shè)置為row
binlog_format = row
relay_log = relay.log
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
######semi sync replication settings########
# 設(shè)置插件目錄路徑
plugin_dir=/usr/local/mysql/lib/plugin
# 加載插件
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
# 開啟master semi sync replication
loose_rpl_semi_sync_master_enabled = 1
# 開啟slave semi sync replication
loose_rpl_semi_sync_slave_enabled = 1
# 等待5秒無ack應(yīng)答自動(dòng)切換為異步模式
loose_rpl_semi_sync_master_timeout = 5000


   * slave服務(wù)器my.cnf配置文件

[mysqld]
########basic settings########
server-id = 210
port = 3306
user = mysql
bind_address = 0.0.0.0
character_set_server=utf8mb4
skip_name_resolve = 1
datadir = /data/mysql_data
log_error = error.log
# slave上開啟只讀,避免應(yīng)用誤寫導(dǎo)致主從數(shù)據(jù)不一致
read_only = on
#######replication settings########
master_info_repository = TABLE
relay_log_info_repository = TABLE
log_bin = bin.log
sync_binlog = 1
log_slave_updates
binlog_format = row
relay_log = relay.log
relay_log_recovery = 1
binlog_gtid_simple_recovery = 1
slave_skip_errors = ddl_exist_errors
######semi sync replication settings########
plugin_dir=/usr/local/mysql/lib/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000


3. 搭建基于半同步主從

   * Master 創(chuàng)建復(fù)制所使用的用戶 [ 此處ip設(shè)置為slave服務(wù)IP或者% ]

grant replication slave on *.* to 'rpl'@'172.18.4.1' identified by '123';


   * master服務(wù)器上查看binlog文件名和日志位置

mysql> show master status;
+------------+----------+--------------+------------------+-------------------+
| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+-------------------+
| bin.000003 |     1187 |              |                  |                   |
+------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


   * slave服務(wù)器上設(shè)置master信息

        未開啟slave服務(wù)時(shí),Slave_IO_Running與Slave_SQL_Running狀態(tài)成No

mysql> show slave status;     # 未開啟復(fù)制功能時(shí),slave狀態(tài)是空的
Empty set (0.00 sec)

mysql> change master to master_host='172.18.0.1',master_user='rpl',master_password='123',master_log_file='bin.000003',master_log_pos=1187;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.18.0.1
                  Master_User: rpl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: bin.000003
          Read_Master_Log_Pos: 1187
               Relay_Log_File: relay.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: bin.000003
             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: 1187
              Relay_Log_Space: 120
              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
                  Master_UUID: 
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)


   * 開啟slave服務(wù),并查看狀態(tài)

   正常開啟slave服務(wù)后,Slave_IO_Running與Slave_SQL_Running狀態(tài)成Yes

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.0.1
                  Master_User: rpl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: bin.000003
          Read_Master_Log_Pos: 1187
               Relay_Log_File: relay.000002
                Relay_Log_Pos: 277
        Relay_Master_Log_File: 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: 1187
              Relay_Log_Space: 440
              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: 110
                  Master_UUID: 08ea3ca7-6dd4-11e7-a45c-00163e0432c5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)


   * Master上查看Slave連接信息

mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|       210 |      | 3306 |       110 | d6ceba69-6dd4-11e7-a462-00163e028c02 |
+-----------+------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)


  * Master上操作創(chuàng)建數(shù)據(jù)庫與表,并插入數(shù)據(jù)

mysql> create database mytest character set utf8mb4;
Query OK, 1 row affected (0.01 sec)

mysql> use mytest;
mysql> create table users(
    -> id BIGINT NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(255) NOT NULL,
    -> sex ENUM('M', 'F') NOT NULL DEFAULT 'M',
    -> age INT SIGNED NOT NULL DEFAULT '0',
    -> PRIMARY KEY (id)
    -> )ENGINE=INNODB DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into users values(null, 'tom', 'M', 24), (null, 'jak', 'F', 32), (null, 'sea', 'M', 35), (null, 'lisea', 'M', 29);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  24 |
|  2 | jak   | F   |  32 |
|  3 | sea   | M   |  35 |
|  4 | lisea | M   |  29 |
+----+-------+-----+-----+
4 rows in set (0.00 sec)


   * Slave上查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use mytest;
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> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| users            |
+------------------+
1 row in set (0.00 sec)

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  24 |
|  2 | jak   | F   |  32 |
|  3 | sea   | M   |  35 |
|  4 | lisea | M   |  29 |
+----+-------+-----+-----+
4 rows in set (0.00 sec)


4. Master/Slave傳輸過程

MySQL--------基于半同步復(fù)制搭建主從

5. 總結(jié)


以需求驅(qū)動(dòng)技術(shù),技術(shù)本身沒有優(yōu)略之分,只有業(yè)務(wù)之分。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI