1、复制概述
1.1、复制解决的问题
数据复制技术有以下一些特点:
(1) 数据分布
(2) 负载平衡(load balancing)
(3) 备份
(4) 高可用性(high availability)和容错
1.2、复制如何工作
从sql layer中逻辑模块上来看
复制模块分为master模块和slave模块两部分,master模块主要负责在replication环境中读取master端的binary日志,以及与slave端的I/O thread交互等工作。slave模块比master模块所要做的事情稍多一些,在系统中主要体现在两个线程上面。一个是负责从master请求和接受binary日志,并写入本地relay_log的I/O thread。另外一个是负责从relay_log中读取相关的日志事件的SQLthread,然后解析成可以在slave端正确执行并得到和master端完全相同的结果的命令并再交给slave执行的过程。
过程如下:
注:上图中同一种颜色的箭头表示一步,自左向右。
1.3实验环境
系统:centos6.4 x86_64
数据库:mysql 5.6.13(源码安装)
Master端
IP:200.168.10.209
源码安装详见http://essun.blog.51cto.com/721033/1288442中间部分。
在Master上操作:
1)、确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。
[root@localhost ~]# vim/etc/my.cnf
[mysqld]
log-bin=mysql-bin 启动二进制文件
server-id=1 服务器ID
2)、登录mysql,在mysql中添加一个puck的账号,并授权给从服务器。 [root@localhost ~]# mysql -uroot -p
mysql> create user puck@200.168.10.200 identified by'password';
mysql> grant replication client,replication slave on *.* to puck@200.168.10.200 identified by 'password';(创建puck用户,并授权给200.168.10.200(Slave)使用。)
3)、查询主数据库状态,并记下FILE及Position的值,这个在后面配置从服务器的时候要用到。
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
Slave端
IP:200.168.10.200
源码安装详见http://essun.blog.51cto.com/721033/1288442中间部分。
mysql> change master to master_host='200.168.10.209',master_user='puck',master_password='password',master_log_file='mysql-bin.000005',master_log_pos=120;
4)、启动slave同步。
mysql> show slave status\G
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
|
*************************** 1. row ***************************
Slave_IO_State: Waiting
for
master
to
send event
Master_Host: 200.168.10.209 #主服务器的地址
Master_User: puck #主服务器允许复制的用户
Master_Port: 3306 #通信端口
Connect_Retry: 60 #重新连接的时间
Master_Log_File: mysql-bin.000005#当前主服务器的日志文件
Read_Master_Log_Pos: 120 #binary_log日志起始点
Relay_Log_File: slave-relay-bin.000005#Slave端当前日志文件
Relay_Log_Pos: 283 #Relay_log日志起始点
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes #I/Othread 用于复制master的
Binary
log到Relay_log中,当前状态运行中
Slave_SQL_Running: Yes #将Relay_log中语句解析并应用于Slave端数据库中
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: 120
Relay_Log_Space: 619
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: 1
Master_UUID: f9ba5511-0fbe-11e3-a989-000c29f64ea5
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay:
NULL
Slave_SQL_Running_State: Slave has
read
all
relay log; waiting
for
the slave I/O thread
to
update
it
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:
Auto_Position: 0
1 row
in
set
(0.00 sec)
|