Mariadb 10.2.8版本GTID主從環境搭建以及切換
1.首先搭建主從
主環境:192.168.1.117
從環境:192.168.1.123
a.首先以二進制包的形式安裝好MariaDB (忽略不計)
b.配置環境的變量
通配
[mysqld]
binlog-format=row
log-slave-updates=True
master-info-repository=table
relay-log-info-repository=table
sync-master-info=1
slave-parallel-threads=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log-events=1
我主從服務器都配置log-bin以及relay-log
主服務器
server_id = 100
log-bin=mysql-bin、
relay_log=relay-bin
從服務器
server_id=101
log-bin=mysql-bin
relay_log=relay-bin
c.先使用主機器給從機器授權
主上操作
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘slave‘@‘192.168.1.123‘ IDENTIFIED BY "密碼"
show master status 查看從服務器的 bin-log 文件以及 偏移量
select binlog_gtid_pos("mysql-bin.000001",1696); 查看gtid號
d. 啟動同步
set global gtid_slave_pos=‘0-1001-58‘;
change master to
master_host=‘192.168.1.123‘,
master_user=‘slave‘,
master_password=‘密碼‘,
master_port=3306,
master_use_gtid=slave_pos;
start slave;
這裏和以前基於二進制日誌進行復制的區別:需要設置全局 gtid_slave_pos值 ,並且使用Master_use_gtid語句
查看同步狀態,無錯誤即正常
F.可以進行主從的數據
看主從數據是 否一致
以上沒有考慮到數據量過大的情況,如果數據量過大,還是現在主庫上做備份,然後導到從庫上
2.故障切換模擬
a.殺死mysql進程 模擬數據庫掛掉的情況
ps -ef|grep mysql
kill -9 pid
b.在從服務器上將 slave 去除掉
reset slave all;
c.授權給從庫
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘slave‘@‘192.168.1.117‘ IDENTIFIED BY "密碼"
d. 在新的從庫上啟動 復制
-
從庫采用gtid的復制,語法為:CHANGE MASTER TO master_use_gtid = { slave_pos | current_pos | no }
-
一般使用slave_pos,當A->B,A掛掉,B當master,然後A好了,想要做B的slave情況下,使用current_pos,因為B以前是主庫,沒有slave_pos這個值
由官網得知
change master to
master_host=‘192.168.1.117‘,
master_user=‘slave‘,
master_password=‘密碼‘,
master_port=3306,
master_use_gtid=current_pos;
start slave;
e.查看新的從庫的信息
show slave status\G;
f. 測試一下看主從是否一致
Mariadb 10.2.8版本GTID主從環境搭建以及切換