详解MySQL主从复制实战 - 基于GTID的复制
基于gtid的复制
简介
基于gtid的复制是mysql 5.6后新增的复制方式.
gtid (global transaction identifier) 即全局事务id, 保证了在每个在主库上提交的事务在集群中有一个唯一的id.
在原来基于日志的复制中, 从库需要告知主库要从哪个偏移量进行增量同步, 如果指定错误会造成数据的遗漏, 从而造成数据的不一致.
而基于gtid的复制中, 从库会告知主库已经执行的事务的gtid的值, 然后主库会将所有未执行的事务的gtid的列表返回给从库. 并且可以保证同一个事务只在指定的从库执行一次.
实战
1、在主库上建立复制账户并授予权限
基于gtid的复制会自动地将没有在从库执行的事务重放, 所以不要在其他从库上建立相同的账号. 如果建立了相同的账户, 有可能造成复制链路的错误.
mysql> create user 'repl'@'172.%' identified by '123456';
注意在生产上的密码必须依照相关规范以达到一定的密码强度, 并且规定在从库上的特定网段上才能访问主库.
mysql> grant replication slave on *.* to 'repl'@'172.%';
查看用户
mysql> select user, host from mysql.user; +-----------+-----------+ | user | host | +-----------+-----------+ | prontera | % | | root | % | | mysql.sys | localhost | | root | localhost | +-----------+-----------+ 4 rows in set (0.00 sec)
查看授权
mysql> show grants for repl@'172.%'; +--------------------------------------------------+ | grants for repl@172.% | +--------------------------------------------------+ | grant replication slave on *.* to 'repl'@'172.%' | +--------------------------------------------------+ 1 row in set (0.00 sec)
2、配置主库服务器
[mysqld] log_bin = /var/log/mysql/mysql-bin log_bin_index = /var/log/mysql/mysql-bin.index binlog_format = row server_id = 101 gtid_mode = on enforce_gtid_consistency = on #log_slave_updates = on
note: 把日志与数据分开是个好习惯, 最好能放到不同的数据分区
enforce_gtid_consistency 强制gtid一致性, 启用后以下命令无法再使用
create table ... select ...
mysql> create table dept select * from departments; error 1786 (hy000): statement violates gtid consistency: create table ... select.
因为实际上是两个独立事件, 所以只能将其拆分先建立表, 然后再把数据插入到表中
create temporary table
事务内部不能创建临时表
mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> create temporary table dept(id int); error 1787 (hy000): statement violates gtid consistency: create temporary table and drop temporary table can only be executed outside transactional context. these statements are also not allowed in a function or trigger because functions and triggers are also considered to be multi-statement transactions.
同一事务中更新事务表与非事务表(myisam)
mysql> create table `dept_innodb` (id int(11) unsigned not null primary key auto_increment); query ok, 0 rows affected (0.04 sec) mysql> create table `dept_myisam` (id int(11) unsigned not null primary key auto_increment) engine = `myisam`; query ok, 0 rows affected (0.03 sec) mysql> begin; query ok, 0 rows affected (0.00 sec) mysql> insert into dept_innodb(id) value(1); query ok, 1 row affected (0.00 sec) mysql> insert into dept_myisam(id) value(1); error 1785 (hy000): statement violates gtid consistency: updates to non-transactional tables can only be done in either autocommitted statements or single-statement transactions, and never in the same statement as updates to transactional tables.
所以建议选择innodb作为默认的数据库引擎.
log_slave_updates 该选项在mysql 5.6版本时基于gtid的复制是必须的, 但是其增大了从服务器的io负载, 而在mysql 5.7中该选项已经不是必须项
3、配置从库服务器
master_info_repository 与relay_log_info_repository
在mysql 5.6.2之前, slave记录的master信息以及slave应用binlog的信息存放在文件中, 即master.info与relay-log.info. 在5.6.2版本之后, 允许记录到table中. 对应的表分别为mysql.slave_master_info与mysql.slave_relay_log_info, 且这两个表均为innodb引擎表.
[mysqld] log_bin = /var/log/mysql/mysql-bin log_bin_index = /var/log/mysql/mysql-bin.index server_id = 102 # slaves relay_log = /var/log/mysql/relay-bin relay_log_index = /var/log/mysql/relay-bin.index relay_log_info_file = /var/log/mysql/relay-bin.info enforce_gtid_consistency = on log_slave_updates = on read_only = on master_info_repository = table relay_log_info_repository = table
4、从库数据初始化 - [optional]
先在主库上备份数据
mysqldump --single-transaction --master-data=2 --triggers --routines --all-databases --events -u root -p > backup.sql
—master-data=2 该选项将当前服务器的binlog的位置和文件名追加到输出文件中(show master status). 如果为1, 将偏移量拼接到change master 命令. 如果为2, 输出的偏移量信息将会被注释。
--all-databases 因为基于gtid的复制会记录全部的事务, 所以要构建一个完整的dump这个选项是推荐的
常见错误
当从库导入sql的时候出现
error 1840 (hy000) at line 24: @@global.gtid_purged can only be set when @@global.gtid_executed is empty.
此时进入从库的mysql command line, 使用reset master即可
5、启动基于gtid的复制
现有master@172.20.0.2和slave@172.20.0.3, 并且已经通过mysqldump将数据同步至从库slave中. 现在在从服务器slave上配置复制链路
mysql> change master to master_host='master', master_user='repl', master_password='123456', master_auto_position=1; query ok, 0 rows affected, 2 warnings (0.06 sec)
启动复制
mysql> start slave;
启动成功后查看slave的状态
mysql> show slave status\g *************************** 1. row *************************** slave_io_state: queueing master event to the relay log master_host: master master_user: repl master_port: 3306 connect_retry: 60 master_log_file: mysql-bin.000002 read_master_log_pos: 12793692 relay_log_file: relay-bin.000002 relay_log_pos: 1027 relay_master_log_file: mysql-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: 814 relay_log_space: 12794106 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: 5096 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: 101 master_uuid: a9fd4765-ec70-11e6-b543-0242ac140002 master_info_file: mysql.slave_master_info sql_delay: 0 sql_remaining_delay: null slave_sql_running_state: reading event from the relay log master_retry_count: 86400 master_bind: last_io_error_timestamp: last_sql_error_timestamp: master_ssl_crl: master_ssl_crlpath: retrieved_gtid_set: a9fd4765-ec70-11e6-b543-0242ac140002:1-39 executed_gtid_set: a9fd4765-ec70-11e6-b543-0242ac140002:1-4 auto_position: 1 replicate_rewrite_db: channel_name: master_tls_version: 1 row in set (0.00 sec)
当slave_io_running, slave_sql_running为yes,
且slave_sql_running_state 为slave has read all relay log; waiting for more updates时表示成功构建复制链路
6、总结
优点
- 因为不用手工设置日志偏移量, 可以很方便地进行故障转移
- 如果启用log_slave_updates那么从库不会丢失主库上的任何修改
缺点
- 对执行的sql有一定限制
- 仅支持mysql 5.6之后的版本, 而且不建议使用早期5.6版本
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: MVC异步分页代码分享