一 异步复制(Asynchronous replication)

1.MariaDB本身支持单向的、异步的复制。异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时,最重要的是这意味着当应用系统的事务提交已经确认时数据并不能在同一时刻拷贝,应用到从机。通常这个延时是由网络带宽、资源可用性和系统负载决定的。然而,使用正确的组件并且调优,复制能做到接近瞬时完成。

2.MariaDB默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题,主如果crash掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。

二 实验过程

1.时间要进行同步

2.注意复制的开始位置
 从0开始
 从备份中恢复到从节点后启动的复制,复制的起始点备份操作时主节点所处的日志文件及其实践位置

3.配置文件
主节点:

  1. [root@node1 ~]# vim /etc/my.cnf.d/server.cnf
  2. skip_name_resolve=ON
  3. innodb_file_per_table=ON
  4. server_id=1
  5. log_bin=log-bin

从节点:

  1. [root@node2 ~]# vim /etc/my.cnf.d/server.cnf/etc/my.cnf.d/server.cnf
  2. skip_name_resolve=ON
  3. innodb_file_per_table=ON
  4. server_id=2
  5. relay_log=relay-log

4.启动服务,并进入MariaDB

  1. [root@node1 ~]# systemctl start mariadb.service
  2. [root@node2 ~]# systemctl start mariadb.service

5.对数据库库进行操作
在主节点上创建用户并授权

  1. MariaDB [(none)]> CREATE USER 'repluser'@'172.18.67.%' IDENTIFIED BY 'replpass';
  2. MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.18.67.%' IDENTIFIED BY 'replpass';
  3. MariaDB [(none)]> FLUSH PRIVILEGES;
  4. MariaDB [(none)]> SHOW MASTER STATUS;
  5. +----------------+----------+--------------+------------------+
  6. | File           | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  7. +----------------+----------+--------------+------------------+
  8. | log-bin.000001 |      497 |              |                  |
  9. +----------------+----------+--------------+------------------+

为从节点关联主节点

  1. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.18.67.11',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='log-bin.000001',MASTER_LOG_POS=497;

查看从节点状态

  1. MariaDB [(none)]> SHOW SLAVE STATUS\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State:
  4.                   Master_Host: 172.18.67.11
  5.                   Master_User: repluser
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: log-bin.000001
  9.           Read_Master_Log_Pos: 497
  10.                Relay_Log_File: relay-log.000001
  11.                 Relay_Log_Pos: 4
  12.         Relay_Master_Log_File: mysql-bin.000001
  13.              Slave_IO_Running: No
  14.             Slave_SQL_Running: No
  15.               Replicate_Do_DB:
  16.           Replicate_Ignore_DB:
  17.            Replicate_Do_Table:
  18.        Replicate_Ignore_Table:
  19.       Replicate_Wild_Do_Table:
  20.   Replicate_Wild_Ignore_Table:
  21.                    Last_Errno: 0
  22.                    Last_Error:
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 497
  25.               Relay_Log_Space: 245
  26.               Until_Condition: None
  27.                Until_Log_File:
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File:
  31.            Master_SSL_CA_Path:
  32.               Master_SSL_Cert:
  33.             Master_SSL_Cipher:
  34.                Master_SSL_Key:
  35.         Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error:
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error:
  41.   Replicate_Ignore_Server_Ids:
  42.              Master_Server_Id: 0
  43. 1 row in set (0.00 sec)

开启IO和SQL线程

  1. MariaDB [(none)]> HELP START SLAVE;
  2. MariaDB [(none)]> HELP STOP SLAVE;
  3. MariaDB [(none)]> START SLAVE IO_THREAD;
  4. MariaDB [(none)]> START SLAVE SQL_THREAD;
  5. MariaDB [(none)]> SHOW SLAVE STATUS\G;
  6. *************************** 1. row ***************************
  7.                Slave_IO_State:
  8.                   Master_Host: 172.18.67.11
  9.                   Master_User: repluser
  10.                   Master_Port: 3306
  11.                 Connect_Retry: 60
  12.               Master_Log_File: log-bin.000001
  13.           Read_Master_Log_Pos: 497
  14.                Relay_Log_File: relay-log.000001
  15.                 Relay_Log_Pos: 4
  16.         Relay_Master_Log_File: mysql-bin.000001
  17.              Slave_IO_Running: Yes
  18.             Slave_SQL_Running: Yes
  19.               Replicate_Do_DB:
  20.           Replicate_Ignore_DB:
  21.            Replicate_Do_Table:
  22.        Replicate_Ignore_Table:
  23.       Replicate_Wild_Do_Table:
  24.   Replicate_Wild_Ignore_Table:
  25.                    Last_Errno: 0
  26.                    Last_Error:
  27.                  Skip_Counter: 0
  28.           Exec_Master_Log_Pos: 497
  29.               Relay_Log_Space: 245
  30.               Until_Condition: None
  31.                Until_Log_File:
  32.                 Until_Log_Pos: 0
  33.            Master_SSL_Allowed: No
  34.            Master_SSL_CA_File:
  35.            Master_SSL_CA_Path:
  36.               Master_SSL_Cert:
  37.             Master_SSL_Cipher:
  38.                Master_SSL_Key:
  39.         Seconds_Behind_Master: NULL
  40. Master_SSL_Verify_Server_Cert: No
  41.                 Last_IO_Errno: 0
  42.                 Last_IO_Error:
  43.                Last_SQL_Errno: 0
  44.                Last_SQL_Error:
  45.   Replicate_Ignore_Server_Ids:
  46.              Master_Server_Id: 0
  47. 1 row in set (0.00 sec)

查看从节点状态,IO和SQL线程已经启动:Slave_IO_Running: Yes,Slave_SQL_Running: Yes
在主节点上创建数据库mydb

  1. MariaDB [(none)]> CREATE DATABASE mydb CHARACTER SET 'utf8';
  2. MariaDB [(none)]> SHOW MASTER STATUS;
  3. +----------------+----------+--------------+------------------+
  4. | File           | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  5. +----------------+----------+--------------+------------------+
  6. | log-bin.000001 |      601 |              |                  |
  7. +----------------+----------+--------------+------------------+
  8. MariaDB [(none)]> SHOW DATABASES;
  9. +--------------------+
  10. | Database           |
  11. +--------------------+
  12. | information_schema |
  13. | mydb               |
  14. | mysql              |
  15. | performance_schema |
  16. | test               |
  17. +--------------------+

查看从节点的状态

  1. MariaDB [(none)]> SHOW SLAVE STATUS\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 172.18.67.11
  5.                   Master_User: repluser
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: log-bin.000001
  9.           Read_Master_Log_Pos: 601
  10.                Relay_Log_File: relay-log.000002
  11.                 Relay_Log_Pos: 631
  12.         Relay_Master_Log_File: log-bin.000001
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB:
  16.           Replicate_Ignore_DB:
  17.            Replicate_Do_Table:
  18.        Replicate_Ignore_Table:
  19.       Replicate_Wild_Do_Table:
  20.   Replicate_Wild_Ignore_Table:
  21.                    Last_Errno: 0
  22.                    Last_Error:
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 601
  25.               Relay_Log_Space: 919
  26.               Until_Condition: None
  27.                Until_Log_File:
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File:
  31.            Master_SSL_CA_Path:
  32.               Master_SSL_Cert:
  33.             Master_SSL_Cipher:
  34.                Master_SSL_Key:
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error:
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error:
  41.   Replicate_Ignore_Server_Ids:
  42.              Master_Server_Id: 1
  43. 1 row in set (0.00 sec)

此时我们可以从从节点上观察到Read_Master_Log_Pos由497变为了631。为了使实验更加深入,准备了一个数据库,我们将其导入到主节点

  1. [root@node1 ~]# mysql < hellodb.sql
  2. MariaDB [(none)]> SHOW MASTER STATUS;
  3. +----------------+----------+--------------+------------------+
  4. | File           | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  5. +----------------+----------+--------------+------------------+
  6. | log-bin.000001 |     8011 |              |                  |
  7. +----------------+----------+--------------+------------------+

再次查看从节点状态

  1. MariaDB [(none)]> SHOW SLAVE STATUS\G;
  2. *************************** 1. row ***************************
  3.                Slave_IO_State: Waiting for master to send event
  4.                   Master_Host: 172.18.67.11
  5.                   Master_User: repluser
  6.                   Master_Port: 3306
  7.                 Connect_Retry: 60
  8.               Master_Log_File: log-bin.000001
  9.           Read_Master_Log_Pos: 8011
  10.                Relay_Log_File: relay-log.000002
  11.                 Relay_Log_Pos: 8041
  12.         Relay_Master_Log_File: log-bin.000001
  13.              Slave_IO_Running: Yes
  14.             Slave_SQL_Running: Yes
  15.               Replicate_Do_DB:
  16.           Replicate_Ignore_DB:
  17.            Replicate_Do_Table:
  18.        Replicate_Ignore_Table:
  19.       Replicate_Wild_Do_Table:
  20.   Replicate_Wild_Ignore_Table:
  21.                    Last_Errno: 0
  22.                    Last_Error:
  23.                  Skip_Counter: 0
  24.           Exec_Master_Log_Pos: 8011
  25.               Relay_Log_Space: 8329
  26.               Until_Condition: None
  27.                Until_Log_File:
  28.                 Until_Log_Pos: 0
  29.            Master_SSL_Allowed: No
  30.            Master_SSL_CA_File:
  31.            Master_SSL_CA_Path:
  32.               Master_SSL_Cert:
  33.             Master_SSL_Cipher:
  34.                Master_SSL_Key:
  35.         Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37.                 Last_IO_Errno: 0
  38.                 Last_IO_Error:
  39.                Last_SQL_Errno: 0
  40.                Last_SQL_Error:
  41.   Replicate_Ignore_Server_Ids:
  42.              Master_Server_Id: 1
  43. 1 row in set (0.00 sec)
  44. MariaDB [(none)]> use hellodb;
  45. MariaDB [hellodb]> SHOW TABLES;
  46. +-------------------+
  47. | Tables_in_hellodb |
  48. +-------------------+
  49. | classes           |
  50. | coc               |
  51. | courses           |
  52. | scores            |
  53. | students          |
  54. | teachers          |
  55. | toc               |
  56. +-------------------+
  57. MariaDB [hellodb]> SELECT * FROM students;
  58. +-------+---------------+-----+--------+---------+-----------+
  59. | StuID | Name          | Age | Gender | ClassID | TeacherID |
  60. +-------+---------------+-----+--------+---------+-----------+
  61. |     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
  62. |     2 | Shi Potian    |  22 | M      |       1 |         7 |
  63. |     3 | Xie Yanke     |  53 | M      |       2 |        16 |
  64. |     4 | Ding Dian     |  32 | M      |       4 |         4 |
  65. |     5 | Yu Yutong     |  26 | M      |       3 |         1 |
  66. |     6 | Shi Qing      |  46 | M      |       5 |      NULL |
  67. |     7 | Xi Ren        |  19 | F      |       3 |      NULL |
  68. |     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
  69. |     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
  70. |    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
  71. |    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
  72. |    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
  73. |    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
  74. |    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
  75. |    15 | Duan Yu       |  19 | M      |       4 |      NULL |
  76. |    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
  77. |    17 | Lin Chong     |  25 | M      |       4 |      NULL |
  78. |    18 | Hua Rong      |  23 | M      |       7 |      NULL |
  79. |    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
  80. |    20 | Diao Chan     |  19 | F      |       7 |      NULL |
  81. |    21 | Huang Yueying |  22 | F      |       6 |      NULL |
  82. |    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
  83. |    23 | Ma Chao       |  23 | M      |       4 |      NULL |
  84. |    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
  85. |    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
  86. +-------+---------------+-----+--------+---------+-----------+

我们观察到主节点的数据发生变化,从节点的数据库都会同步。

三 复制时应该注意的问题

1.从服务设定为“只读”
在从服务器启动read_only,但仅对非SUPER权限的用户有效
阻止所有用户:MariaDB [(none)]> FLUSH TABLES WITH READ LOCK

2.尽量确保复制时的事务安全
在master节点启用参数:
 sync_binlog = ON
如果用到的是InnoDB存储引擎:
 innodb_flush_logs_at_trx_commit=ON
 innodb_support_xa=ON
3.从服务器意外中止时尽量避免自动启动复制线程

MariaDB主从异步复制详解的更多相关文章

  1. MySQL异步复制、半同步复制详解

    MySQL数据复制的原理图大致如下: 从上图我们可以看出MySQL数据库的复制需要启动三个线程来实现: 其中1个在主服务器上,另两个在从服务器上.当发出START SLAVE时,从服务器创建一个I/O ...

  2. mysql互为主从实战设置详解及自动化备份(Centos7.2)

    mysql互为主从实战设置详解(Centos7.2) 第一步:mysql配置  my.cnf配置 服务器1 (10.89.10.90) [mysqld]  server-id=1  log-bin=/ ...

  3. MariaDB主从半同步复制详解

    半同步复制(Semisynchronous replication) 介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到relay lo ...

  4. MySQL数据的主从复制、半同步复制和主主复制详解

    一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费 ...

  5. MySQL数据的主从复制、半同步复制和主主复制详解-转

    一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费 ...

  6. mysql的主主复制详解

    Mysql双主部署 解释: 所谓双主备份,其实也就是互做主从复制,每台master既是master,又是另一台服务器的slave.这样,任何一方所做的变更,都会通过复制应用到另外一方的数据库中. 要求 ...

  7. 基于 CentOS Mysql 安装与主从同步配置详解

    CentOS Mysql 安装 Mysql (Master/Slave) 主从同步 1.为什么要使用主从同步 1.如果主服务器出现问题,可以快速切换到从服务器提供的服务 2.可以在从服务器上执行查询操 ...

  8. MYSQL 复制详解

    MySql 复制介绍 MySQL复制允许将主实例(master)上的数据同步到一个或多个从实例(slave)上,默认情况 下复制是异步进行的,从库也不需要一直连接到主库来同步数据 MySQL复制的数据 ...

  9. Linux的fasync驱动异步通知详解【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/51851338 版权声明:本文为博主原创文章,未经博主允许不得转载. 工作项目用有 ...

随机推荐

  1. c# 系统校时工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HuaT ...

  2. c# 文件日志处理 需要log4net配置

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  3. Shell 命令行统计 apache 网站日志访问IP以及IP归属地

    Shell 命令行统计 apache 网站日志访问IP以及IP归属地 我的一个站点用 apache 服务跑着,积攒了很多的日志.我想用 shell 看看有哪些人访问过我的站点,并且他来自哪里. 因为日 ...

  4. 【剑指offer】 和为s的连续正数序列,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 设置两个辅助变量small和big,small表示序列的最小值,big表示序列的最大值.如果sum(small ~ big) > s,则增大sma ...

  5. Eclipse远程调试Java程序

    1. 在服务器上运行jar包时加入参数 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address= 2. 在Eclipse中操作 ...

  6. 10day2

    最多因子数 搜索 [问题描述] 数学家们喜欢各种类型的有奇怪特性的数.例如,他们认为 945 是一个有趣的数,因为它是第一个所有约数之和大于本身的奇数. 为了帮助他们寻找有趣的数,你将写一个程序扫描一 ...

  7. HTML中name,value属性区分

    name和value区分(注重前后台的结合),个人笔记使用^_^ 前台 后台对于单选框来说,name是一样的,决定后台的键名,value的值决定了后台键值 复选框同理,但name应该是不同的

  8. js 字符串和数组注意点

    var a="foo"; var b=[ "f","o","o"]; a[1]="o"; b[1]= ...

  9. 二次剩余-Cipolla

    二次剩余 \(Cipolla\) 算法 概述 大概就是在模 \(p\) 意义下开根号,如求解方程\(x^2\equiv n(mod\ p)\). 这里只考虑 \(p\) 为素数的情况.若 \(p=2\ ...

  10. 20179223《Linux内核原理与分析》第二周学习笔记

    第二周实验 本周学习情况: 学习了X86 cpu的几个寄存器及X86汇编指令: movl %eax,%edx edx=eax %表示一个寄存器,把eax内容放入edx,等号相当于把eax赋值给edx, ...