Mariadb之半同步复制集群配置

前端之家收集整理的这篇文章主要介绍了Mariadb之半同步复制集群配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  首先我们来了解下在mariadb/MysqL数据库主从复制集群中什么是同步,什么是异步,什么是半同步;所谓同步就是指主节点发生写操作事件,它不会立刻返回,而是等到从节点接收到主节点发送过来的写操作事件,并在从节点完成重放后,从节点返回一个重放完成的消息给主节点,然后主节点才返回,这种叫同步;简单讲就是主节点要等从节点把写操作重放完成后再接收到从节点发来的重放完成消息后,然后返回告诉客户端;异步指的是在主节点发生写操作后,它只管把写操作产生的事件,写入到自己的二进制日志中后,就返回客户端;它不会等从节点重放完成;这种叫异步;默认情况mariadb/MysqL主从复制集群就是这种异步同步的方式进行;半同步指的是在主节点发生写操作事件后,它会把该操作的事件发送给从节点,当从节点接收到主节点发送过来的事件后,就立刻告诉主节点,从节点已经接收到主节点发送过来的事件,此时主机点并不会等到从节点重放完成,而是接收到从节点接收到主节点发送过去的的事件确认消息后,就返回给客户端;而在mariadb/MysqL主从复制集群中的半同步,并不是我们刚才说的这种半同步机制;在mariadb/MysqL主从复制集群中的半同步指的是,一主多从的复制集群中,一个从节点或一部分从节点和主节点是同步复制,一部分从节点是异步复制;

  配置mariadb半同步复制

  在mariadb/MysqL中,半同步的实现是基于插件的方式实现的,在早期的版本中我们需要手动安装模块,然后才能实现半同步复制;目前mairadb10.5.4的版本中,默认就支持半同步,不需要手动安装模块;

  1. [root@lxc ~]# MysqL
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 8
  4. Server version: 10.5.4-MariaDB-log MariaDB Server
  5.  
  6. Copyright (c) 2000,2018,Oracle,MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> show global variables like 'rpl%';
  11. +---------------------------------------+--------------+
  12. | Variable_name | Value |
  13. +---------------------------------------+--------------+
  14. | rpl_semi_sync_master_enabled | OFF |
  15. | rpl_semi_sync_master_timeout | 10000 |
  16. | rpl_semi_sync_master_trace_level | 32 |
  17. | rpl_semi_sync_master_wait_no_slave | ON |
  18. | rpl_semi_sync_master_wait_point | AFTER_COMMIT |
  19. | rpl_semi_sync_slave_delay_master | OFF |
  20. | rpl_semi_sync_slave_enabled | OFF |
  21. | rpl_semi_sync_slave_kill_conn_timeout | 5 |
  22. | rpl_semi_sync_slave_trace_level | 32 |
  23. +---------------------------------------+--------------+
  24. 9 rows in set (0.002 sec)
  25.  
  26. MariaDB [(none)]> show global status like 'rpl%';
  27. +--------------------------------------------+-------------+
  28. | Variable_name | Value |
  29. +--------------------------------------------+-------------+
  30. | Rpl_semi_sync_master_clients | 0 |
  31. | Rpl_semi_sync_master_get_ack | 0 |
  32. | Rpl_semi_sync_master_net_avg_wait_time | 0 |
  33. | Rpl_semi_sync_master_net_wait_time | 0 |
  34. | Rpl_semi_sync_master_net_waits | 0 |
  35. | Rpl_semi_sync_master_no_times | 0 |
  36. | Rpl_semi_sync_master_no_tx | 0 |
  37. | Rpl_semi_sync_master_request_ack | 0 |
  38. | Rpl_semi_sync_master_status | OFF |
  39. | Rpl_semi_sync_master_timefunc_failures | 0 |
  40. | Rpl_semi_sync_master_tx_avg_wait_time | 0 |
  41. | Rpl_semi_sync_master_tx_wait_time | 0 |
  42. | Rpl_semi_sync_master_tx_waits | 0 |
  43. | Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
  44. | Rpl_semi_sync_master_wait_sessions | 0 |
  45. | Rpl_semi_sync_master_yes_tx | 0 |
  46. | Rpl_semi_sync_slave_send_ack | 0 |
  47. | Rpl_semi_sync_slave_status | OFF |
  48. | Rpl_status | AUTH_MASTER |
  49. | Rpl_transactions_multi_engine | 0 |
  50. +--------------------------------------------+-------------+
  51. 20 rows in set (0.001 sec)
  52.  
  53. MariaDB [(none)]>

  提示:以上变量信息和状态信息可以了解到mariadb是支持半同步的,只是没有开启;如果在mariadb/MysqL数据库上查不到上面的信息,就需要我们手动安装模块;如下所示

  1. [root@docker_node01 ~]# MysqL
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 5
  4. Server version: 5.5.65-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000,MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> show global variables like 'rpl%';
  11. +-------------------+-------+
  12. | Variable_name | Value |
  13. +-------------------+-------+
  14. | rpl_recovery_rank | 0 |
  15. +-------------------+-------+
  16. 1 row in set (0.00 sec)
  17.  
  18. MariaDB [(none)]> show global status like 'rpl%';
  19. +---------------+-------------+
  20. | Variable_name | Value |
  21. +---------------+-------------+
  22. | Rpl_status | AUTH_MASTER |
  23. +---------------+-------------+
  24. 1 row in set (0.00 sec)
  25.  
  26. MariaDB [(none)]>

  提示:mariadb5.5.65默认是没有启动半同步插件,我们需要手动安装插件

  安装插件

  提示:默认情况在/usr/lib64/MysqL/plugin/目录下就有semisync_master.so和semisync_slave.so这两个模块;从名字上我们就能清楚知道semisync_master.so这个模块用于主节点,semisync_slave.so用于从节点;安装方法如下

  主节点安装semisync_master.so

  1. [root@docker_node01 ~]# MysqL
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 8
  4. Server version: 5.5.65-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000,MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> show global variables like 'rpl%';
  11. +-------------------+-------+
  12. | Variable_name | Value |
  13. +-------------------+-------+
  14. | rpl_recovery_rank | 0 |
  15. +-------------------+-------+
  16. 1 row in set (0.01 sec)
  17.  
  18. MariaDB [(none)]> show global status like 'rpl%';
  19. +---------------+-------------+
  20. | Variable_name | Value |
  21. +---------------+-------------+
  22. | Rpl_status | AUTH_MASTER |
  23. +---------------+-------------+
  24. 1 row in set (0.01 sec)
  25.  
  26. MariaDB [(none)]> show plugins;
  27. +--------------------------------+----------+--------------------+---------+---------+
  28. | Name | Status | Type | Library | License |
  29. +--------------------------------+----------+--------------------+---------+---------+
  30. | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
  31. | MysqL_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  32. | MysqL_old_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  33. | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
  34. | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  35. | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
  36. | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  37. | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
  38. | INNODB_RSEG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  39. | INNODB_UNDO_LOGS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  40. | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  41. | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  42. | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  43. | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  44. | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  45. | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  46. | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  47. | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  48. | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  49. | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  50. | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  51. | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  52. | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  53. | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  54. | INNODB_SYS_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  55. | INNODB_TABLE_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  56. | INNODB_INDEX_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  57. | INNODB_BUFFER_POOL_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  58. | INNODB_BUFFER_POOL_PAGES_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  59. | INNODB_BUFFER_POOL_PAGES_BLOB | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  60. | XTRADB_ADMIN_COMMAND | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  61. | INNODB_CHANGED_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  62. | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  63. | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  64. | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  65. | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
  66. | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  67. | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  68. | Aria | ACTIVE | STORAGE ENGINE | NULL | GPL |
  69. | FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL |
  70. | FeedBACK | DISABLED | INFORMATION SCHEMA | NULL | GPL |
  71. | partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
  72. +--------------------------------+----------+--------------------+---------+---------+
  73. 42 rows in set (0.00 sec)
  74.  
  75. MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
  76. Query OK,0 rows affected (0.00 sec)
  77.  
  78. MariaDB [(none)]> show plugins;
  79. +--------------------------------+----------+--------------------+--------------------+---------+
  80. | Name | Status | Type | Library | License |
  81. +--------------------------------+----------+--------------------+--------------------+---------+
  82. | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
  83. | MysqL_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  84. | MysqL_old_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  85. | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
  86. | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  87. | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
  88. | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  89. | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
  90. | INNODB_RSEG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  91. | INNODB_UNDO_LOGS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  92. | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  93. | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  94. | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  95. | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  96. | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  97. | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  98. | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  99. | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  100. | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  101. | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  102. | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  103. | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  104. | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  105. | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  106. | INNODB_SYS_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  107. | INNODB_TABLE_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  108. | INNODB_INDEX_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  109. | INNODB_BUFFER_POOL_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  110. | INNODB_BUFFER_POOL_PAGES_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  111. | INNODB_BUFFER_POOL_PAGES_BLOB | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  112. | XTRADB_ADMIN_COMMAND | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  113. | INNODB_CHANGED_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  114. | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  115. | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  116. | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  117. | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
  118. | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  119. | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  120. | Aria | ACTIVE | STORAGE ENGINE | NULL | GPL |
  121. | FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL |
  122. | FeedBACK | DISABLED | INFORMATION SCHEMA | NULL | GPL |
  123. | partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
  124. | rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
  125. +--------------------------------+----------+--------------------+--------------------+---------+
  126. 43 rows in set (0.00 sec)
  127.  
  128. MariaDB [(none)]> show global variables like 'rpl%';
  129. +------------------------------------+-------+
  130. | Variable_name | Value |
  131. +------------------------------------+-------+
  132. | rpl_recovery_rank | 0 |
  133. | rpl_semi_sync_master_enabled | OFF |
  134. | rpl_semi_sync_master_timeout | 10000 |
  135. | rpl_semi_sync_master_trace_level | 32 |
  136. | rpl_semi_sync_master_wait_no_slave | ON |
  137. +------------------------------------+-------+
  138. 5 rows in set (0.00 sec)
  139.  
  140. MariaDB [(none)]> show global status like 'rpl%';
  141. +--------------------------------------------+-------------+
  142. | Variable_name | Value |
  143. +--------------------------------------------+-------------+
  144. | Rpl_semi_sync_master_clients | 0 |
  145. | Rpl_semi_sync_master_net_avg_wait_time | 0 |
  146. | Rpl_semi_sync_master_net_wait_time | 0 |
  147. | Rpl_semi_sync_master_net_waits | 0 |
  148. | Rpl_semi_sync_master_no_times | 0 |
  149. | Rpl_semi_sync_master_no_tx | 0 |
  150. | Rpl_semi_sync_master_status | OFF |
  151. | Rpl_semi_sync_master_timefunc_failures | 0 |
  152. | Rpl_semi_sync_master_tx_avg_wait_time | 0 |
  153. | Rpl_semi_sync_master_tx_wait_time | 0 |
  154. | Rpl_semi_sync_master_tx_waits | 0 |
  155. | Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
  156. | Rpl_semi_sync_master_wait_sessions | 0 |
  157. | Rpl_semi_sync_master_yes_tx | 0 |
  158. | Rpl_status | AUTH_MASTER |
  159. +--------------------------------------------+-------------+
  160. 15 rows in set (0.00 sec)
  161.  
  162. MariaDB [(none)]>

  提示:如果是主节点就安装semisync_master.so,从节点就安装semisync_slave.so;当然你把两个模块都安装上也是可以的,只不过启动时对应启动就行;

  从节点安装semisync_slave.so

  1. [root@docker-node03 ~]# MysqL
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 10
  4. Server version: 5.5.65-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000,MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> show global variables like 'rpl%';
  11. +-------------------+-------+
  12. | Variable_name | Value |
  13. +-------------------+-------+
  14. | rpl_recovery_rank | 0 |
  15. +-------------------+-------+
  16. 1 row in set (0.00 sec)
  17.  
  18. MariaDB [(none)]> show global status like 'rpl%';
  19. +---------------+-------------+
  20. | Variable_name | Value |
  21. +---------------+-------------+
  22. | Rpl_status | AUTH_MASTER |
  23. +---------------+-------------+
  24. 1 row in set (0.00 sec)
  25.  
  26. MariaDB [(none)]> show plugins;
  27. +--------------------------------+----------+--------------------+---------+---------+
  28. | Name | Status | Type | Library | License |
  29. +--------------------------------+----------+--------------------+---------+---------+
  30. | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
  31. | MysqL_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  32. | MysqL_old_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  33. | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
  34. | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  35. | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
  36. | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  37. | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
  38. | INNODB_RSEG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  39. | INNODB_UNDO_LOGS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  40. | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  41. | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  42. | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  43. | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  44. | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  45. | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  46. | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  47. | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  48. | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  49. | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  50. | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  51. | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  52. | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  53. | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  54. | INNODB_SYS_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  55. | INNODB_TABLE_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  56. | INNODB_INDEX_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  57. | INNODB_BUFFER_POOL_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  58. | INNODB_BUFFER_POOL_PAGES_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  59. | INNODB_BUFFER_POOL_PAGES_BLOB | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  60. | XTRADB_ADMIN_COMMAND | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  61. | INNODB_CHANGED_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  62. | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  63. | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  64. | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  65. | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
  66. | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  67. | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  68. | Aria | ACTIVE | STORAGE ENGINE | NULL | GPL |
  69. | FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL |
  70. | FeedBACK | DISABLED | INFORMATION SCHEMA | NULL | GPL |
  71. | partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
  72. +--------------------------------+----------+--------------------+---------+---------+
  73. 42 rows in set (0.00 sec)
  74.  
  75. MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
  76. Query OK,0 rows affected (0.00 sec)
  77.  
  78. MariaDB [(none)]> show plugins;
  79. +--------------------------------+----------+--------------------+-------------------+---------+
  80. | Name | Status | Type | Library | License |
  81. +--------------------------------+----------+--------------------+-------------------+---------+
  82. | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
  83. | MysqL_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  84. | MysqL_old_password | ACTIVE | AUTHENTICATION | NULL | GPL |
  85. | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
  86. | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  87. | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
  88. | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
  89. | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
  90. | INNODB_RSEG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  91. | INNODB_UNDO_LOGS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  92. | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  93. | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  94. | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  95. | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  96. | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  97. | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  98. | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  99. | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  100. | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  101. | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  102. | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  103. | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  104. | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  105. | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  106. | INNODB_SYS_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  107. | INNODB_TABLE_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  108. | INNODB_INDEX_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  109. | INNODB_BUFFER_POOL_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  110. | INNODB_BUFFER_POOL_PAGES_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  111. | INNODB_BUFFER_POOL_PAGES_BLOB | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  112. | XTRADB_ADMIN_COMMAND | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  113. | INNODB_CHANGED_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  114. | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  115. | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  116. | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
  117. | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
  118. | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  119. | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
  120. | Aria | ACTIVE | STORAGE ENGINE | NULL | GPL |
  121. | FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL |
  122. | FeedBACK | DISABLED | INFORMATION SCHEMA | NULL | GPL |
  123. | partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
  124. | rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
  125. +--------------------------------+----------+--------------------+-------------------+---------+
  126. 43 rows in set (0.01 sec)
  127.  
  128. MariaDB [(none)]> show global variables like 'rpl%';
  129. +---------------------------------+-------+
  130. | Variable_name | Value |
  131. +---------------------------------+-------+
  132. | rpl_recovery_rank | 0 |
  133. | rpl_semi_sync_slave_enabled | OFF |
  134. | rpl_semi_sync_slave_trace_level | 32 |
  135. +---------------------------------+-------+
  136. 3 rows in set (0.00 sec)
  137.  
  138. MariaDB [(none)]> show global status like 'rpl%';
  139. +----------------------------+-------------+
  140. | Variable_name | Value |
  141. +----------------------------+-------------+
  142. | Rpl_semi_sync_slave_status | OFF |
  143. | Rpl_status | AUTH_MASTER |
  144. +----------------------------+-------------+
  145. 2 rows in set (0.00 sec)
  146.  
  147. MariaDB [(none)]>

  提示:可以看到安装好插件后,我们就可以在插件列表中相应的插件,在全局变量中也能查到对应的变量和状态信息;

  到此半同步需要要点插件就已经装载完成;接下来就可以直接配置主从复制了,然后在开启半同步插件

  配置mariadb/MysqL主从复制

  主节点开启二进制日志,设置server-id为1,开启sync_binlog=on

  提示:以上在主节点开启了二进制日志,开启同步刷新二进制日志到磁盘二进制日志;开启刷新事务日志,设置服务器id为1

  重启主节点,在主节点创建具有复制权限的账号,并查看当前二进制日志文件名和对应位置

  1. [root@docker_node01 ~]# systemctl restart mariadb
  2. [root@docker_node01 ~]# MysqL
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 4
  5. Server version: 5.5.65-MariaDB MariaDB Server
  6.  
  7. Copyright (c) 2000,MariaDB Corporation Ab and others.
  8.  
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10.  
  11. MariaDB [(none)]> grant replication slave,replication client on *.* to 'rpluser'@'192.168.0.%' identified by 'admin';
  12. Query OK,0 rows affected (0.00 sec)
  13.  
  14. MariaDB [(none)]> flush privileges;
  15. Query OK,0 rows affected (0.01 sec)
  16.  
  17. MariaDB [(none)]> show master status;
  18. +------------------+----------+--------------+------------------+
  19. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  20. +------------------+----------+--------------+------------------+
  21. | MysqL-bin.000003 | 642 | | |
  22. +------------------+----------+--------------+------------------+
  23. 1 row in set (0.00 sec)
  24.  
  25. MariaDB [(none)]>

  从节点配置server-id为2,配置从节点为只读,开启从节点的中继日志

  提示:以上配置表示,设置服务器id为2,开启中继日志,并保持到/var/lib/MysqL/relay-log中;开启只读(仅对非super权限的用户生效);开启同步刷新master_info到磁盘;开启同步刷新relay_log_info到磁盘;后面两项主要是确保事实时记录复制和重放二进制日志的位置,避免出现故障,重复复制;

  重启从节点,配置连接从服务器连接主服务的相关信息

  1. [root@docker-node03 ~]# systemctl restart mariadb
  2. [root@docker-node03 ~]# MysqL
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 2
  5. Server version: 5.5.65-MariaDB MariaDB Server
  6.  
  7. Copyright (c) 2000,MariaDB Corporation Ab and others.
  8.  
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10.  
  11. MariaDB [(none)]> CHANGE MASTER TO
  12. -> MASTER_HOST='192.168.0.22',-> MASTER_USER='rpluser',-> MASTER_PASSWORD='admin',-> MASTER_PORT=3306,-> MASTER_LOG_FILE='MysqL-bin.000003',-> MASTER_LOG_POS=642;
  13. Query OK,0 rows affected (0.06 sec)
  14.  
  15. MariaDB [(none)]> show slave status\G
  16. *************************** 1. row ***************************
  17. Slave_IO_State:
  18. Master_Host: 192.168.0.22
  19. Master_User: rpluser
  20. Master_Port: 3306
  21. Connect_Retry: 60
  22. Master_Log_File: MysqL-bin.000003
  23. Read_Master_Log_Pos: 642
  24. Relay_Log_File: relay-log.000001
  25. Relay_Log_Pos: 4
  26. Relay_Master_Log_File: MysqL-bin.000003
  27. Slave_IO_Running: No
  28. Slave_sql_Running: No
  29. Replicate_Do_DB:
  30. Replicate_Ignore_DB:
  31. Replicate_Do_Table:
  32. Replicate_Ignore_Table:
  33. Replicate_Wild_Do_Table:
  34. Replicate_Wild_Ignore_Table:
  35. Last_Errno: 0
  36. Last_Error:
  37. Skip_Counter: 0
  38. Exec_Master_Log_Pos: 642
  39. Relay_Log_Space: 245
  40. Until_Condition: None
  41. Until_Log_File:
  42. Until_Log_Pos: 0
  43. Master_SSL_Allowed: No
  44. Master_SSL_CA_File:
  45. Master_SSL_CA_Path:
  46. Master_SSL_Cert:
  47. Master_SSL_Cipher:
  48. Master_SSL_Key:
  49. Seconds_Behind_Master: NULL
  50. Master_SSL_Verify_Server_Cert: No
  51. Last_IO_Errno: 0
  52. Last_IO_Error:
  53. Last_sql_Errno: 0
  54. Last_sql_Error:
  55. Replicate_Ignore_Server_Ids:
  56. Master_Server_Id: 0
  57. 1 row in set (0.00 sec)
  58.  
  59. MariaDB [(none)]>

  启动io线程和sql线程

  1. MariaDB [(none)]> start slave;
  2. Query OK,0 rows affected (0.00 sec)
  3.  
  4. MariaDB [(none)]> show slave status\G
  5. *************************** 1. row ***************************
  6. Slave_IO_State: Waiting for master to send event
  7. Master_Host: 192.168.0.22
  8. Master_User: rpluser
  9. Master_Port: 3306
  10. Connect_Retry: 60
  11. Master_Log_File: MysqL-bin.000003
  12. Read_Master_Log_Pos: 642
  13. Relay_Log_File: relay-log.000002
  14. Relay_Log_Pos: 529
  15. Relay_Master_Log_File: MysqL-bin.000003
  16. Slave_IO_Running: Yes
  17. Slave_sql_Running: Yes
  18. Replicate_Do_DB:
  19. Replicate_Ignore_DB:
  20. Replicate_Do_Table:
  21. Replicate_Ignore_Table:
  22. Replicate_Wild_Do_Table:
  23. Replicate_Wild_Ignore_Table:
  24. Last_Errno: 0
  25. Last_Error:
  26. Skip_Counter: 0
  27. Exec_Master_Log_Pos: 642
  28. Relay_Log_Space: 817
  29. Until_Condition: None
  30. Until_Log_File:
  31. Until_Log_Pos: 0
  32. Master_SSL_Allowed: No
  33. Master_SSL_CA_File:
  34. Master_SSL_CA_Path:
  35. Master_SSL_Cert:
  36. Master_SSL_Cipher:
  37. Master_SSL_Key:
  38. Seconds_Behind_Master: 0
  39. Master_SSL_Verify_Server_Cert: No
  40. Last_IO_Errno: 0
  41. Last_IO_Error:
  42. Last_sql_Errno: 0
  43. Last_sql_Error:
  44. Replicate_Ignore_Server_Ids:
  45. Master_Server_Id: 1
  46. 1 row in set (0.00 sec)
  47.  
  48. MariaDB [(none)]>

  提示:到此主从复制就搭建好了,但是我们还没有启动半同步插件,到此之前mariadb的主从复制还是基于一部的方式进行,接下来我们启动半同步插件

  在主节点启用半同步插件

  1. [root@docker_node01 ~]# MysqL
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 7
  4. Server version: 5.5.65-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000,MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> show variables like 'rpl%';
  11. +------------------------------------+-------+
  12. | Variable_name | Value |
  13. +------------------------------------+-------+
  14. | rpl_recovery_rank | 0 |
  15. | rpl_semi_sync_master_enabled | OFF |
  16. | rpl_semi_sync_master_timeout | 10000 |
  17. | rpl_semi_sync_master_trace_level | 32 |
  18. | rpl_semi_sync_master_wait_no_slave | ON |
  19. +------------------------------------+-------+
  20. 5 rows in set (0.00 sec)
  21.  
  22. MariaDB [(none)]> set global rpl_semi_sync_master_enabled=ON;
  23. Query OK,0 rows affected (0.00 sec)
  24.  
  25. MariaDB [(none)]> show variables like 'rpl%';
  26. +------------------------------------+-------+
  27. | Variable_name | Value |
  28. +------------------------------------+-------+
  29. | rpl_recovery_rank | 0 |
  30. | rpl_semi_sync_master_enabled | ON |
  31. | rpl_semi_sync_master_timeout | 10000 |
  32. | rpl_semi_sync_master_trace_level | 32 |
  33. | rpl_semi_sync_master_wait_no_slave | ON |
  34. +------------------------------------+-------+
  35. 5 rows in set (0.01 sec)
  36.  
  37. MariaDB [(none)]> show status like 'rpl%';
  38. +--------------------------------------------+-------------+
  39. | Variable_name | Value |
  40. +--------------------------------------------+-------------+
  41. | Rpl_semi_sync_master_clients | 0 |
  42. | Rpl_semi_sync_master_net_avg_wait_time | 0 |
  43. | Rpl_semi_sync_master_net_wait_time | 0 |
  44. | Rpl_semi_sync_master_net_waits | 0 |
  45. | Rpl_semi_sync_master_no_times | 0 |
  46. | Rpl_semi_sync_master_no_tx | 0 |
  47. | Rpl_semi_sync_master_status | ON |
  48. | Rpl_semi_sync_master_timefunc_failures | 0 |
  49. | Rpl_semi_sync_master_tx_avg_wait_time | 0 |
  50. | Rpl_semi_sync_master_tx_wait_time | 0 |
  51. | Rpl_semi_sync_master_tx_waits | 0 |
  52. | Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
  53. | Rpl_semi_sync_master_wait_sessions | 0 |
  54. | Rpl_semi_sync_master_yes_tx | 0 |
  55. | Rpl_status | AUTH_MASTER |
  56. +--------------------------------------------+-------------+
  57. 15 rows in set (0.00 sec)
  58.  
  59. MariaDB [(none)]>

  提示:可以看到半同步主节点插件已经开启

  从节点启用半同步插件

  1. [root@docker-node03 ~]# MysqL
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 7
  4. Server version: 5.5.65-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000,MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> show global variables like 'rpl%';
  11. +---------------------------------+-------+
  12. | Variable_name | Value |
  13. +---------------------------------+-------+
  14. | rpl_recovery_rank | 0 |
  15. | rpl_semi_sync_slave_enabled | OFF |
  16. | rpl_semi_sync_slave_trace_level | 32 |
  17. +---------------------------------+-------+
  18. 3 rows in set (0.00 sec)
  19.  
  20. MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on;
  21. Query OK,0 rows affected (0.00 sec)
  22.  
  23. MariaDB [(none)]> show global variables like 'rpl%';
  24. +---------------------------------+-------+
  25. | Variable_name | Value |
  26. +---------------------------------+-------+
  27. | rpl_recovery_rank | 0 |
  28. | rpl_semi_sync_slave_enabled | ON |
  29. | rpl_semi_sync_slave_trace_level | 32 |
  30. +---------------------------------+-------+

  重启从节点IO线程

  1. MariaDB [(none)]> stop slave io_thread;
  2. Query OK,0 rows affected (0.01 sec)
  3.  
  4. MariaDB [(none)]> start slave io_thread;
  5. Query OK,0 rows affected (0.00 sec)
  6.  
  7. MariaDB [(none)]> show slave status\G
  8. *************************** 1. row ***************************
  9. Slave_IO_State: Waiting for master to send event
  10. Master_Host: 192.168.0.22
  11. Master_User: rpluser
  12. Master_Port: 3306
  13. Connect_Retry: 60
  14. Master_Log_File: MysqL-bin.000003
  15. Read_Master_Log_Pos: 642
  16. Relay_Log_File: relay-log.000004
  17. Relay_Log_Pos: 529
  18. Relay_Master_Log_File: MysqL-bin.000003
  19. Slave_IO_Running: Yes
  20. Slave_sql_Running: Yes
  21. Replicate_Do_DB:
  22. Replicate_Ignore_DB:
  23. Replicate_Do_Table:
  24. Replicate_Ignore_Table:
  25. Replicate_Wild_Do_Table:
  26. Replicate_Wild_Ignore_Table:
  27. Last_Errno: 0
  28. Last_Error:
  29. Skip_Counter: 0
  30. Exec_Master_Log_Pos: 642
  31. Relay_Log_Space: 1101
  32. Until_Condition: None
  33. Until_Log_File:
  34. Until_Log_Pos: 0
  35. Master_SSL_Allowed: No
  36. Master_SSL_CA_File:
  37. Master_SSL_CA_Path:
  38. Master_SSL_Cert:
  39. Master_SSL_Cipher:
  40. Master_SSL_Key:
  41. Seconds_Behind_Master: 0
  42. Master_SSL_Verify_Server_Cert: No
  43. Last_IO_Errno: 0
  44. Last_IO_Error:
  45. Last_sql_Errno: 0
  46. Last_sql_Error:
  47. Replicate_Ignore_Server_Ids:
  48. Master_Server_Id: 1
  49. 1 row in set (0.00 sec)
  50.  
  51. MariaDB [(none)]>

  主节点查看状态信息,看看是否有从节点以半同步的方式连接上来了

  提示:在主节点上可以看到状态信息中有一个从节点已经连接上来了;到此半同步复制就配置完了;

  测试:在主节点上执行写操作,看看对应状态有什么变化?

  1. MariaDB [(none)]> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | MysqL |
  7. | performance_schema |
  8. | test |
  9. +--------------------+
  10. 4 rows in set (0.01 sec)
  11.  
  12. MariaDB [(none)]> create database first_db;
  13. Query OK,1 row affected (0.01 sec)
  14.  
  15. MariaDB [(none)]> show global variables like 'rpl%';
  16. +------------------------------------+-------+
  17. | Variable_name | Value |
  18. +------------------------------------+-------+
  19. | rpl_recovery_rank | 0 |
  20. | rpl_semi_sync_master_enabled | ON |
  21. | rpl_semi_sync_master_timeout | 10000 |
  22. | rpl_semi_sync_master_trace_level | 32 |
  23. | rpl_semi_sync_master_wait_no_slave | ON |
  24. +------------------------------------+-------+
  25. 5 rows in set (0.00 sec)
  26.  
  27. MariaDB [(none)]> show global status like 'rpl%';
  28. +--------------------------------------------+-------------+
  29. | Variable_name | Value |
  30. +--------------------------------------------+-------------+
  31. | Rpl_semi_sync_master_clients | 1 |
  32. | Rpl_semi_sync_master_net_avg_wait_time | 1037 |
  33. | Rpl_semi_sync_master_net_wait_time | 1037 |
  34. | Rpl_semi_sync_master_net_waits | 1 |
  35. | Rpl_semi_sync_master_no_times | 0 |
  36. | Rpl_semi_sync_master_no_tx | 0 |
  37. | Rpl_semi_sync_master_status | ON |
  38. | Rpl_semi_sync_master_timefunc_failures | 0 |
  39. | Rpl_semi_sync_master_tx_avg_wait_time | 1321 |
  40. | Rpl_semi_sync_master_tx_wait_time | 1321 |
  41. | Rpl_semi_sync_master_tx_waits | 1 |
  42. | Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
  43. | Rpl_semi_sync_master_wait_sessions | 0 |
  44. | Rpl_semi_sync_master_yes_tx | 1 |
  45. | Rpl_status | AUTH_MASTER |
  46. +--------------------------------------------+-------------+
  47. 15 rows in set (0.00 sec)
  48.  
  49. MariaDB [(none)]>

  提示:可以看到,当我们在主库中新建了一个first_db的库,在状态信息中可以看到创建这个库,平均等待了1037毫秒,事务平均等待时间是1321毫秒;这意味着主节点在完成发送事件给从节点,从节点重放完成后,返回给主节点用了1321毫秒;

  在从库中查看,是否将first_db同步过来了?

  提示:在从库上可以看到刚才在主库上创建的库,已经同步到从库了;

到此半同步复制集群就配置好了,这里需要提醒下,配置半同步复制集群有损主库写的性能,毕竟它要等待从库完成重放后或者等到超时后才能返回,如果从库宕机了,那么主库会等到超时后自动降级为异步;如果还要对主库做高可用,那么配置半同步的从节点就是优选主节点的从节点;有关主库高可用集群解决方案MHA的相关配置可以参考本人博客https://www.cnblogs.com/qiuhom-1874/p/12183521.html

猜你在找的Mariadb相关文章