首页 > 数据库 > MySQL > 正文

MySQL5.7基于无损复制搭建主从详解

2024-07-24 12:36:14
字体:
来源:转载
供稿:网友
     * MySQL Replication默认都是异步(asynchronous),当主库在执行完一些事务后,是不会管备库的进度的。如果备库不幸落后,而更不幸的是主库此时又出现Crash(例如宕机),这时备库中的数据就是不完整的。简而言之,在主库发生故障的时候,我们无法使用备库来继续提供数据一致的服务了。
 
     * Semi sync Replication(半同步复制)是在master上提交完成后,再传送到slave等待ack应答,仅仅在一定情况下事务的已经传递到一个slave上,但是并不确保已经在备库上执行完成,会造成最后一次events的主备不一致。
 
     * lossless replication(无损复制)是在master提前过程中,传送到slave中等待应答。当至少一个slave request bilog后写入到relay-log并flush disk,就返回ack
 
  2. lossless replication传输过程
 
  3. 环境
 
     * 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';
  +---------------+------------+

  [mysqld]
  ########basic settings########
  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
  # slave上开启只读,避免应用误写导致主从数据不一致
  read_only = on
  super_read_only = on
  #######replication settings########
  master_info_repository = TABLE
  relay_log_info_repository = TABLE
  log_bin = bin.log
  sync_binlog = 1
  log_slave_updates
  binlog_format = row
  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
  4. 搭建无数据基于无损全复制主从 [ master原来无数据 ]
 
     * 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)
     * master服务器上查看binlog文件名和日志位置
 
  mysql> show master status;
  +------------+----------+--------------+------------------+-------------------+
  | File       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  +------------+----------+--------------+------------------+-------------------+
  | bin.000002 |      689 |              |                  |                   |
  +------------+----------+--------------+------------------+-------------------+
  mysql> insert into mytest.users select null, 'test', 'M', 42;
  Query OK, 1 row affected (0.01 sec)
  Records: 1  Duplicates: 0  Warnings: 0
 
  mysql> update mytest.users set name='seasea'  where id = 3;
  Query OK, 1 row affected (0.01 sec)
  Rows matched: 1  Changed: 1  Warnings: 0
 
  mysql> select * from mytest.users;
  +----+--------+-----+-----+
  | id | name   | sex | age |
  +----+--------+-----+-----+
  |  1 | tom    | M   |  24 |
  |  2 | jak    | F   |  32 |
  |  3 | seasea | M   |  35 |
  |  4 | lisea  | M   |  29 |
  |  5 | test   | M   |  42 |
  +----+--------+-----+-----+
  5 rows in set (0.00 sec)
     * slave上查看
 
  mysql> select * from mytest.users;
  +----+--------+-----+-----+
  | id | name   | sex | age |
  +----+--------+-----+-----+
  |  1 | tom    | M   |  24 |
  |  2 | jak    | F   |  32 |
  |  3 | seasea | M   |  35 |
  |  4 | lisea  | M   |  29 |
  |  5 | test   | M   |  42 |
  +----+--------+-----+-----+
  5 rows in set (0.00 sec)
  6. 总结
 
  以需求驱动技术,技术本身没有优略之分,只有业务之分。

(编辑:武林网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表