一、MariaDB的GTID          

   MySQL 5.6引入的GTID(Global Transaction IDs)使得其复制功能的配置、监控及管理变得更加易于实现,且更加健壮。

   要在MySQL 5.6中使用复制功能,其服务配置段[mysqld]中于少应该定义如下选项:

   binlog-format:二进制日志的格式,有row、statement和mixed几种类型;

   需要注意的是:当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;

   log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用于启动GTID及满足附属的其它需求;

master-info-repository和relay-log-info-repository:启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;

   sync-master-info:启用之可确保无信息丢失;

   slave-paralles-workers:设定从服务器的SQL线程数;0表示关闭多线程复制功能;

   binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:启用复制有关的所有校验功能;

   binlog-rows-query-log-events:启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度;

   log-bin:启用二进制日志,这是保证复制功能的基本前提;

   server-id:同一个复制拓扑中的所有服务器的id号必须惟一;

   report-host:

   The host name or IP address of the slave to be reported to the master during slave registration. This value appears in the output of SHOW SLAVE HOSTS on the master server.

   report-port:

   The TCP/IP port number for connecting to the slave, to be reported to the master during slave registration.

   master-info-repository:

   The setting of this variable determines whether the slave logs master status and connection information to a FILE (master.info), or to a TABLE (mysql.slave_master_info)

   relay-log-info-repository:

   This option causes the server to log its relay log info to a file or a table.

   log_slave_updates:

   Whether updates received by a slave server from a master server should be logged to the slave's own binary log. Binary logging must be enabled on the slave for this variable to have any effect.

    enforce_gtid_consistency:

1、简单主从模式配置步骤

   1、配置主从节点的服务配置文件

   1.1、配置master节点:

[mysqld]
binlog-format=ROW
log-bin=master-bin
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=1
report-port=3306
port=3306
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=master.wangfeng7399.com

   1.2、配置slave节点:

[mysqld]
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=11
report-port=3306
port=3306
log-bin=mysql-bin.log
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=slave.wangfeng7399.com

   2、创建复制用户

MariaDB[none]> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY 'replpass';

   说明:192.168.1.201是从节点服务器;如果想一次性授权更多的节点,可以自行根据需要修改;

   3、为备节点提供初始数据集

   锁定主表,备份主节点上的数据,将其还原至从节点;如果没有启用GTID,在备份时需要在master上使用show master status命令查看二进制日志文件名称及事件位置,以便后面启动slave节点时使用。

   4、启动从节点的复制线程

   如果启用了GTID功能,则使用如下命令:  

MariaDB[none]> CHANGE MASTER TO MASTER_HOST='master.waangfeng7399.com', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_AUTO_POSITION=1;

   没启用GTID,需要使用如下命令:  

slave> CHANGE MASTER TO MASTER_HOST='192.168.1.201',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000005',MASTER_LOG_POS=453;

二、半同步复制

   1、分别在主从节点上安装相关的插件

       master> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

       slave> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

   2、启用半同步复制

       在master上的配置文件中,添加 rpl_semi_sync_master_enabled=ON

       在至少一个slave节点的配置文件中添加 rpl_semi_sync_slave_enabled=ON

       而后重新启动mysql服务即可生效。或者,也可以mysql服务上动态启动其相关功能:

       master> SET GLOBAL rpl_semi_sync_master_enabled = ON;

       slave> SET GLOBAL rpl_semi_sync_slave_enabled = ON;

       slave> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;

   3、确认半同步功能已经启用

       master> CREATE DATABASE magedudb;

       master> SHOW STATUS LIKE 'Rpl_semi_sync_master_yes_tx';

       slave> SHOW DATABASES;