1. 首页 > 国产数据库教程 > GreatSQL教程 > 正文

greatsql教程FG014-GreatSQL集群与主从复制实战

内容简介

本教程详细介绍GreatSQL数据库的集群和主从复制,包括主从复制配置、集群部署、高可用方案等内容。风哥教程参考GreatSQL官方文档集群和复制指南,帮助读者掌握数据库集群和主从复制的最佳实践。

集群和主从复制是提高数据库可用性和性能的重要手段,合理的集群架构可以确保系统的高可用性和数据安全性。本教程将从基础概念入手,逐步深入到实战案例和最佳实践。

目录大纲

Part01-基础概念与理论知识

1.1 主从复制概述

主从复制是GreatSQL提供的一种数据同步机制,主要功能包括:

  • 数据备份:从库可以作为主库的备份
  • 读写分离:主库负责写操作,从库负责读操作
  • 负载均衡:多个从库可以分担读负载
  • 高可用性:当主库故障时,从库可以提升为主库

1.2 集群概述

GreatSQL集群是由多个数据库节点组成的系统,主要类型包括:

  • 主从集群:一主多从架构
  • 双主集群:互为主从的架构
  • 多主集群:多个主库的架构
  • Galera集群:同步复制的多主集群

1.3 高可用架构

高可用架构的核心目标是:

  • 减少系统 downtime
  • 确保数据一致性
  • 提高系统可靠性
  • 实现自动故障切换

Part02-生产环境规划与建议

2.1 复制架构规划

风哥提示:复制架构应根据业务需求和数据量进行规划,确保性能和可靠性。

复制架构规划建议:

  • 一主一从:适合小型应用,简单可靠
  • 一主多从:适合读密集型应用,提高读性能
  • 级联复制:适合大型架构,减少主库负载
  • 双主复制:适合需要双向写入的场景

2.2 集群架构规划

集群架构规划建议:

  • 选择合适的集群类型
  • 确定节点数量和角色
  • 配置网络拓扑
  • 设置故障切换策略
  • 规划监控和管理方案

2.3 网络规划

网络规划建议:

  • 使用专用网络进行复制
  • 确保网络带宽充足
  • 配置网络延迟监控
  • 实现网络冗余

更多视频教程www.fgedu.net.cn

Part03-生产环境项目实施方案

3.1 主从复制配置方案

主从复制配置步骤:

  1. 配置主库参数
  2. 创建复制用户
  3. 备份主库数据
  4. 配置从库参数
  5. 启动复制进程
  6. 验证复制状态

3.2 集群部署方案

集群部署步骤:

  1. 准备集群节点
  2. 安装GreatSQL
  3. 配置集群参数
  4. 初始化集群
  5. 添加节点
  6. 验证集群状态

3.3 监控与管理方案

监控与管理方案:

  • 监控复制状态
  • 监控集群健康状态
  • 设置告警机制
  • 定期备份数据
  • 制定故障切换流程

Part04-生产案例与实战讲解

4.1 主从复制配置实战

# 配置主库参数
cat >> /etc/my.cnf << EOF
# 主库复制配置 server-id = 1 log-bin = /greatsql/logs/mysql-bin binlog-format = ROW binlog-row-image = FULL enforce-gtid-consistency = ON gtid-mode = ON EOF
# 重启主库
systemctl restart greatsql

# 创建复制用户
mysql -u root -pFGedu123456! -e “CREATE USER ‘repl’@’%’ IDENTIFIED BY ‘Repl@123456’;” mysql -u root -pFGedu123456! -e “GRANT REPLICATION SLAVE ON *.* TO ‘repl’@’%’;” mysql -u root -pFGedu123456! -e “FLUSH PRIVILEGES;”

Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)

# 获取主库状态
mysql -u root -pFGedu123456! -e “SHOW MASTER STATUS;”

+—————+———-+————–+——————+——————————————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+—————+———-+————–+——————+——————————————+
| mysql-bin.000001 | 156 | | | 00000001-0000-0000-0000-000000000001:1-5 |
+—————+———-+————–+——————+——————————————+

学习交流加群风哥微信: itpux-com

# 配置从库参数
cat >> /etc/my.cnf << EOF
# 从库复制配置 server-id = 2 relay-log = /greatsql/logs/relay-bin read-only = ON enforce-gtid-consistency = ON gtid-mode = ON EOF
# 重启从库
systemctl restart greatsql

# 配置从库复制
mysql -u root -pFGedu123456! -e “CHANGE MASTER TO \ MASTER_HOST=’192.168.1.100′, \ MASTER_USER=’repl’, \ MASTER_PASSWORD=’Repl@123456′, \ MASTER_PORT=3306, \ MASTER_AUTO_POSITION=1;”

Query OK, 0 rows affected, 2 warnings (0.01 sec)

# 启动复制
mysql -u root -pFGedu123456! -e “START SLAVE;”

Query OK, 0 rows affected (0.01 sec)

# 查看复制状态
mysql -u root -pFGedu123456! -e “SHOW SLAVE STATUS\G”

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 156
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
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: 156
Relay_Log_Space: 524
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: 0
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_UUID: 550e8400-e29b-41d4-a716-446655440000
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 00000001-0000-0000-0000-000000000001:1-5
Executed_Gtid_Set: 00000001-0000-0000-0000-000000000001:1-5, 00000002-0000-0000-0000-000000000002:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:

学习交流加群风哥QQ113257174

4.2 集群部署实战

# 安装GreatSQL集群
# 在所有节点上执行 rpm -ivh greatsql-8.0.32-24.x86_64.rpm

# 配置集群参数
cat >> /etc/my.cnf << EOF
# 集群配置 server-id = 1 log-bin = /greatsql/logs/mysql-bin binlog-format = ROW innodb_flush_log_at_trx_commit = 1 sync_binlog = 1
# Galera集群配置 wsrep_on = ON wsrep_cluster_name = ‘greatsql_cluster’ wsrep_cluster_address = ‘gcomm://192.168.1.100,192.168.1.101,192.168.1.102’ wsrep_node_name = ‘node1’ wsrep_node_address = ‘192.168.1.100’ wsrep_provider = /greatsql/app/lib/libgalera_smm.so wsrep_sst_method = rsync EOF

# 初始化集群
# 在第一个节点上执行 greatsql_bootstrap –user=root –password=GreatSQL123!

# 启动其他节点
# 在其他节点上执行
systemctl start greatsql

# 查看集群状态
mysql -u root -pFGedu123456! -e “SHOW STATUS LIKE ‘wsrep%’;”

+——————————+————————————————–+
| Variable_name | Value |
+——————————+————————————————–+
| wsrep_local_state_uuid | 550e8400-e29b-41d4-a716-446655440000 |
| wsrep_protocol_version | 10 |
| wsrep_last_committed | 10 |
| wsrep_replicated | 5 |
| wsrep_replicated_bytes | 1024 |
| wsrep_received | 10 |
| wsrep_received_bytes | 2048 |
| wsrep_local_commits | 5 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_max | 1 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_send_queue_avg | 0.000000 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_max | 1 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_recv_queue_avg | 0.000000 |
| wsrep_local_cached_downto | 1 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_paused | 0.000000 |
| wsrep_flow_control_sent | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_cert_index_size | 0 |
| wsrep_causal_reads | 0 |
| wsrep_incoming_addresses | 192.168.1.100:3306,192.168.1.101:3306,192.168.1.102:3306 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 550e8400-e29b-41d4-a716-446655440000 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy |
| wsrep_provider_version | 4.16(rxxx) |
| wsrep_ready | ON |
+——————————+————————————————–+

4.3 故障切换实战

# 模拟主库故障
# 在主库上执行
systemctl stop greatsql

# 检查从库状态
mysql -u root -pFGedu123456! -e “SHOW SLAVE STATUS\G”

*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.1.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 156
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Connecting
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: 156
Relay_Log_Space: 524
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master ‘repl@192.168.1.100:3306’ – retry-time: 60 retries: 1
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 550e8400-e29b-41d4-a716-446655440000
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 260409 13:00:00
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 00000001-0000-0000-0000-000000000001:1-5, 00000002-0000-0000-0000-000000000002:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:

# 提升从库为主库
mysql -u root -pFGedu123456! -e “STOP SLAVE;” mysql -u root -pFGedu123456! -e “RESET SLAVE ALL;” mysql -u root -pFGedu123456! -e “SET GLOBAL read_only = OFF;”

Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)

# 配置其他从库指向新主库
mysql -u root -pFGedu123456! -e “STOP SLAVE;” mysql -u root -pFGedu123456! -e “CHANGE MASTER TO \ MASTER_HOST=’192.168.1.101′, \ MASTER_USER=’repl’, \ MASTER_PASSWORD=’Repl@123456′, \ MASTER_PORT=3306, \ MASTER_AUTO_POSITION=1;” mysql -u root -pFGedu123456! -e “START SLAVE;”

Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected, 2 warnings (0.01 sec)
Query OK, 0 rows affected (0.01 sec)

Part05-风哥经验总结与分享

5.1 常见复制问题与解决方案

问题 解决方案
复制延迟 检查网络连接,调整复制参数,使用并行复制
复制错误 检查错误日志,修复数据冲突,重新同步
主库故障 提升从库为主库,更新应用连接配置
集群分裂 检查网络连接,确保quorum,重启节点

5.2 集群管理最佳实践

  • 定期监控复制状态
  • 实施自动化故障切换
  • 定期备份数据
  • 使用GTID确保复制一致性
  • 配置合理的网络拓扑
  • 定期测试故障切换流程
  • 保持集群节点配置一致

更多学习教程公众号风哥教程itpux_com

5.3 性能优化建议

# 优化复制性能
cat >> /etc/my.cnf << EOF
# 复制性能优化 slave-parallel-type = LOGICAL_CLOCK slave-parallel-workers = 4 slave_preserve_commit_order = 1 slave_transaction_retries = 10 EOF
# 重启服务
systemctl restart greatsql

复制监控脚本

# 创建复制监控脚本
cat > /greatsql/scripts/replication_monitor.sh << 'EOF'
#!/bin/bash # replication_monitor.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
echo “=== GreatSQL Replication Monitor ===” echo “Date: $(date)” echo “”
# 检查复制状态 SLAVE_STATUS=$(mysql -u root -pFGedu123456! -e “SHOW SLAVE STATUS\G” -s) if
echo “$SLAVE_STATUS” | grep -q “Slave_IO_Running: Yes” && echo “$SLAVE_STATUS” | grep -q “Slave_SQL_Running: Yes”; then echo “Replication is running normally”
# 检查复制延迟 SECONDS_BEHIND=$(echo “$SLAVE_STATUS” | grep “Seconds_Behind_Master” | awk ‘{print $2}’) if [ “$SECONDS_BEHIND” -gt 300 ]; then
echo “Warning: Replication delay is high ($SECONDS_BEHIND seconds)” else echo “Replication delay: $SECONDS_BEHIND seconds” fi else echo “Error: Replication is not running” echo “Slave IO running: $(echo “$SLAVE_STATUS” | grep “Slave_IO_Running” | awk ‘{print $2}’)” echo “Slave SQL running: $(echo “$SLAVE_STATUS” | grep “Slave_SQL_Running” | awk ‘{print $2}’)” echo “Last error: $(echo “$SLAVE_STATUS” | grep “Last_Error” | cut -d: -f2-)” fi echo “” echo “Replication monitoring completed!” EOF
# 设置脚本权限
chmod +x /greatsql/scripts/replication_monitor.sh

# 执行复制监控脚本 /greatsql/scripts/replication_monitor.sh

=== GreatSQL Replication Monitor ===
Date: Wed Apr 9 20:00:00 CST 2026
Replication is running normally
Replication delay: 0 seconds
Replication monitoring completed!

集群监控脚本

# 创建集群监控脚本
cat > /greatsql/scripts/cluster_monitor.sh << 'EOF'
#!/bin/bash # cluster_monitor.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
echo “=== GreatSQL Cluster Monitor ===” echo “Date: $(date)” echo “”
# 检查集群状态 CLUSTER_STATUS=$(mysql -u root -pFGedu123456! -e “SHOW STATUS LIKE ‘wsrep%’;” -s) CLUSTER_SIZE=$(echo “$CLUSTER_STATUS” | grep “wsrep_cluster_size” | awk ‘{print $2}’) CLUSTER_STATUS=$(echo “$CLUSTER_STATUS” | grep “wsrep_cluster_status” | awk ‘{print $2}’) LOCAL_STATE=$(echo “$CLUSTER_STATUS” | grep “wsrep_local_state_comment” | awk ‘{print $2}’) if [ “$CLUSTER_STATUS” = “Primary” ] && [ “$LOCAL_STATE” = “Synced” ]; then
echo “Cluster is healthy” echo “Cluster size: $CLUSTER_SIZE” echo “Local state: $LOCAL_STATE” else echo “Error: Cluster is not healthy” echo “Cluster status: $CLUSTER_STATUS” echo “Local state: $LOCAL_STATE” fi echo “” echo “Cluster monitoring completed!” EOF
# 设置脚本权限
chmod +x /greatsql/scripts/cluster_monitor.sh

# 执行集群监控脚本 /greatsql/scripts/cluster_monitor.sh

=== GreatSQL Cluster Monitor ===
Date: Wed Apr 9 20:00:00 CST 2026
Cluster is healthy
Cluster size: 3
Local state: Synced
Cluster monitoring completed!

from greatsql视频:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息