萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL 5.6 GTID 主從複制 + Amoeba 讀寫分離完整部署實戰

架構總覽


[Client] → [Amoeba Proxy:3306] → [Master:172.16.18.1:3306] (寫)
                              → [Slave:172.16.18.2:3306] (讀)

第一部分:MySQL 5.6 GTID 主從複制

核心概念

  • GTID (Global Transaction ID) = source_id:transaction_id,全局唯一
  • source_id = 服務器 server_uuid(128位隨機數)
  • 優勢:故障切換無需手動找 Binlog 位置、多級複制拓撲自動定位、數據一致性強保證

環境

  • Master (node1):172.16.18.1,server-id=1
  • Slave (node2):172.16.18.2,server-id=10
  • MySQL 5.6.13,二進制包安裝至 /usr/local/mysql

Master 配置 /etc/my.cnf 關鍵項


[mysqld]
binlog_format=row
server-id=1
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=4
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
report-port=3306
report-host=node1.magedu.com

建立複制用戶


GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'rpuser'@'%' IDENTIFIED BY 'rppass';
FLUSH PRIVILEGES;

Slave 配置差異


server-id=10
report-host=node2.magedu.com

啟動複制(GTID 自動定位,無需 MASTER_LOG_FILE/POS)


CHANGE MASTER TO
    MASTER_HOST='172.16.18.1',
    MASTER_USER='rpuser',
    MASTER_PASSWORD='rppass',
    MASTER_AUTO_POSITION=1;
START SLAVE;

驗證


SHOW SLAVE STATUS\G
# 關鍵:Slave_IO_Running=Yes, Slave_SQL_Running=Yes, Auto_Position=1
# Retrieved_Gtid_Set / Executed_Gtid_Set 應一致

第二部分:Amoeba 讀寫分離部署

環境

  • Amoeba:172.16.18.6 (雙網卡,外網/內網)
  • JDK 1.6.0_31(Amoeba 2.2 僅驗證 1.6)
  • Amoeba 2.2.0 解壓至 /usr/local/amoeba

核心配置 amoeba.xml



    3306
    0.0.0.0
    
        
            128
            64
        
    
    
        
            root
            mypass
            
                
                    ${amoeba.home}/conf/access_list.conf
                
            
        
    



    node1
    node1
    node2
    true

後端數據源 dbServers.xml



    
        3306
        test
        root
        mypass
    
    
        500
        500
        10
    



    
        192.168.18.1
    



    
        192.168.18.2
    

啟動與驗證


amoeba start &
ss -tanl | grep 3306  # 應監聽 :::3306

mysql -uroot -h172.16.18.6 -pmypass
# Server version: 5.1.45-mysql-amoeba-proxy-2.2.0
SELECT VERSION();  -- 顯示 5.6.13-log,證明轉發至後端

抓包驗證讀寫分離


# Master 抓寫
tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host 192.168.18.1
# 出現 "create database yong"

# Slave 抓讀
tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host 192.168.18.2
# 出現 "select User,Host from mysql.user"

Amoeba 既有局限

  • 不支援分布式事務(XA)
  • 複雜 SQL 路由解析可能失敗
  • 單點故障(Amoeba 自身無 HA,需 LVS/Keepalived 前置)
  • 社區維護停滯,新版 MySQL 兼容性存疑

現代替代方案

中間件特點
ProxySQL高性能、動態配置、查詢重寫、鏡像、原生 GTID 支援
MyCat分片/讀寫分離/多租戶、活躍社區
ShardingSphereJava 生態、分片/讀寫分離/影子表/加密
MaxScaleMariaDB 官方、Binlog 路由、自動故障切換

新專案強烈建議 ProxySQL 或 MyCat,Amoeba 僅作歷史系統維護參考。

copyright © 萬盛學電腦網 all rights reserved