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

源码安装详见中间部分。

在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

源码安装详见中间部分。

在Slave上操作:
1)、确保/etc/my.cnf中有log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=10。修改之后如下所示:
[mysqld]
log-bin=mysql-bin 启动二进制文件
server-id=10 服务器ID
2)、重启mysql服务。
[root@localhost ~]#/etc/init.d/mysql restart
3)、登录mysql,执行如下语句
[root@localhost ~]# mysql –u root –p

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> start slave;
5)、检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。

mysql> show slave status\G

*************************** 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: 0Master_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: 01 row in set (0.00 sec)
验证配置是否正常,mysql主从能否正常复制。
1.4、测试主从复制
在主数据库上新建一个库,并且在库中写一个表和一些数据。
[root@localhost ~]# mysql –u root –p
mysql> create database mydatabase;
mysql> use mydatabase;
mysql> create table jin (id int(5),name char(10));
mysql> insert into jin values (2,'puck');
在从数据库中验证一下,是否正常复制到数据。
[root@localhost ~]# mysql -uroot –p123456
mysql> show databases;
mysql> select * from mydatabase.jin;