赞
踩
半同步复制时,为了保证主库上的每一个Binlog事务都能够被可靠的复制到从库上,主库在每次事务成功提交时,并不及时反馈给前端应用用户,而是等待其中的一个从库也接收到Binlog事务并成功写入中继日志后,出库才返回commit操作成功给客户端。半同步复制保证了事务成功提交后,至少有两份日志记录,一份在主库的Binlog日志上,另一份在至少一个从库的中继日志Relay log上,从而更近一步保证了数据的完整性。
前文已经有二进制多实例主从复制的文档,本文档不再从新开启主从复制,如果没有开启请点击:二进制主从复制
在做半同步复制前请保证开启了主从同步
[mark_d]注意:主库与从库加载的插件不是同一个!!![/mark_d]
[root@mysql-manager ~]# mysql -S /opstands/mysql-3307/mysql.sock -e "install plugin rpl_semi_sync_master soname 'semisync_master.so';"
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "install plugin rpl_semi_sync_slave soname 'semisync_slave.so';"
[root@mysql-manager ~]# mysql -S /opstands/mysql-3307/mysql.sock -e "show plugins" |grep rpl_semi_sync
rpl_semi_sync_master ACTIVE REPLICATION semisync_master.so GPL
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "show plugins" |grep rpl_semi_sync
rpl_semi_sync_slave ACTIVE REPLICATION semisync_slave.so GPL
注意: 插件名称不一样
[root@mysql-manager ~]# mysql -S /opstands/mysql-3307/mysql.sock -e "set global rpl_semi_sync_master_enabled = 1;"
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "set global rpl_semi_sync_slave_enabled= 1 ;"
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "stop slave io_thread;"
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "start slave io_thread;"
[root@mysql-manager ~]# mysql -S /opstands/mysql-3307/mysql.sock -e "show status like 'Rpl_semi_sync_master_status';"
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON |
+-----------------------------+-------+
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "show status like 'Rpl_semi_sync_slave_status';"
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
在mysql复制环境中,有8个参数可以让我们控制,需要复制或需要忽略不进行复制的DB或table。
前文已经有二进制多实例主从复制的文档,本文档不再从新开启主从复制,如果没有开启请点击:二进制主从复制
在做过滤复制前请保证开启了主从同步
[root@mysql-manager ~]# echo 'replicate_do_db=olda' >> /opstands/mysql-3308/my.cnf
# -----------------------------------------------------
# replicate_ignore_db是黑名单,用法和白名单一样
[root@mysql-manager ~]# systemctl restart mysqld-3308.service
[root@mysql-manager ~]# mysql -S /opstands/mysql-3308/mysql.sock -e "show slave status\G" |egrep 'Replicate_Do_DB:|Replicate_Ignore_DB:'
Replicate_Do_DB: olda
Replicate_Ignore_DB:
现在主从复制只会同步olda库的数据
1、master更新数据时,会在事务前产生GTID,一同记录到binlog日志中。
2、slave端的i/o 线程将变更的binlog,写入到本地的relay log中。
3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。
4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会从relay log中执行该GTID的事务,并记录到binlog。
6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
GTID模式主从复制先决条件
前文已经有二进制多实例安装的文档,本文档不再从新安装,如果没有安装请点击:二进制多实例安装
[root@mysql-manager ~]# mkdir -p /opstands/mysql-5.7.20/binlog
本文将使用MySQL-3306实例为主节点,MySQL3309实例为从节点。
[root@mysql-manager ~]# sed -i '7a log_bin=/opstands/mysql-5.7.20/binlog/mysql-bin' /etc/my.cnf
[root@mysql-manager ~]# sed -i '8a binlog_format=row' /etc/my.cnf
[root@mysql-manager ~]# sed -i '9a sync_binlog=1' /etc/my.cnf
[root@mysql-manager ~]# sed -i '10a gtid_mode=ON' /etc/my.cnf
[root@mysql-manager ~]# sed -i '11a enforce_gtid_consistency' /etc/my.cnf
[root@mysql-manager ~]# cat >> /opstands/mysql-3309/my.cnf <<EOF
gtid_mode=ON
enforce_gtid_consistency
EOF
[root@mysql-manager ~]# chown -R mysql.mysql /opstands/mysql-*
[root@mysql-manager ~]# systemctl restart mysqld.service
[root@mysql-manager ~]# systemctl restart mysqld-3309.service
[root@mysql-manager ~]# mysql -uroot -predhat -e "show variables like '%gtid_%';" mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | <------- 是否开启GTID | gtid_executed_compression_period | 1000 | | gtid_mode | ON | <-------是否启用GTID模块 | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+ [root@mysql-manager ~]# mysql -S /opstands/mysql-3309/mysql.sock -e "show variables like '%gtid_%';" +----------------------------------+-----------+ | Variable_name | Value | +----------------------------------+-----------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | <------- 是否开启GTID | gtid_executed_compression_period | 1000 | | gtid_mode | ON | <-------是否启用GTID模块 | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | | | session_track_gtids | OFF | +----------------------------------+-----------+
[root@mysql-manager ~]# mysql -uroot -predhat -e "grant replication slave on *.* to rep@'%' identified by 'redhat';"
# 进入3309实例数据库内开启GTID连接到3306实例
[root@mysql-manager ~]# mysql -S /opstands/mysql-3309/mysql.sock
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.18.1.79',
-> MASTER_USER='rep',
-> MASTER_PASSWORD='redhat',
-> MASTER_PORT=3306,
-> MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
[root@mysql-manager mysql-3309]# mysql -S /opstands/mysql-3309/mysql.sock -e "show slave status\G;" |egrep 'Slave_IO_Running:|Slave_SQL_Running:|Seconds_Behind_Master:'
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
可以看到当前两个实例都没有olda库,接下来在3306库创建olda库,然后查看3309库是否有同步。
[root@mysql-manager ~]# mysql -uroot -predhat -e "show databases;" mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ [root@mysql-manager mysql-3309]# mysql -S /opstands/mysql-3309/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
可以看到3309实例从库也同步创建了olda库
[root@mysql-manager ~]# mysql -uroot -predhat -e "create database olda charset utf8mb4;" mysql: [Warning] Using a password on the command line interface can be insecure. [root@mysql-manager ~]# mysql -uroot -predhat -e "show databases;" mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | olda | | performance_schema | | sys | +--------------------+ [root@mysql-manager mysql-3309]# mysql -S /opstands/mysql-3309/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | olda | | performance_schema | | sys | +--------------------+ sys | +--------------------+ [root@mysql-manager mysql-3309]# mysql -S /opstands/mysql-3309/mysql.sock -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | olda | | performance_schema | | sys | +--------------------+
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。