1. 程式人生 > >CentOS6.8下MySQL5.6.40基於GTID主從及多線程復制

CentOS6.8下MySQL5.6.40基於GTID主從及多線程復制

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 [email protected]:/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.
[email protected]‘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主從及多線程復制