48、mysql基於GTID的主從複製實戰
MySQL 5.6引入的GTID(Global Transaction IDs)使得其複製功能的配置、監控及管理變得更加易於實現,且更加健壯。
要在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號必須惟一;
一、Mysql基於GTID的主從複製實戰
1、準備工作
hostnamectl set-hostname www.mysql61.com
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.130.61 www.mysql61.com
192.168.130.62 www.mysql62.com
192.168.130.63 www.mysql63.com
關防火牆、selinux
firewall-cmd --state
systemctl stop firewalld
firewall-cmd --state
systemctl disable firewalld
vim /etc/selinux/config
setenforce 0
修改UUID
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
vim /mydata/data/auto.cnf
2、配置master節點:
[mysqld]
binlog-format=ROW
log-bin=/mydata/binlog/log-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
server-id=61
report-port=3306
port=3306
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=www.mysql61.com
mysql
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.130.%' IDENTIFIED BY 'replpass';
FLUSH PRIVILEGES;
3、配置slave節點:
[mysqld]
binlog-format=ROW
log-bin=/mydata/binlog/log-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
server-id=62
report-port=3306
port=3306
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=www.mysql62.com
4、為備節點提供初始資料集
鎖定主表,備份主節點上的資料,將其還原至從節點;如果沒有啟用GTID,在備份時需要在master上使用show master status命令檢視二進位制日誌檔名稱及事件位置,以便後面啟動slave節點時使用。
master節點
mysql
CREATE DATABASE mydb;
mysqldump --all-databases --lock-all-tables --flush-logs --master-data=2 > all.sql
scp all.sql [email protected]:/tmp
slave節點
mysql < /tmp/all.sql
head -30 /tmp/all.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='log-bin.000016', MASTER_LOG_POS=194;
5、啟動從節點的複製執行緒
如果啟用了GTID功能,則使用如下命令:
CHANGE MASTER TO MASTER_HOST='www.mysql61.com', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_AUTO_POSITION=1;
沒啟用GTID,需要使用如下命令:
CHANGE MASTER TO MASTER_HOST='192.168.130.61',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='log-bin.000014',MASTER_LOG_POS=154;
master節點
START SLAVE;
SHOW SLAVE STATUS\G;
show processlist;
slave節點
show slave hosts;
mysql < hellodb.sql
MariaDB GTID:
文件中應用MariaDB-10,需要做的修改:
1、不支援的引數:
gtid-mode=on
enforce-gtid-consistency=true
2、修改的引數:
slave-parallel-workers引數修改為slave-parallel-threads
3、連線至主服務使用的命令:
一個新的引數:MASTER_USER_GTID={current_pos|slave_pos|no}
CHANGE MASTER TO master_host="127.0.0.1", master_port=3310, master_user="root", master_use_gtid=current_pos;