標籤:mysql5.7 高可用 主從複製
MySQL 5.7主從複製
https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-howto.html
GTID(GlobalTransaction ID)是對於一個已提交事務的編號,並且是一個全域唯一的編號。GTID實際上是由UUID+TID組成的。其中UUID是一個MySQL執行個體的唯一標識。TID代表了該執行個體上已經提交的事務數量,並且隨著事務提交單調遞增。
設定gtid模式,在my.cnf裡面的mysqld選項卡裡面設定,設定完後,重啟mysql服務生效:
[mysqld]
gtid_mode=ON
log-slave-updates=ON
enforce-gtid-consistency=ON
mysql> change master to
master_host=‘主庫IP‘,
master_port=主庫連接埠,
master_user=‘repl‘,
master_password=‘repl密碼‘,
master_auto_position=1;
mysql> start slave;
PS:其中這裡有差別,以前非gtid的是用master_log_file=‘mysql-bin.000009‘,master_log_pos=154;而gtid就採用了master_auto_positon=1;來自動同步主庫的binlog了。
查看從庫複製狀態,是雙YES,而且Seconds_Behind_Master: 0:
Tell the slave to use the master with GTID based transactions as the replication data source, and to use GTID-based auto-positioning rather than file-based positioning. Issue a CHANGE MASTER TO statement on the slave, including the MASTER_AUTO_POSITION option in the statement to tell the slave that the master‘s transactions are identified by GTIDs.
查看備機資訊
1.主庫show slave hosts;
- SELECT * FROM information_schema.PROCESSLIST WHERE COMMAND = ‘Binlog Dump‘;
查看同步狀態
1.Show slave status\G
- SELECT SERVICE_STATE FROM performance_schema.replication_connection_status;
搭建級聯備庫
- 需設定log_slave_updates=1,使備庫產生binlog
- 備份資料庫並通過備份建立新資料庫
- 新資料庫與備庫同步
新資料庫改為與主庫同步
reset slave;
change master to
master_host=‘主庫IP‘,
master_port=主庫連接埠,
master_user=‘repl‘,
master_password=‘repl密碼‘,
master_auto_position=1;
- su - mysql
2.登入預設(3306)連接埠的執行個體
source .profile
mysql –uroot –p –h127.0.0.1 –P3306
3.登入3307連接埠的執行個體
source .p3307
mysql –uroot –p –h127.0.0.1 –P3307
知識點整理:
1、.mysql_history檔案記錄mysql使用者執行的命令,存在安全隱患,需要進行軟串連處理。lns -l /dev/null $HOME/.mysql_history
2、mysql 如何搭建半同步和非同步備庫。
3、mysql環境安裝。注意修改/relay_log檔案夾的屬主,VIP配置。
4 MySQL 5.7 高可用