符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
下文给大家带来有关MySQL中基于GTID半同步如何搭建主从内容,相信大家一定看过类似的文章。我们给大家带来的有何不同呢?一起来看看正文部分吧,相信看完MySQL中基于GTID半同步如何搭建主从你一定会有所收获。
宝塔ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!1. 背景
* GTID: 全局事物ID(Global Transaction ID),在整个事务架构中每一个事务ID号是全局唯一的,不止是在一个节点上而是整个主从复制架构中每任何两个事务的ID号都不会相同。
* GTID就是由当前节点的UUID(一个128位的随机数)和为当前节点生成的自增数(TID)组成的。
* GTID在分布式架构中可以保证数据的一致性。从而也实现了mysql的高可用性。
* MySQL 5.6开始支持。 GTID在复制中代替原有的binlog file和file posistion复制定位。
2. 环境
* Master云服务器环境
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.7.18-log | +---------------+------------+ 1 row in set (0.00 sec)
* Slave云服务器环境
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.7.18-log | +---------------+------------+ 1 row in set (0.00 sec)
* Master my.cnf配置文件
[mysqld] ########basic settings######## # 主从server-id一定要设置不同 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######## # 开启 gtid gtid_mode = on # 强制gtid的一致性 enforce-gtid-consistency = true master_info_repository = TABLE relay_log_info_repository = TABLE # MySQL复制是基于binlog日志的 log_bin = bin.log sync_binlog = 1 log_slave_updates # MySQL binlog格式搭建主从时必须设置为row binlog_format = row relay_log = relay.log relay_log_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" # 开启master semi sync replication loose_rpl_semi_sync_master_enabled = 1 # 开启slave semi sync replication loose_rpl_semi_sync_slave_enabled = 1 # 等待5秒无ack应答自动切换为异步模式 loose_rpl_semi_sync_master_timeout = 5000
* Slave my.cnf配置文件
[mysqld] ########basic settings######## gtid_mode = on enforce-gtid-consistency = true 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 #######replication settings######## master_info_repository = TABLE relay_log_info_repository = TABLE log_bin = bin.log sync_binlog = 1 log_slave_updates binlog_format = row # slave上开启只读,避免应用误写导致主从数据不一致 read_only = on 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 rpl_semi_sync_master_wait_point = AFTER_SYNC rpl_semi_sync_master_wait_for_slave_count = 1
3. 搭建基于GTID半同步复制主从
* master 创建复制所使用的用户 [ 此处ip设置为slave服务IP或者% ]
mysql> grant replication slave on *.* to 'rpl'@'172.18.4.1' identified by '123'; Query OK, 0 rows affected, 1 warning (0.00 sec)
* slave云服务器上配置连接master信息
未开启slave服务时,Slave_IO_Running与Slave_SQL_Running状态成No
mysql> show slave status; # 未开启复制功能时,slave状态是空的 Empty set (0.00 sec) mysql> change master to master_host='172.18.0.1',master_user='rpl',master_password='123',master_auto_position=1; Query OK, 0 rows affected, 2 warnings (0.02 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: Read_Master_Log_Pos: 4 Relay_Log_File: relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: 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: 0 Relay_Log_Space: 154 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: e5b2d96a-7047-11e7-b39c-00163e028c02:1 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
* 开启slave服务,并查看状态
正常开启slave服务后,Slave_IO_Running与Slave_SQL_Running状态成Yes
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.000002 Read_Master_Log_Pos: 689 Relay_Log_File: relay.000002 Relay_Log_Pos: 890 Relay_Master_Log_File: bin.000002 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: 689 Relay_Log_Space: 1087 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: aaa45482-7047-11e7-a7b3-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 more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: aaa45482-7047-11e7-a7b3-00163e0432c5:1-2 Executed_Gtid_Set: aaa45482-7047-11e7-a7b3-00163e0432c5:1-2, e5b2d96a-7047-11e7-b39c-00163e028c02:1 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
* master 查看slave连接信息
mysql> show slave hosts; +-----------+------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+------+------+-----------+--------------------------------------+ | 210 | | 3306 | 110 | e5b2d96a-7047-11e7-b39c-00163e028c02 | +-----------+------+------+-----------+--------------------------------------+ 1 row in set (0.00 sec)
* Master上操作创建数据库与表,并插入数据
mysql> create database mytest character set utf8mb4; Query OK, 1 row affected (0.02 sec) mysql> use mytest; Database changed mysql> create table a(data INT PRIMARY KEY NOT NULL AUTO_INCREMENT)ENGINE=INNODB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected (0.02 sec) mysql> insert into a select null; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into a select null; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into a select null; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from a; +------+ | data | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
* slave上查看
mysql> select * from mytest.a; +------+ | data | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
4. slave误写,gtid错误解决
* master数据查看
mysql> select * from mytest.a; +------+ | data | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
* slave数据查看
mysql> select * from mytest.a; +------+ | data | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
* slave数据误写
mysql> insert into mytest.a select null; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from mytest.a; +------+ | data | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec)
* master相同数据插入
mysql> insert into a select null; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from a; +------+ | data | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec)
* slave云服务器状态查看 报1062错误,SQL线程停止工作,由于开启gtid不能设置跳过错误
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.000002 Read_Master_Log_Pos: 5768 Relay_Log_File: relay.000005 Relay_Log_Pos: 1354 Relay_Master_Log_File: bin.000002 Slave_IO_Running: Yes 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: 1062 Last_Error: Could not execute Write_rows event on table mytest.a; Duplicate entry '4' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log bin.000002, end_log_pos 5737 Skip_Counter: 0 Exec_Master_Log_Pos: 5512 Relay_Log_Space: 2601 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: 1062 Last_SQL_Error: Could not execute Write_rows event on table mytest.a; Duplicate entry '4' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log bin.000002, end_log_pos 5737 Replicate_Ignore_Server_Ids: Master_Server_Id: 110 Master_UUID: aaa45482-7047-11e7-a7b3-00163e0432c5 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: 170724 17:15:51 Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: aaa45482-7047-11e7-a7b3-00163e0432c5:10-23 Executed_Gtid_Set: aaa45482-7047-11e7-a7b3-00163e0432c5:1-22, e5b2d96a-7047-11e7-b39c-00163e028c02:1-5 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
* slave云服务器停止复制
mysql> stop slave; Query OK, 0 rows affected (0.00 sec)
* 设置gtid下一次执行 通过Retrieved_Gtid_Set和Executed_Gtid_Set得到未执行的gtid
mysql> set gtid_next = 'aaa45482-7047-11e7-a7b3-00163e0432c5:23'; Query OK, 0 rows affected (0.00 sec)
* 执行空事物
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec)
* 设置gtid下一次自动寻找gtid事物
mysql> set gtid_next = 'automatic'; Query OK, 0 rows affected (0.00 sec)
* slave云服务器开启复制
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
* slave云服务器查看状态 SQL线程正常开启工作
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.000002 Read_Master_Log_Pos: 5768 Relay_Log_File: relay.000006 Relay_Log_Pos: 436 Relay_Master_Log_File: bin.000002 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: 5768 Relay_Log_Space: 2089 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: aaa45482-7047-11e7-a7b3-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 more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: aaa45482-7047-11e7-a7b3-00163e0432c5:10-23 Executed_Gtid_Set: aaa45482-7047-11e7-a7b3-00163e0432c5:1-23, e5b2d96a-7047-11e7-b39c-00163e028c02:1-5 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
5. 总结
以需求驱动技术,技术本身没有优略之分,只有业务之分。
对于上文关于MySQL中基于GTID半同步如何搭建主从,大家觉得是自己想要的吗?如果想要了解更多相关,可以继续关注我们的行业资讯板块。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。