大纲
一、前言
二、Mysql 基础知识
三、Mysql 复制(Replication)
四、Mysql 复制(Replication)类型
五、Mysql 主从复制基本步骤
六、Mysql 主从复制(异步)
七、Mysql 主从复制(半同步)
八、Mysql 复制工具
九、Mysql 复制注意事项
十、Mysql 复制过滤
一、前言
从这一篇博客开始我们就来学习mysql的高级课程,在前面的几篇博客我们讲解了mysql基础知识、mysql日志类型、mysql配置文件、mysql备份策略,这一篇博客中我们来讲解mysql的复制。
二、mysql基础知识
1.mysql日志类型
- 二进制日志
- 事务日志
- 错误日志
- 一般查询日志
- 中继日志
- 慢查询日志
注,有博友对mysql日志不怎么了解,可以参考这篇博客http://freeloda.blog.51cto.com/2033581/1253991
2.mysql二进制日志详解
说明:默认开启,精确的记录了用户对数据库中的数据进行操作的命令和操作的数据对象。
二进制日志文件的作用:
- 提供了增量备份的功能
- 提供了数据基于时间点的恢复,这个恢复的时间点可以由用户控制
- 为mysql的复制架构提供基础,将这主服务器的二进制日志复制到从服务器上并执行同样的操作,就可将数据进行同步
- 实现数据的高可用
- 分担负载
二进制日志默认存放位置:
- 数据目录下,以mysql-bin.XXXXXX命名的日志
二进制日志格式:
- 基于语句 statement
- 基于行 row
- 混合方式 mixed
二进制日志滚动方式:
- 重启服务
- 日志达到最大上限
- 执行flush logs
二进制日志事件:
- position 基于位置,也就是offset(偏移量)
- datetime 基于时间
- timestamp
二进制日志的查看与删除方式:
1 2 3 4 5 |
|
文件系统中查看二进制日志的命令:
1 2 3 4 5 6 |
|
配置mysql的主配置文件:
1 2 3 4 5 6 7 8 9 |
|
注:一般建议将binlog日志与数据文件分开存放,不但可以提高mysql性能,还可以增加安全性!
三、Mysql 复制(Replication)
1.Mysql 复制作用
- 负载平衡(load balancing)
- 备份
- 高可用性(high availability)和容错
2.Mysql 复制如何工作
主要有三步(如下图):
- master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
- slave将master的binary log events拷贝到它的中继日志(relay log);
- slave重做中继日志中的事件,将改变反映它自己的数据。
具体说明:
该过程的第一部分就是master记录二进制日志。在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。
下一步就是slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。
SQL slave thread处理该过程的最后一步。SQL线程从中继日志读取事件,更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。
此外,在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。复制过程有一个很重要的限制——复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。所以slave上数据一般要慢于master上数据。即master与slave之间的数据在一定时间内会不同步。
四、Mysql 复制(Replication)类型
1.mysql复制类型有以下一些基本原则
- 每个slave只能有一个master;
- 每个slave只能有一个唯一的服务器ID;
- 每个master可以有很多slave;
- 如果你设置log_slave_updates,slave可以是其它slave的master,从而扩散master的更新。
注,MySQL不支持多主服务器复制(Multimaster Replication)——即一个slave可以有多个master。但是,通过一些简单的组合,我们却可以建立灵活而强大的复制体系结构。
2.mysql复制类型
(1).一主多从模式
注,由一个master和一个slave组成复制系统是最简单的情况。Slave之间并不相互通信,只能与master进行通信。
具体说明:
如果写操作较少,而读操作很时,可以采取这种结构。你可以将读操作分布到其它的slave,从而减小master的压力。但是,当slave增加到一定数量时,slave对master的负载以及网络带宽都会成为一个严重的问题。这种结构虽然简单,但是,它却非常灵活,足够满足大多数应用需求。一些建议:
- 不同的slave扮演不同的作用(例如使用不同的索引,或者不同的存储引擎)
- 用一个slave作为备用master,只进行复制
- 用一个远程的slave,用于灾难恢复
发送复制事件到其它slave,当设置log_slave_updates时,你可以让slave扮演其它slave的master。此时,slave把SQL线程执行的事件写进行自己的二进制日志(binary log),然后,它的slave可以获取这些事件并执行它。
(2).双主模式
注,Master-Master复制的两台服务器,既是master,又是另一台服务器的slave。
具体说明:
主动的Master-Master复制有一些特殊的用处。例如,地理上分布的两个部分都需要自己的可写的数据副本。这种结构最大的问题就是更新冲突。假设一个表只有一行(一列)的数据,其值为1,如果两个服务器分别同时执行如下语句:
在第一个服务器上执行:
1 2 3 |
|
那么结果是多少呢?一台服务器是4,另一个服务器是3,但是,这并不会产生错误。
实际上,MySQL并不支持其它一些DBMS支持的多主服务器复制(Multimaster Replication),这是MySQL的复制功能很大的一个限制(多主服务器的难点在于解决更新冲突),但是,如果你实在有这种需求,你可以采用MySQL Cluster,以及将Cluster和Replication结合起来,可以建立强大的高性能的数据库平台。但是,可以通过其它一些方式来模拟这种多主服务器的复制。
(3).主从模式
注,这是master-master结构变化而来的,它避免了M-M的缺点,实际上,这是一种具有容错和高可用性的系统。它的不同点在于其中一个服务只能进行只读操作。
(4).带从服务器的Master-Master结构(Master-Master with Slaves)
注,这种结构的优点就是提供了冗余。在地理上分布的复制结构,它不存在单一节点故障问题,而且还可以将读密集型的请求放到slave上。
总结:一般常用的两种复制类型一种是主从模式,另一种是一主多从模式。在这一篇博客中我们主要讲解主从模式复制。
五、Mysql 主从复制基本步骤
1.master 配置
- 启用二进制日志
- 配置一个唯一的server-id
- 创建具有复制权限的用户
2.slave 配置
- 启用中继日志
- 配置一个唯一的server-id
- 连接主服务器,并开始复制数据
- 启动数据复制
注,基本步骤我们就说到这里,下面我们来具体演示一下主从复制。
六、Mysql 主从复制(异步)
1.mysql异步复制
异步复制:MySQL本身支持单向的、异步的复制。异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时,最重要的是这意味着当应用系统的事务在主服务器上提交并确认时数据并不能在同一时刻拷贝或应用到从服务器上。通常这个延时是由网络带宽、资源可用性和系统负载决定的。然而,使用正确的组件并且调优,复制能做到接近瞬时完成。
当主库有更新的时候,主库会把更新操作的SQL写入二进制日志(Bin log),并维护一个二进制日志文件的索引,以便于日志文件轮回(Rotate)。在从库启动异步复制的时候,从库会开启两个I/O线程,其中一个线程连接主库,要求主库把二进制日志的变化部分传给从库,并把传回的日志写入本地磁盘。另一个线程则负责读取本地写入的二进制日志,并在本地执行,以反映出这种变化。较老的版本在复制的时候只启用一个I/O线程,实现这两部分的功能。下面我们来具体演示一下mysql的异步复制。
2.实验拓扑
注,Active (master,node1) 192.168.1.201 , Passive (slave,node2)192.168.1.202
3.环境配置
时间同步
1 2 |
|
4.操作系统
- CentOS 6.4 X86_64
5.软件版本
- Mysql 5.5.33 (注,这里用的是mysql 5.5.33二进制通用安装包,解压就能用)
6.安装并配置mysql
master:
(1).解压并链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
(2).新建mysql用户
1 2 3 4 |
|
(3).修改mysql安装目录所有者与所属组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
(4).为mysql提供配置文件并修改
1 2 3 4 5 |
|
(5).为mysql提供启动脚本
1 2 |
|
(6).初始化mysql数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
(7).启动并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
好了,到这里master的mysql配置完成,下面我们进行slave的mysql配置。
slave:
(1).解压并链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
(2).新建mysql用户
1 2 3 4 |
|
(3).修改mysql安装目录所有者与所属组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
(4).为mysql提供配置文件并修改
1 2 3 4 |
|
(5).为mysql提供启动脚本
1 2 |
|
(6).初始化mysql数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
(7).启动并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
好了,slave的mysql也配置完成了,下面我们来配置主从复制。
7.配置master
(1).修改配置文件
1 2 3 4 5 6 7 |
|
(2).授权复制用户
1 2 3 4 |
|
(3).重启一下mysql服务
1 2 3 |
|
8.配置slave
(1).修改配置文件
1 2 3 4 5 6 |
|
(2).重启mysql服务
1 2 3 |
|
(3).连接主服务器并复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
9.主从复制测试
master:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
slave:
1 2 3 4 5 6 7 8 9 10 11 |
|
好了,到这里异步的主从复制到这里配置完成。下面我们来说一下什么是半同步复制(或说是同步也行)。
七、Mysql 主从复制(半同步)
1.半同步复制
在说明半同步复制之前我们先来了解一下,什么是同步复制?同步复制:同步复制可以定义为数据在同一时刻被提交到一台或多台机器,通常这是通过众所周知的“两阶段提交”做到的。虽然这确实给你在多系统中保持一致性,但也由于增加了额外的消息交换而造成性能下降。使用MyISAM或者InnoDB存储引擎的MySQL本身并不支持同步复制,然而有些技术,例如分布式复制块设备(简称DRBD),可以在下层的文件系统提供同步复制,允许第二个MySQL服务器在主服务器丢失的情况下接管(使用第二服务器的复本)。了解了同步复制我们正下面来说一下,什么是半同步复制?
MYSQL 5.5开始,支持半自动复制。之前版本的MySQL Replication都是异步(asynchronous)的,主库在执行完一些事务后,是不会管备库的进度的。如果备库不幸落后,而更不幸的是主库此时又出现Crash(例如宕机),这时备库中的数据就是不完整的。简而言之,在主库发生故障的时候,我们无法使用备库来继续提供数据一致的服务了。Semisynchronous Replication(半同步复制)则一定程度上保证提交的事务已经传给了至少一个备库。Semi synchronous中,仅仅保证事务的已经传递到备库上,但是并不确保已经在备库上执行完成了。
此外,还有一种情况会导致主备数据不一致。在某个session中,主库上提交一个事务后,会等待事务传递给至少一个备库,如果在这个等待过程中主库Crash,那么也可能备库和主库不一致,这是很致命的。如果主备网络故障或者备库挂了,主库在事务提交后等待10秒(rpl_semi_sync_master_timeout的默认值)后,就会继续。这时,主库就会变回原来的异步状态。
MySQL在加载并开启Semi-sync插件后,每一个事务需等待备库接收日志后才返回给客户端。如果做的是小事务,两台主机的延迟又较小,则Semi-sync可以实现在性能很小损失的情况下的零数据丢失。
2.异步与半同步异同
默认情况下MySQL的复制是异步的,Master上所有的更新操作写入Binlog之后并不确保所有的更新都被复制到Slave之上。异步操作虽然效率高,但是在Master/Slave出现问题的时候,存在很高数据不同步的风险,甚至可能丢失数据。
MySQL5.5引入半同步复制功能的目的是为了保证在master出问题的时候,至少有一台Slave的数据是完整的。在超时的情况下也可以临时转入异步复制,保障业务的正常使用,直到一台salve追赶上之后,继续切换到半同步模式。
3.具体配置
注,mysql5.5半同步插件是由谷歌提供,具体位置/usr/local/mysql/lib/plugin/下,一个是master用的semisync_master.so,一个是slave用的semisync_slave.so,下面我们就来具体配置一下。
master:
(1).安装插件
1 2 3 4 5 6 |
|
(2).修改配置文件
1 2 3 4 |
|
(3).重新启动服务
1 2 3 |
|
slave:
(1).安装插件
1 2 3 4 5 6 7 8 |
|
(2).修改配置文件
1 2 3 |
|
(3).重新启动服务
1 2 3 |
|
4.查看一下状态
master:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
slave:
1 2 3 4 5 6 7 |
|
5.测试一下
master:
1 2 3 4 5 6 7 8 9 10 11 |
|
注,大家可以看到创建一个表的插入一个数据的时间都很长,说明半同步配置完成。
6.模拟一下故障
slave:
1 2 3 4 5 |
|
注,大家可以看到主服务器会卡1s,我们超时时间设置的为1s。
7.查看一下状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
好了,到这里我们就配置完成了半同步复制。希望大家有所收获。下面我们来简单说一下mysql复制的工具。
八、Mysql 复制工具
1.percona-toolkit简介
percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括:
- 检查master和slave数据的一致性
- 有效地对记录进行归档
- 查找重复的索引
- 对服务器信息进行汇总
- 分析来自日志和tcpdump的查询
- 当系统出问题的时候收集重要的系统信息
percona-toolkit源自Maatkit 和Aspersa工具,这两个工具是管理mysql的最有名的工具,现在Maatkit工具已经不维护了,请大家还是使用percona-toolkit吧!这些工具主要包括开发、性能、配置、监控、复制、系统、实用六大类,作为一个优秀的DBA,里面有的工具非常有用,如果能掌握并加以灵活应用,将能极大的提高工作效率。
2.安装percona-toolkit
1 2 3 4 5 6 7 8 9 10 11 12 |
|
3.简单使用
常用工具:
(1).服务器摘要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
(2).服务器磁盘监测
1 2 3 4 5 6 7 8 9 10 11 |
|
(3).mysql服务状态摘要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
(4).慢查询日志分析统计
1 |
|
(5).主从状态监测,提供给它一台mysql服务器的IP用户名密码,就可以分析出整个主从架构中每台服务器的信息,包括但不限于mysql版本,IP地址,server ID,mysql服务的启动时间,角色(主/从),Slave Status(落后于主服务器多少秒,有没有错误,slave有没有在运行)。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
(6).mysql死锁监测
1 |
|
(7).监测从库的复制延迟
1 |
|
注,简单说明就到这里,想学习更详细的内容,命令的使用可以通过--help获知
九、Mysql 复制注意事项
注,在主-从架构上建议使用的配置
master:
1 2 3 4 5 |
|
十、Mysql 复制过滤
master:
1 2 3 4 5 6 7 8 9 |
|
测试一下:
在从服务器上只复制testdb一个数据库
slave:
1 2 3 4 5 6 7 |
|
master:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
slave:
1 2 3 4 5 6 7 8 9 10 11 |
|
注,大家可以看到没有同步mydb1,再测试一下。
master:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
注,大家可以看到同步了testdb,好了到这里所有演示全部完成,希望大家有所收获。^_^……
本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1282329