1. 程式人生 > >四、mysql集群-基於GTID的主從復制

四、mysql集群-基於GTID的主從復制

space lin pac 為我 eth mar pos sql_mode conn

mysql集群采用MHA+mycat+mysql主從復制+mycat-web監控構成由於篇幅過長 將分步驟發布

技術分享圖片

這裏因為我電腦內存限制 沒有做mycat的高可用 可以使用HAporxy+Keppalive實現mycat的高可用這樣集群中就不存在單點故障



簡介

基於GTID的復制是MySQL 5.6後新增的復制方式.

GTID (global transaction identifier) 即全局事務ID, 保證了在每個在主庫上提交的事務在集群中有一個唯一的ID.

在原來基於日誌的復制中, 從庫需要告知主庫要從哪個偏移量進行增量同步, 如果指定錯誤會造成數據的遺漏, 從而造成數據的不一致.

而基於GTID的復制中, 從庫會告知主庫已經執行的事務的GTID的值, 然後主庫會將所有未執行的事務的GTID的列表返回給從庫. 並且可以保證同一個事務只在指定的從庫執行一次.


1.mysql配置基於gtid主從復制

安裝腳本
id=`ifconfig eth0 | awk -F "[ :]+" 'NR==2 {print $4}' | cut -d . -f 4`
useradd -s /sbin/nologin mysql -M
cd /application/tools/
tar xf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 
ln -s /application/tools/mysql-5.7.21-linux-glibc2.12-x86_64/ /application/mysql && echo success
cd /application
mkdir /application/mysql/{log,data,tmp} -p
chown -R mysql:mysql mysql
cd /application/mysql
chown -R mysql:mysql ./* 
cat << EOF >/etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server_id= $id
port = 3306
user = mysql
character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = /application/mysql
datadir = /application/mysql/data
pid-file = /application/mysql/data/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:1024M:autoextend
innodb_undo_tablespaces=3
######====================================[log]==============================
log_error = /application/mysql/log/mysql-error.log 
slow_query_log = 1
long_query_time = 1 
slow_query_log_file = /application/mysql/log/mysql-slow.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log-bin=mysql-bin
##################slave設置參數##################
relay_log_purge=0
######====================================[gtid]============================
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
EOF
cd /application/tools/mysql-5.7.21-linux-glibc2.12-x86_64/bin
./mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp && echo success
cp /application/tools/mysql-5.7.21-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
service mysql start 
cat << EOF >>/etc/profile
#for mysql 
PATH=$PATH:/application/mysql/bin
EOF
source /etc/profile && echo success
password=`grep -i password /application/mysql/log/mysql-error.log | awk -F '[ :]+' '{print $14}' | head -1`
mysqladmin -uroot -p$password password 123456


創建復制用戶 在master1中創建一個從機中可以登錄的MySQL用戶

GRANT REPLICATION SLAVE ON *.* TO 'mysql01'@'172.16.1.%' IDENTIFIED BY 'mysql01';
FLUSH PRIVILEGES;
master2
CHANGE MASTER TO
MASTER_HOST='172.16.1.103',
MASTER_USER='mysql01',
MASTER_PASSWORD='mysql01',
MASTER_AUTO_POSITION=1;
slave
CHANGE MASTER TO
MASTER_HOST='172.16.1.103',
MASTER_USER='mysql01',
MASTER_PASSWORD='mysql01',
MASTER_AUTO_POSITION=1;


開啟復制

master2
START SLAVE; 

slave
START SLAVE;


查看主從復制是否配置成功

master2
SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.103
                  Master_User: mysql01
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1200
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 472
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            
slave
SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.103
                  Master_User: mysql01
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1200
               Relay_Log_File: slave-relay-bin.000003
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes


四、mysql集群-基於GTID的主從復制