1. 程式人生 > >maridb 主從復制

maridb 主從復制

init show 多少 conn external number action 通用 pri

maridb(>10.0)主從復制

1.環境說明

  • 測試環境

系統版本:Ubuntu 16.04.5 LTS
內核版本:4.4.0-141-generic x86_64
MariaDB版本:mysql Ver 15.1 Distrib 10.0.36-MariaDB
主服務器:192.168.100.179 xyx-test01.lingda.com
從服務器:192.168.100.70 xyx0001.lingda.com
  • 官方:https://mariadb.com/kb/en/library/replication-overview/#standard-replication

2.MariaDB配置

  • /etc/mysql/mariadb.conf.d/50-server.cnf #通用配置

    [mysqld]
    binlog-format=ROW
    log-slave-updates=True # slave更新是否記入日誌
    master-info-repository=TABLE
    relay-log-info-repository=TABLE # 此兩項為打開從服務器崩潰二進制日誌功能,信息記錄在事物表而不是保存在文件
    sync-master-info=1 # 值為1確保信息不會丟失
    slave-parallel-threads=1 #同時啟動多少個復制線程,最多與要復制的數據庫數量相等即可
    binlog-checksum=CRC32 # 效驗碼
    master-verify-checksum=1 # 啟動主服務器效驗
    slave-sql-verify-checksum=1 # 啟動從服務器效驗
    binlog-rows-query-log-events=1 # 用於在二進制日誌詳細記錄事件相關的信息,可降低故障排除的復雜度;
  • master #添加配置

    [mysqld]
    server-id = 1
    log_bin = /data/mysql/maridb-bin/maridb-bin.log
  • 重啟master數據庫

    [email protected]:~# /etc/init.d/mysql restart
    [email protected]:~# ls /data/mysql/maridb-bin/ #生成日誌文件
    maridb-bin.000001 maridb-bin.index
  • slave添加配置

    server-id               = 2
    relay_log = /var/lib/mysql/maridb-bin/maridb-relay-bin.log
  • 重啟slave數據庫

    [email protected]:~# /etc/init.d/mysql restart

3.導出數據

  • 保證數據一致性

    主要考慮的問題是maridb從節點slave是在主節點master運行了一段時間之後才接入,常規同步時鎖表(master)導出老數據,記錄當前二進制日誌文件maridb-bin.log和位置postion,但鎖表會影響業務正常使用;故采用方法二:通過gtid的方式同步,導出老數據的時候文件開頭自帶當前二進制日誌文件名、位置postion、gtid值,導出數據時帶入參數: --master-data=2,記錄二進制日誌文件名、位置postion、gtid值 --single-transaction,導出老數據過程不鎖表

    [email protected]:/data# mysqldump -uzabbix -pzabbix --master-data=2 --single-transaction zabbix |gzip > zabbix.sql.gz
  • 日誌樣例

-- CHANGE MASTER TO MASTER_LOG_FILE=‘maridb-bin.000005‘, MASTER_LOG_POS=3554718;
--
-- GTID to start replication from
--
28 -- SET GLOBAL gtid_slave_pos=‘0-1-465‘;

4.創建同步權限用戶

master:

MariaDB [zabbix]>GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘replica‘@‘%‘ identified by ‘replica‘;
MariaDB [zabbix]>flush privileges;

5.同步數據

  • 導入數據

[email protected]:/var/lib/mysql# nohup mysql -uzabbix -pzabbix zabbix < zabbix.sql &
  • 啟動同步(slave)

MariaDB [(none)]> SET GLOBAL gtid_slave_pos=‘0-1-465‘;
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST=‘192.168.100.179‘,
-> MASTER_USER=‘replica‘,
-> MASTER_PASSWORD=‘lingdasa‘,
-> MASTER_PORT=3306,
-> MASTER_USE_GTID=slave_pos;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.179
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: maridb-bin.000007
Read_Master_Log_Pos: 87457185
Relay_Log_File: maridb-relay-bin.000007
Relay_Log_Pos: 64698613
Relay_Master_Log_File: maridb-bin.000007
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: 64698321
Relay_Log_Space: 192672037
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: 2839
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: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: Slave_Pos
Gtid_IO_Pos: 0-1-884
  • 部分參數說明

Slave_IO_Running: Yes、Slave_SQL_Running: Yes #表示同步狀態正常

Master_Log_File和Relay_Master_Log_File 對應文件名相同,則slave當前讀的日誌和master當前最新的日誌文件相同

Exec_Master_Log_Pos <= Read_Master_Log_Pos #slave同步的二進制日誌位置小於等於主上二進制日誌位置

maridb 主從復制