码迷,mamicode.com
首页 > 数据库 > 详细

CentOS6.8下MySQL5.6.40基于GTID主从及多线程复制

时间:2018-05-12 11:56:55      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:GTID 复制 mysql

大纲

一 GTID简介

二 环境准备

三 数据库的安装

四 基于GTID主从配置步骤

五 验证GTID复制功能

一 GTID简介

GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。下面是一个GTID的具体形式3E11FA47-71CA-11E1-9E33-C80AA9429562:23
更详细的介绍可以参见:官方文档

GTID的作用
具体归纳主要有以下两点:
? 根据GTID可以知道事务最初是在哪个实例上提交的
? GTID的存在方便了Replication的Failover
这里详细解释下第二点。我们可以看下在MySQL 5.6的GTID出现以前replication failover的操作过程。假设我们有一个如下图的环境

此时,Server A的服务器宕机,需要将业务切换到Server B上。同时,我们又需要将Server C的复制源改成Server B。复制源修改的命令语法很简单即CHANGE MASTER TO MASTER_HOST=‘xxx‘, MASTER_LOG_FILE=‘xxx‘, MASTER_LOG_POS=nnnn。而难点在于,由于同一个事务在每台机器上所在的binlog名字和位置都不一样,那么怎么找到Server C当前同步停止点,对应Server B的master_log_file和master_log_pos是什么的时候就成为了难题。这也就是为什么M-S复制集群需要使用MMM,MHA这样的额外管理工具的一个重要原因。
这个问题在5.6的GTID出现后,就显得非常的简单。由于同一事务的GTID在所有节点上的值一致,那么根据Server C当前停止点的GTID就能唯一定位到Server B上的GTID。甚至由于MASTER_AUTO_POSITION功能的出现,我们都不需要知道GTID的具体值,直接使用CHANGE MASTER TO MASTER_HOST=‘xxx‘, MASTER_AUTO_POSITION命令就可以直接完成failover的工作。

二 环境准备

系统环境 CentOS6.8 X86_64
master.com master 192.168.4.34
slave.com slave 192.168.4.58
软件包
mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

三 数据库的安装

1、时间同步
[root@master ~]# ntpdate s2c.time.edu.cn
[root@slave ~]# ntpdate s2c.time.edu.cn
在每个节点定义crontab任务
[root@Paul ~]# which crontab
/usr/bin/crontab
[root@Paul ~]# echo "/5 /sbin/ntpdate s2c.time.edu.cn $> /dev/null" >>/var/spool/cron/root
[root@Paul ~]# crontab –l
/5 /sbin/ntpdate s2c.time.edu.cn $> /dev/null

将crontab文件拷贝进slave
[root@Paul ~]# scp /var/spool/cron/root root@192.168.4.88:/var/spool/cron/

The authenticity of host ‘192.168.4.88 (192.168.4.88)‘ can‘t be established.
RSA key fingerprint is 5f:6c:92:59:f5:a1:b0:a4:6c:a6:b9:93:15:42:4c:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.4.88‘ (RSA) to the list of known hosts.
root@192.168.4.88‘s password:
root 100% 55 0.1KB/s 00:00
You have new mail in /var/spool/mail/root
[root@Paul ~]#

2、修改主机名
master:
[root@master ~]# hostname master.com
[root@master ~]# sed -i ‘s@(HOSTNAME=).*@\1master.com@g‘ /etc/sysconfig/network
[root@master ~]# more /etc/sysconfig/network
[root@master ~]# hostname
master.com

slave:
[root@master ~]# hostname slave.com
[root@slave ~]# sed -i ‘s@(HOSTNAME=).*@\1slave.com@g‘ /etc/sysconfig/network
[root@slave ~]# hostname
slave.com

master添加hosts解析
[root@master ~]# vim /etc/hosts
127.0.0.1 localhostlocalhost.localdomain localhost4 localhost4.localdomain4
::1localhostlocalhost.localdomain localhost6 localhost6.localdomain6
192.168.4.58 master.com master
192.168.4.88 slave.com slave

拷贝此hosts文件至slave
[root@master ~]# scp /etc/hosts slave:/etc/
The authenticity of host ‘slave (192.168.4.88)‘ can‘t be established.
RSA key fingerprint is 5f:6c:92:59:f5:a1:b0:a4:6c:a6:b9:93:15:42:4c:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘slave‘ (RSA) to the list of known hosts.
root@slave‘s password:
hosts100% 218 0.2KB/s 00:00

3、关闭master和slave的iptables和selinux
master
[root@master ~]# serviceiptables stop
[root@master ~]# vim /etc/sysconfig/selinux

#This file controls the state of SELinux on the system.
#SELINUX= can take one of these three values:
#enforcing - SELinux security policy is enforced.
#permissive - SELinux prints warnings instead of enforcing.
#disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
#SELINUXTYPE= can take one of these two values:
#targeted - Targeted processes are protected,
#mls - Multi Level Security protection.
SELINUXTYPE=targeted

slave
[root@master ~]# serviceiptables stop
[root@master ~]# vim /etc/sysconfig/selinux

#This file controls the state of SELinux on the system.
#SELINUX= can take one of these three values:
#enforcing - SELinux security policy is enforced.
permissive - SELinux prints warnings instead of enforcing.
disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
#SELINUXTYPE= can take one of these two values:
#targeted - Targeted processes are protected,
#mls - Multi Level Security protection.
#SELINUXTYPE=targeted

4、Master安装并配置MySQL
数据目录底层最好是个逻辑卷,此处没有使用逻辑卷
准备数据目录并添加mysql用户
[root@master ~]# mkdir -pv /data/mysql
[root@master ~]# groupadd -g 3306 mysql
[root@master ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[root@master ~]#chown -R mysql.mysql /data/mysql

解压并初始化mysql
[root@master ~]# tarxf ~/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@master ~]# cd /usr/local/
[root@master local]# ln -sv mysql-5.6.40-linux-glibc2.12-x86_64/ mysql
[root@master local]# cdmysql
[root@mastermysql]# chown -R root.mysql ./*
[root@mastermysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

复制启动脚本
[root@mastermysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@mastermysql]# chkconfig --add mysqld

增加PATH环境变量并使之生效
[root@mastermysql]# echo ‘PATH=$PATH:/usr/local/mysql/bin‘ > /etc/profile.d/mysql.sh
[root@mastermysql]# source /etc/profile.d/mysql.sh
输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man

5、Slave安装并配置MySQL
准备数据目录并添加mysql用户
[root@slave ~]# mkdir -pv /data/mysql
[root@slave ~]# groupadd -g 3306 mysql
[root@slave ~]# useradd -g 3306 -u 3306 -s /sbin/nologin -M mysql
[root@slave ~]# chown -R mysql.mysql /data/mysql

解压并初始化mysql
[root@slave ~]# tar xf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz -C /usr/local
root@slave ~]# mv /usr/local/mysql-5.6.40-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@slave ~]# chown -R mysql.mysql /usr/local/mysql

[root@slave ~]# cd /usr/local/mysql
[root@slavemysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysq
l
复制服务启动脚本
[root@slavemysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@slavemysql]# chkconfig --add mysqld
增加PATH环境变量并使之生效
[root@slavemysql]#echo ‘PATH=$PATH:/usr/local/mysql/bin‘ > /etc/profile.d/mysql.sh
[root@slavemysql]#source /etc/profile.d/mysql.sh

输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man

四 基于GTID的主从模式配置过程

基于GTID的主从复制简介
根据MySQL官方文档给出的GTID搭建建议。需要一次对主从节点做配置修改,并重启服务。这样的操作,显然在production环境进行升级时是不可接受的。Facebook,Booking.com,Percona都对此通过patch做了优化,做到了更优雅的升级。具体的操作方式会在以后的博文当中介绍到。这里我们就按照官方文档,进行一次实验性的升级。
主要的升级步骤会有以下几步:
? 确保主从同步
? 在master上配置read_only,保证没有新数据写入
? 修改master上的my.cnf,并重启服务
? 修改slave上的my.cnf,并重启服务
? 在slave上执行change master to并带上master_auto_position=1启用基于GTID的复制
由于是实验环境,read_only和服务重启并无大碍。只要按照官方的GTID搭建建议做就能顺利完成升级

要在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号必须惟一;

分别为主从数据库提供配置文件/etc/my.cnf
1、 编辑master配置文件 /etc/my.cnf

basedir = /usr/local/mysql
datadir = /data/mysql
server-id = 34
socket = /tmp/mysql.sock
innodb_file_per_table=on
binlog-format=ROW
log-bin=mysql-bin
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
port=3306
report-port=3306
report-host=192.168.4.34

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid启动服务并查看gtid是否生效

2、重启服务,使修改配置生效
[root@mastermysql]# service mysqld start
[root@mastermysql]# mysql

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 151 | | | |
+------------------+----------+--------------+------------------+-------------------+

mysql> SHOW GLOBAL VARIABLES LIKE ‘%gtid%‘;
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| binlog_gtid_simple_recovery | OFF |
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| simplified_binlog_gtid_recovery | OFF |
+---------------------------------+-------+
3、查看master的uuid
mysql> SHOW GLOBAL VARIABLES LIKE ‘%uuid%‘;
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 08003687-5529-11e8-88d7-000c29138f00 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

4、编辑slave配置文件 /etc/my.cnf
[mysqld]
binlog-format=row
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=58
report=3306
port=3306
socket=/tmp/mysql.sock
#Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

5、重启slave服务
[root@slave ~]# service mysqld restart
[root@slave ~]# mysql
mysql> SHOW GLOBAL VARIABLES LIKE ‘%uuid%‘;
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | c3888692-552c-11e8-88ef-000c29f5e0c1 |
+---------------+--------------------------------------+

6、master建立复制账号
mysql> GRANT REPLICATION SLAVE ON . TO ‘repluser‘@‘192.168.%.%‘ IDENTIFIED BY ‘123456‘;
mysql> FLUSH PRIVILEGES;

7、启动从节点复制线程
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.4.34‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘123456‘,
MASTER_AUTO_POSITION=1;
mysql> SHOW WARNINGS;

8、启动slave,并查看slave状态
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.34
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 538
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 408
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
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: 538
Relay_Log_Space: 606
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: 34
Master_UUID: 08003687-5529-11e8-88d7-000c29138f00
Master_Info_File: mysql.slave_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: 08003687-5529-11e8-88d7-000c29138f00:1-2
Auto_Position: 1

五 验证GTID复制功能

1、在master创建测试数据库
mysql> CREATE DATABASE mydb;
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |

2、在master上查看从服务器状态
mysql> SHOW SLAVE HOSTS;
+-----------+--------------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+--------------+------+-----------+--------------------------------------+
| 58 | 192.168.4.58 | 3306 | 34 | c3888692-552c-11e8-88ef-000c29f5e0c1 |
+-----------+--------------+------+-----------+--------------------------------------+

mysql> SHOW GLOBAL VARIABLES LIKE ‘%gtid%‘;
+---------------------------------+------------------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery | OFF |
| enforce_gtid_consistency | ON |
| gtid_executed | 08003687-5529-11e8-88d7-000c29138f00:1-3 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| simplified_binlog_gtid_recovery | OFF |
+---------------------------------+------------------------------------------

3、查看master上已经执行的事务数
mysql> mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 680 | |08003687-5529-11e8-88d7-000c29138f00:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+

4、在slave上查看二进制日志
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000003 | 680 | | | 08003687-5529-11e8-88d7-000c29138f00:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------

5、查看maser二进制日志内容:
[root@master ~]# cd /data/mysql
/!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1/;
/!40019 SET @@session.max_insert_delayed_threads=0/;
/!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0/;
DELIMITER /!/;
at 4
#180512 9:41:11 server id 34 end_log_pos 120 CRC32 0x5cadd884 Start: binlog v 4, server v 5.6.
40-log created 180512 9:41:11 at startup# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/!/;
BINLOG ‘
N0b2Wg8iAAAAdAAAAHgAAAABAAQANS42LjQwLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA3RvZaEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAYTY
rVw=
‘/!/;
at 120
#180512 9:41:11 server id 34 end_log_pos 151 CRC32 0x9c4d66e7 Previous-GTIDs
[empty]
at 151
#180512 9:55:39 server id 34 end_log_pos 199 CRC32 0x6347a863 GTID [commit=yes]
SET @@SESSION.GTID_NEXT= ‘08003687-5529-11e8-88d7-000c29138f00:1‘/!/;
at 199
#180512 9:55:39 server id 34 end_log_pos 411 CRC32 0x6cdc6812 Query thread_id=3 exec_tim
e=0 error_code=0SET TIMESTAMP=1526090139/!/;
SET @@session.pseudo_thread_id=3/!/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.a
utocommit=1/!/;SET @@session.sql_mode=1075838976/!/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/!/;
/!\C utf8 //!/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/!
/;SET @@session.lc_time_names=0/!/;
SET @@session.collation_database=DEFAULT/!/;
GRANT REPLICATION SLAVE ON . TO ‘repluser‘@‘192.168.%.%‘ IDENTIFIED BY PASSWORD ‘6BB4837EB74329105EE4
568DDA7DC67ED2CA2AD9‘/
!/;
at 411
#180512 9:55:46 server id 34 end_log_pos 459 CRC32 0xb750c713 GTID [commit=yes]
SET @@SESSION.GTID_NEXT= ‘08003687-5529-11e8-88d7-000c29138f00:2‘/
!/;
at 459
#180512 9:55:46 server id 34 end_log_pos 538 CRC32 0x2bd7b613 Query thread_id=3 exec_tim
e=0 error_code=0SET TIMESTAMP=1526090146/
!/;
FLUSH PRIVILEGES
/
!/;
at 538
#180512 10:03:13 server id 34 end_log_pos 586 CRC32 0x9b4299c3 GTID [commit=yes]
SET @@SESSION.GTID_NEXT= ‘08003687-5529-11e8-88d7-000c29138f00:3‘/
!/;
at 586
#180512 10:03:13 server id 34 end_log_pos 680 CRC32 0x9112c7b3 Query thread_id=3 exec_tim
e=0 error_code=0SET TIMESTAMP=1526090593/
!/;
CREATE DATABASE mydb
/
!/;
SET @@SESSION.GTID_NEXT= ‘AUTOMATIC‘ /
added by mysqlbinlog //!/;
DELIMITER ;
End of log file
ROLLBACK /
added by mysqlbinlog /;
/
!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE/;
/
!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

注意:可以观察到日志事务号和UUID

CentOS6.8下MySQL5.6.40基于GTID主从及多线程复制

标签:GTID 复制 mysql

原文地址:http://blog.51cto.com/437549/2115456

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!