MySQL 5.6 基于GTID及多线程的复制详解

来源:互联网 发布:stussy淘宝正品 编辑:程序博客网 时间:2024/05/01 13:39


一、Mysql 5.6 新特性

MySQL 5.6 主要在查询性能的优化、InnoDB改进以支持高吞吐量的事务、NoSQL风格的API、分区功能的改进、数据复制的改进,增加 PERFORMANCE_SCHEMA 库以获得数据库性能信息等。

1.查询性能优化

       优化 WHERE 语句改进索引条件的处理性能,Multi-Range Read:通过随机数据访问来提升 SSD 上的数据读取速度,优化文件排序:对一些组合了ORDER BYnon_indexed_columnLIMITx 的SQL语句,该特性将大大加速此类语句的执行速度。

2.InnoDB 的改进

       MySQL 5.6 完全集成 InnoDB 作为默认的存储引擎。同时 5.6 版本在使用 InnoDB 上的很多细节做了改进

3.提供 NoSQL 风格的 API

       该功能主要适用于将 MySQL 来作为 NoSQL 使用,而 MySQL 使用的是 memcached 兼容的 API。通过该接口程序访问数据可直达 InnoDB 存储引擎,而无需通过 MySQL 对 SQL 的转换过程,大大提升了数据访问的性能。

4.分区的改进

显式分区数据查询,例如:


view sourceprint?
1.SELECT* FROM employees PARTITION (p0, p2);
2.DELETEFROM employees PARTITION (p0, p1);
3.UPDATEemployees PARTITION (p0) SETstore_id = 2 WHERE fname = 'Jill';
4.SELECTe.id, s.city FROM employees AS e JOINstores PARTITION (p1) AS s ...;

分区数据的导入导出,此功能用于快速的将某个表迁移到分区上:ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;

5.复制功能的改进

       支持多线程复制,事实上是针对每个database开启相应的独立线程。即每个库有一个单独的(sql thread)如果线上业务中,只有一个database或者绝大多数压力集中在个别database的话,多线程并发复制特性就没有意义了。

       支持启用GTID,对运维人员来说应该是一件令人高兴的事情,在配置主从复制,传统的方式里,你需要找到binlog和POS点,然后change master to指向,而不是很有经验的运维,往往会将其找错,造成主从同步复制报错,在mysql5.6里,无须再知道binlog和POS点,需要知道master的IP、端口,账号密码即可,因为同步复制是自动的,mysql通过内部机制GTID自动找点同步。

6.大大增强 PERFORMANCE_SCHEMA 数据库

       降低了数据库开销、表IO的信息汇集和监控、表锁信息汇集和监控、会话和用户级别的监控、全局性能信息汇总。

二、GITD 详解

MySQL 5.6 的新特性之一,是加入了全局事务 ID (GTID) 来强化数据库的主备一致性,故障恢复,以及容错能力。

1.什么是GTID?

       官方文档:http://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html在这篇文档里,我们可以知道全局事务 ID 的官方定义是:GTID = source_id:transaction_id

MySQL 5.6 中,每一个 GTID 代表一个数据库事务。在上面的定义中,source_id 表示执行事务的主库 uuid(server_uuid),transaction_id 是一个从 1 开始的自增计数,表示在这个主库上执行的第 n 个事务。MySQL 会保证事务与 GTID 之间的 1 : 1 映射。

       例如,下面就是一个 GTID:3E11FA47-71CA-11E1-9E33-C80AA9429562:50 表示在以 "3E11FA47-71CA-11E1-9E33-C80AA9429562" 为唯一标示的 MySQL 实例上执行的第 50 个数据库事务。很容易理解,MySQL 只要保证每台数据库的 server_uuid 全局唯一,以及每台数据库生成的 transaction_id 自身唯一,就能保证 GTID 的全局唯一性。

2.什么是server_uuid?

       MySQL 5.6 用 128 位的 server_uuid 代替了原本的 32 位 server_id 的大部分功能。原因很简单,server_id 依赖于 my.cnf 的手工配置,有可能产生冲突 —— 而自动产生 128 位 uuid 的算法可以保证所有的 MySQL uuid 都不会冲突。

       在首次启动时 MySQL 会调用 generate_server_uuid() 自动生成一个 server_uuid,并且保存到 auto.cnf 文件 —— 这个文件目前存在的唯一目的就是保存 server_uuid。


view sourceprint?
01.[root@master data]# ll 
02.总用量 110624 
03.-rw-rw---- 1 mysql mysql       56 8月  26 13:57 auto.cnf 
04.-rw-rw---- 1 mysql mysql 12582912 8月  26 14:00 ibdata1 
05.-rw-rw---- 1 mysql mysql 50331648 8月  26 14:00 ib_logfile0 
06.-rw-rw---- 1 mysql mysql 50331648 8月  26 13:44 ib_logfile1 
07.-rw-rw---- 1 mysql mysql      143 8月  26 14:00 master-bin.000001 
08.-rw-rw---- 1 mysql mysql      120 8月  26 14:00 master-bin.000002 
09.-rw-rw---- 1 mysql mysql       40 8月  26 14:00 master-bin.index 
10.-rw-rw---- 1 mysql mysql        5 8月  26 14:00 master.test.com.pid 
11.drwx------ 2 mysql mysql     4096 8月  26 13:44 mysql 
12.drwx------ 2 mysql mysql     4096 8月  26 13:44 performance_schema 
13.drwx------ 2 mysql mysql     4096 8月  26 13:44 test
14.[root@master data]# cat auto.cnf 
15.[auto] 
16.server-uuid=6b27d8b7-0e14-11e3-9eab-000c291192e4

       在 MySQL 再次启动时会读取 auto.cnf 文件,继续使用上次生成的 server_uuid。使用 SHOW 命令可以查看 MySQL 实例当前使用的 server_uuid?:SHOW GLOBAL VARIABLES LIKE 'server_uuid';它是一个 MySQL 5.6 global variables,文档链接在这里:server_uuid? 全局唯一的 server_uuid 的一个好处是:可以解决由 server_id 配置冲突带来的 MySQL 主备复制的异常终止(BUG#33815?)

       在MySQL 5.6,Slave 向 Master 申请 binlog 时,会首先发送自己的 server_uuid,Master 用 Slave 发送的 server_uuid 代替 server_id (MySQL 5.6 之前的方式)作为 kill_zombie_dump_threads 的参数,终止冲突或者僵死的 BINLOG_DUMP 线程。

三、多线程复制基于库

       MySQL 5.6之前的版本,同步复制是单线程的,队列的,只能一个一个执行,在5.6里,可以做到多个库之间的多线程复制,例如数据库里,存放着用户表,商品表,价格表,订单表,那么将每个业务表单独放在一个库里,这时就可以做到多线程复制,但一个库里的表,多线程复制是无效的。

注,每个数据库仅能使用一个线程,复制涉及到多个数据库时多线程复制才有意义。

四、Mysql 5.6 复制管理工具

官方下载地址:http://dev.mysql.com/downloads/tools/utilities/#downloads

注,这里只简单的介绍一下,具体的工具使用,不具体说明,使用方法 命令—help

  • mysqlreplicate 快速启动复制

  • mysqlrplcheck 快速检查复制环境

  • mysqlrplshow 显示复制拓扑

  • mysqlfailover 故障转移

  • mysqlrpladmim 管理工具

五、具体配置过程

1.环境准备

操作系统

  • CentOS 6.4 x86_64

软件版本

  • Mysql 5.6.13

2.实验拓扑

mysql

3.修改主机名


view sourceprint?
1.[root@master ~]# uname -n 
2.master.test.com
3.[root@slave ~]# uname -n 
4.slave.test.com

4.配置名称解析


view sourceprint?
01.[root@master ~]# cat /etc/hosts 
02.127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 
03.::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 
04.192.168.18.201    master.test.com    master 
05.192.168.18.202    slave.test.com    slave
06.[root@slave ~]# cat /etc/hosts 
07.127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 
08.::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 
09.192.168.18.201    master.test.com    master 
10.192.168.18.202    slave.test.com    slave

5.配置时间同步


view sourceprint?
1.[root@master ~]# ntpdate 202.120.2.101
2.[root@slave ~]# ntpdate 202.120.2.101

6.关闭防火墙与SELinux


view sourceprint?
1.[root@master ~]# service iptables stop 
2.[root@master ~]# chkconfig iptables off
3.[root@master ~]# getenforce 
4.Disabled
5.[root@slave ~]# service iptables stop 
6.[root@slave ~]# chkconfig iptables off
7.[root@slave ~]# getenforce 
8.Disabled

7.安装并配置mysql

master:

(1).安装并链接mysql


view sourceprint?
01.[root@master ~]# clear 
02.[root@master ~]# cd src/ 
03.[root@master src]# ls 
04.mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz 
05.[root@master src]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ 
06.[root@master src]# cd /usr/local/ 
07.[root@masterlocal]# ln -sv /usr/local/mysql-5.6.13-linux-glibc2.5-x86_64 mysql 
08."mysql"->"/usr/local/mysql-5.6.13-linux-glibc2.5-x86_64"
09.[root@masterlocal]# cd mysql 
10.[root@master mysql]# ll 
11.总用量 156 
12.drwxr-xr-x  2 root root   4096 8月  26 13:35 bin 
13.-rw-r--r--  1 7161 wheel 17987 7月  11 00:17 COPYING 
14.drwxr-xr-x  3 root root   4096 8月  26 13:34 data 
15.drwxr-xr-x  2 root root   4096 8月  26 13:35 docs 
16.drwxr-xr-x  3 root root   4096 8月  26 13:35 include 
17.-rw-r--r--  1 7161 wheel 88178 7月  11 00:17 INSTALL-BINARY 
18.drwxr-xr-x  3 root root   4096 8月  26 13:34 lib 
19.drwxr-xr-x  4 root root   4096 8月  26 13:35man
20.drwxr-xr-x 10 root root   4096 8月  26 13:35 mysql-test
21.-rw-r--r--  1 7161 wheel  2496 7月  11 00:17 README 
22.drwxr-xr-x  2 root root   4096 8月  26 13:34 scripts 
23.drwxr-xr-x 28 root root   4096 8月  26 13:34 share 
24.drwxr-xr-x  4 root root   4096 8月  26 13:35 sql-bench

(2).新建mysql用户


view sourceprint?
1.[root@master mysql]# groupadd -g 3306 mysql 
2.[root@master mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql 
3.[root@master mysql]# id mysql 
4.uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)

(3).修改mysql安装目录所有者与所属组


view sourceprint?
01.[root@master mysql]# chown -R root.mysql /usr/local/mysql/* 
02.[root@master mysql]# ll 
03.总用量 156 
04.drwxr-xr-x  2 root mysql  4096 8月  26 13:35 bin 
05.-rw-r--r--  1 root mysql 17987 7月  11 00:17 COPYING 
06.drwxr-xr-x  3 root mysql  4096 8月  26 13:34 data 
07.drwxr-xr-x  2 root mysql  4096 8月  26 13:35 docs 
08.drwxr-xr-x  3 root mysql  4096 8月  26 13:35 include 
09.-rw-r--r--  1 root mysql 88178 7月  11 00:17 INSTALL-BINARY 
10.drwxr-xr-x  3 root mysql  4096 8月  26 13:34 lib 
11.drwxr-xr-x  4 root mysql  4096 8月  26 13:35man
12.drwxr-xr-x 10 root mysql  4096 8月  26 13:35 mysql-test
13.-rw-r--r--  1 root mysql  2496 7月  11 00:17 README 
14.drwxr-xr-x  2 root mysql  4096 8月  26 13:34 scripts 
15.drwxr-xr-x 28 root mysql  4096 8月  26 13:34 share 
16.drwxr-xr-x  4 root mysql  4096 8月  26 13:35 sql-bench 
17.drwxr-xr-x  3 root mysql  4096 8月  26 13:34 support-files

(4).初始化mysql数据库

先安装libaio库文件


view sourceprint?
1.[root@master mysql]# yum install  -y libaio

创建数据存放目录


view sourceprint?
1.[root@master mysql]# mkdir -pv /mydata/data 
2.mkdir: 已创建目录"/mydata"
3.mkdir: 已创建目录"/mydata/data"

修改目录所有者与所属组


view sourceprint?
1.[root@master mysql]#  chown -R mysql.mysql /mydata/data/ 
2.[root@master mysql]# cd /mydata/data/

初始化mysql


view sourceprint?
01.[root@master data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
02.[root@master data]# ll 
03.总用量 110604 
04.-rw-rw---- 1 mysql mysql 12582912 8月  26 13:44 ibdata1 
05.-rw-rw---- 1 mysql mysql 50331648 8月  26 13:44 ib_logfile0 
06.-rw-rw---- 1 mysql mysql 50331648 8月  26 13:44 ib_logfile1 
07.drwx------ 2 mysql mysql     4096 8月  26 13:44 mysql 
08.drwx------ 2 mysql mysql     4096 8月  26 13:44 performance_schema 
09.drwx------ 2 mysql mysql     4096 8月  26 13:44test

(5).简单修改配置文件

注,mysql5.6以后,初始化完成后会在安装目录自动生成my.cnf的配置文件,直接修改即可。


view sourceprint?
01.[root@master mysql]# ll 
02.总用量 160 
03.drwxr-xr-x  2 root mysql  4096 8月  26 13:35 bin 
04.-rw-r--r--  1 root mysql 17987 7月  11 00:17 COPYING 
05.drwxr-xr-x  3 root mysql  4096 8月  26 13:34 data 
06.drwxr-xr-x  2 root mysql  4096 8月  26 13:35 docs 
07.drwxr-xr-x  3 root mysql  4096 8月  26 13:35 include 
08.-rw-r--r--  1 root mysql 88178 7月  11 00:17 INSTALL-BINARY 
09.drwxr-xr-x  3 root mysql  4096 8月  26 13:34 lib 
10.drwxr-xr-x  4 root mysql  4096 8月  26 13:35man
11.-rw-r--r--  1 root root    943 8月  26 13:44 my.cnf 
12.drwxr-xr-x 10 root mysql  4096 8月  26 13:35 mysql-test
13.-rw-r--r--  1 root mysql  2496 7月  11 00:17 README 
14.drwxr-xr-x  2 root mysql  4096 8月  26 13:34 scripts 
15.drwxr-xr-x 28 root mysql  4096 8月  26 13:34 share 
16.drwxr-xr-x  4 root mysql  4096 8月  26 13:35 sql-bench 
17.drwxr-xr-x  3 root mysql  4096 8月  26 13:34 support-files 
18.[root@master mysql]# vim my.cnf
19.#增加下面四行
20.datadir = /mydata/data
21.log-bin=master-bin 
22.log-bin-index=master-bin.index 
23.innodb_file_per_table = 1

(6).为mysql提供启动脚本


view sourceprint?
1.[root@master mysql]# cp support-files/mysql.server /etc/init.d/mysqld 
2.[root@master mysql]# chmod +x /etc/init.d/mysqld

(7).启动并测试


view sourceprint?
01.[root@master mysql]# service mysqld start 
02.Starting <a class="keylink"href="http://www.it165.net/database/dbmy/"target="_blank">MySQL</a> SUCCESS! 
03.[root@master mysql]# netstat -ntlp 
04.Active Internet connections (only servers) 
05.Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 
06.tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1025/sshd        
07.tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1102/master      
08.tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      1144/sshd        
09.tcp        0      0 127.0.0.1:6011              0.0.0.0:*                   LISTEN      1280/sshd        
10.tcp        0      0 :::22                       :::*                        LISTEN      1025/sshd        
11.tcp        0      0 ::1:25                      :::*                        LISTEN      1102/master      
12.tcp        0      0 ::1:6010                    :::*                        LISTEN      1144/sshd        
13.tcp        0      0 ::1:6011                    :::*                        LISTEN      1280/sshd        
14.tcp        0      0 :::3306                     :::*                        LISTEN      1648/mysqld

环境变量配置


view sourceprint?
1.[root@master data]# vim /etc/profile.d/mysql.sh
2.exportPATH=$PATH:/usr/local/mysql/bin
3.[root@master data]# source /etc/profile

测试一下


view sourceprint?
01.[root@master mysql]# mysql -h127.0.0.1 
02.Welcome to the <a class="keylink"href="http://www.it165.net/database/dbmy/"target="_blank">MySQL</a> monitor.  Commands end with ; or \g. 
03.Your MySQL connectionid is 1 
04.Server version: 5.6.13-log MySQL Community Server (GPL)
05.Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
06.Oracle is a registered trademark of Oracle Corporation and/or its 
07.affiliates. Other names may be trademarks of their respective 
08.owners.
09.Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
10.mysql&gt; show databases; 
11.+--------------------+ 
12.| Database           | 
13.+--------------------+ 
14.| information_schema | 
15.| mysql              | 
16.| performance_schema | 
17.|test              
18.+--------------------+ 
19.4 rowsin set (0.11 sec)

slave:

(1).安装并链接mysql


view sourceprint?
01.[root@slave ~]# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ 
02.[root@slave ~]# cd /usr/local/ 
03.[root@slavelocal]# ln -sv /usr/local/mysql-5.6.13-linux-glibc2.5-x86_64 mysql 
04."mysql"-&gt;"/usr/local/mysql-5.6.13-linux-glibc2.5-x86_64"
05.[root@slavelocal]# cd mysql 
06.[root@slave mysql]# ll 
07.总用量 156 
08.drwxr-xr-x  2 root root   4096 8月  26 14:04 bin 
09.-rw-r--r--  1 7161 wheel 17987 7月  11 00:17 COPYING 
10.drwxr-xr-x  3 root root   4096 8月  26 14:04 data 
11.drwxr-xr-x  2 root root   4096 8月  26 14:04 docs 
12.drwxr-xr-x  3 root root   4096 8月  26 14:04 include 
13.-rw-r--r--  1 7161 wheel 88178 7月  11 00:17 INSTALL-BINARY 
14.drwxr-xr-x  3 root root   4096 8月  26 14:04 lib 
15.drwxr-xr-x  4 root root   4096 8月  26 14:04man
16.drwxr-xr-x 10 root root   4096 8月  26 14:04 mysql-test
17.-rw-r--r--  1 7161 wheel  2496 7月  11 00:17 README 
18.drwxr-xr-x  2 root root   4096 8月  26 14:04 scripts 
19.drwxr-xr-x 28 root root   4096 8月  26 14:04 share 
20.drwxr-xr-x  4 root root   4096 8月  26 14:04 sql-bench 
21.drwxr-xr-x  3 root root   4096 8月  26 14:04 support-files

(2).新建mysql用户


view sourceprint?
1.[root@slavemysql]# groupadd -g 3306 mysql 
2.[root@slavemysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql 
3.[root@slavemysql]# id mysql 
4.uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)

(3).修改mysql安装目录所有者与所属组


view sourceprint?
01.[root@slave mysql]# chown -R root.mysql /usr/local/mysql/* 
02.[root@slave mysql]# ll 
03.总用量 156 
04.drwxr-xr-x  2 root mysql  4096 8月  26 14:04 bin 
05.-rw-r--r--  1 root mysql 17987 7月  11 00:17 COPYING 
06.drwxr-xr-x  3 root mysql  4096 8月  26 14:04 data 
07.drwxr-xr-x  2 root mysql  4096 8月  26 14:04 docs 
08.drwxr-xr-x  3 root mysql  4096 8月  26 14:04 include 
09.-rw-r--r--  1 root mysql 88178 7月  11 00:17 INSTALL-BINARY 
10.drwxr-xr-x  3 root mysql  4096 8月  26 14:04 lib 
11.drwxr-xr-x  4 root mysql  4096 8月  26 14:04man
12.drwxr-xr-x 10 root mysql  4096 8月  26 14:04 mysql-test
13.-rw-r--r--  1 root mysql  2496 7月  11 00:17 README 
14.drwxr-xr-x  2 root mysql  4096 8月  26 14:04 scripts 
15.drwxr-xr-x 28 root mysql  4096 8月  26 14:04 share 
16.drwxr-xr-x  4 root mysql  4096 8月  26 14:04 sql-bench 
17.drwxr-xr-x  3 root mysql  4096 8月  26 14:04 support-files

(4).初始化mysql数据库

安装libaio库文件


view sourceprint?
1.[root@slave mysql]# yum install -y libaio

创建数据存放目录


view sourceprint?
1.[root@slave mysql]# mkdir -pv /mydata/data 
2.mkdir: 已创建目录"/mydata"
3.mkdir: 已创建目录"/mydata/data"

修改目录所有者与所属组


view sourceprint?
1.[root@slave mysql]#  chown -R mysql.mysql /mydata/data/ 
2.[root@slave mysql]# cd /mydata/data/

初始化mysql


view sourceprint?
1.[root@master data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql

(5).简单修改配置文件


view sourceprint?
1.[root@slave data]# cd /usr/local/mysql 
2.[root@slave mysql]# vim my.cnf
3.datadir = /mydata/data
4.log-bin=master-bin
5.log-bin-index=master-bin.index
6.innodb_file_per_table = 1

(6).为mysql提供启动脚本


view sourceprint?
1.[root@slave mysql]# cp support-files/mysql.server /etc/init.d/mysqld 
2.[root@slave mysql]# chmod +x /etc/init.d/mysqld

(7).启动并测试


view sourceprint?
01.[root@slave mysql]# service mysqld start 
02.Starting MySQL..... SUCCESS! 
03.[root@slave mysql]# netstat -ntlp 
04.Active Internet connections (only servers) 
05.Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 
06.tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1047/sshd        
07.tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1141/master      
08.tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      1055/sshd        
09.tcp        0      0 :::22                       :::*                        LISTEN      1047/sshd        
10.tcp        0      0 ::1:25                      :::*                        LISTEN      1141/master      
11.tcp        0      0 ::1:6010                    :::*                        LISTEN      1055/sshd        
12.tcp        0      0 :::3306                     :::*                        LISTEN      1576/mysqld

环境变量配置


view sourceprint?
1.[root@slave data]# vim /etc/profile.d/mysql.sh
2.exportPATH=$PATH:/usr/local/mysql/bin
3.[root@slave data]# source /etc/profile

测试登录一下


view sourceprint?
01.[root@slave mysql]# mysql -h127.0.0.1 
02.Welcome to the MySQL monitor.  Commands end with ; or \g. 
03.Your MySQL connectionid is 1 
04.Server version: 5.6.13-log MySQL Community Server (GPL)
05.Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
06.Oracle is a registered trademark of Oracle Corporation and/or its 
07.affiliates. Other names may be trademarks of their respective 
08.owners.
09.Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
10.mysql&gt; show databases; 
11.+--------------------+ 
12.| Database           | 
13.+--------------------+ 
14.| information_schema | 
15.| mysql              | 
16.| performance_schema | 
17.|test              
18.+--------------------+ 
19.4 rowsin set (0.12 sec)

好了,到这里基本配置就全部完成了,下面我们来配置基于GTID及多线程的主从复制。

8.配置mysql主从复制

(1).配置选项说明

要在MySQL 5.6中使用复制功能,其服务配置段[mysqld]中于少应该定义如下选项,

  • binlog-format:二进制日志的格式,有row、statement和mixed几种类型;需要注意的是:当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;

  • log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用于启动GTID及满足附属的其它需求;

  • master-info-repository和relay-log-info-repository:启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;

  • sync-master-info:启用之可确保无信息丢失;

  • slave-paralles-workers:设定从服务器的SQL线程数;0表示关闭多线程复制功能;

  • binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:启用复制有关的所有校验功能;

  • binlog-rows-query-log-events:启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度;

  • log-bin:启用二进制日志,这是保证复制功能的基本前提;

  • server-id:同一个复制拓扑中的所有服务器的id号必须惟一。

(2).配置主服务器master


view sourceprint?
01.[root@master mysql]# vim my.cnf
02.binlog-format=ROW 
03.log-slave-updates=true
04.gtid-mode=on 
05.enforce-gtid-consistency=true
06.master-info-repository=TABLE 
07.relay-log-info-repository=TABLE 
08.sync-master-info=1 
09.slave-parallel-workers=2 
10.binlog-checksum=CRC32 
11.master-verify-checksum=1 
12.slave-sql-verify-checksum=1 
13.binlog-rows-query-log_events=1 
14.report-port=3306 
15.port=3306 
16.report-host=192.168.18.201
17.server_id = 1

(3).重新启动mysql


view sourceprint?
1.[root@master mysql]# service mysqld restart 
2.Shutting down MySQL.. SUCCESS! 
3.Starting MySQL...... SUCCESS!

(4).查看gtid的相关信息


view sourceprint?
01.[root@master mysql]# mysql -h127.0.0.1
02.Welcome to the MySQL monitor.  Commands end with ; or \g.
03.Your MySQL connectionid is 1
04.Server version: 5.6.13-log MySQL Community Server (GPL)
05.Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
06.Oracle is a registered trademark of Oracle Corporation and/or its
07.affiliates. Other names may be trademarks of their respective
08.owners.
09.Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
10.mysql&gt; show global variables like '%gtid%';
11.+--------------------------+-------+
12.| Variable_name            | Value |
13.+--------------------------+-------+
14.| enforce_gtid_consistency | ON    |
15.| gtid_executed            |       |
16.| gtid_mode                | ON    |#说明gti功能已启动
17.| gtid_owned               |       |
18.| gtid_purged              |       |
19.+--------------------------+-------+
20.5 rowsin set (0.10 sec)

(5).创建有复制权限的用户


view sourceprint?
1.mysql> GRANT REPLICATION SLAVE ON *.* TO'repluser'@'192.168.18.%'IDENTIFIED BY 'replpass'
2.Query OK,0 rows affected (0.44sec)
3.mysql> flush privileges; 
4.Query OK,0 rows affected (0.03sec)

(6).配置从服务器slave


view sourceprint?
01.[root@slave mysql]# vim my.cnf
02.relay-log = relay-log 
03.relay-log-index = relay-log.index 
04.binlog-format=ROW 
05.log-slave-updates=true
06.gtid-mode=on 
07.enforce-gtid-consistency=true
08.master-info-repository=TABLE 
09.relay-log-info-repository=TABLE 
10.sync-master-info=1 
11.slave-parallel-workers=2 
12.binlog-checksum=CRC32 
13.master-verify-checksum=1 
14.slave-sql-verify-checksum=1 
15.binlog-rows-query-log_events=1 
16.report-port=3306 
17.port=3306 
18.report-host=192.168.18.202
19.server_id = 10

(7).重新启动mysql


view sourceprint?
1.[root@slave mysql]# service mysqld restart 
2.Shutting down MySQL.. SUCCESS! 
3.Starting MySQL...... SUCCESS!

(8).在从服务器上使用主mysql上创建的账号密码登录并进行复制


view sourceprint?
1.mysql&gt; change master to master_host='192.168.18.201', master_user='repluser',master_pass<a class="keylink" href="http://www.it165.net/edu/ebg/"target="_blank">word</a>='replpass',master_auto_position=1; 
2.Query OK, 0 rows affected, 2 warnings (0.24 sec)
3.mysql&gt; start slave; 
4.Query OK, 0 rows affected, 1 warning (0.04 sec)

(9).查看一下复制状态


view sourceprint?
01.mysql&gt; show slave status\G 
02.*************************** 1. row *************************** 
03.               Slave_IO_State: Waitingfor master to send event 
04.                  Master_Host: 192.168.18.201 
05.                  Master_User: repluser 
06.                  Master_Port: 3306 
07.                Connect_Retry: 60 
08.              Master_Log_File: master-bin.000001 
09.          Read_Master_Log_Pos: 151 
10.               Relay_Log_File: relay-log.000002 
11.                Relay_Log_Pos: 363 
12.        Relay_Master_Log_File: master-bin.000001 
13.             Slave_IO_Running: Yes #IO线程与SQL线程都是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: 151 
25.              Relay_Log_Space: 561 
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.                  Master_UUID: 6b27d8b7-0e14-11e3-9eab-000c291192e4 
44.             Master_Info_File: mysql.slave_master_info 
45.                    SQL_Delay: 0 
46.          SQL_Remaining_Delay: NULL 
47.      Slave_SQL_Running_State: Slave hasread all relay log; waitingfor the slave I/O thread to update it 
48.           Master_Retry_Count: 86400 
49.                  Master_Bind: 
50.      Last_IO_Error_Timestamp: 
51.     Last_SQL_Error_Timestamp: 
52.               Master_SSL_Crl: 
53.           Master_SSL_Crlpath: 
54.           Retrieved_Gtid_Set: 
55.            Executed_Gtid_Set: 
56.                Auto_Position: 1 
57.1 rowin set (0.00 sec)

(10).测试一下主从复制

master:


view sourceprint?
01.mysql&gt; create database mydb; 
02.Query OK, 1 row affected (0.05 sec)
03.mysql&gt; show databases; 
04.+--------------------+ 
05.| Database           | 
06.+--------------------+ 
07.| information_schema | 
08.| mydb               | 
09.| mysql              | 
10.| performance_schema | 
11.|test              
12.+--------------------+ 
13.5 rowsin set (0.00 sec)

slave:


view sourceprint?
01.mysql&gt; show databases; 
02.+--------------------+ 
03.| Database           | 
04.+--------------------+ 
05.| information_schema | 
06.| mydb               | 
07.| mysql              | 
08.| performance_schema | 
09.|test              
10.+--------------------+ 
11.5 rowsin set (0.00 sec)

(11).查看一下复制状态


view sourceprint?
01.mysql&gt; show slave status \G 
02.*************************** 1. row *************************** 
03.               Slave_IO_State: Waitingfor master to send event 
04.                  Master_Host: 192.168.18.201 
05.                  Master_User: repluser 
06.                  Master_Port: 3306 
07.                Connect_Retry: 60 
08.              Master_Log_File: master-bin.000001 
09.          Read_Master_Log_Pos: 293 
10.               Relay_Log_File: relay-log.000002 
11.                Relay_Log_Pos: 505 
12.        Relay_Master_Log_File: master-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: 293 
25.              Relay_Log_Space: 703 
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.                  Master_UUID: 6b27d8b7-0e14-11e3-9eab-000c291192e4 
44.             Master_Info_File: mysql.slave_master_info 
45.                    SQL_Delay: 0 
46.          SQL_Remaining_Delay: NULL 
47.      Slave_SQL_Running_State: Slave hasread all relay log; waitingfor the slave I/O thread to update it 
48.           Master_Retry_Count: 86400 
49.                  Master_Bind: 
50.      Last_IO_Error_Timestamp: 
51.     Last_SQL_Error_Timestamp: 
52.               Master_SSL_Crl: 
53.           Master_SSL_Crlpath: 
54.           Retrieved_Gtid_Set: 6b27d8b7-0e14-11e3-9eab-000c291192e4:1 
55.            Executed_Gtid_Set: 53c8fa53-0e16-11e3-9eb8-000c29b8df6a:1-2, 
56.6b27d8b7-0e14-11e3-9eab-000c291192e4:1 
57.                Auto_Position: 1 
58.1 rowin set (0.00 sec)

好了,到此为止基于Gtid的mysql主从复制配置成功,希望大家有所收获。^_^……

0 0