opengauss教程FG059-openGauss主备流复制部署生产实战
本文档详细介绍openGauss数据库主备流复制的部署方法,包括主库配置、备库配置、复制关系建立等操作,风哥教程参考openGauss官方文档主备复制指南、高可用架构指南等内容,适合DBA人员进行主备环境部署时参考。
Part01-基础概念与理论知识
1.1 openGauss流复制基本概念
- 实时同步:WAL日志实时传输,备库数据延迟小
- 多备支持:一个主库可以连接多个备库
- 级联复制:备库可以作为其他备库的主库
- 热备查询:备库支持只读查询,实现读写分离
1.2 openGauss流复制工作原理
流复制的工作流程:
1. WAL生成
– 主库执行事务,生成WAL日志
– WAL日志写入WAL缓冲区
– WAL缓冲区刷盘到WAL文件
2. WAL传输
– 主库WAL发送进程(wal sender)读取WAL
– 通过网络传输到备库
– 备库WAL接收进程(wal receiver)接收WAL
3. WAL应用
– 备库将WAL写入本地存储
– 备库启动恢复进程应用WAL
– 备库数据与主库保持一致
4. 反馈机制
– 备库定期向主库发送LSN位置
– 主库根据反馈确认提交
– 同步模式下等待备库确认
架构组件:
– WalSender:主库WAL发送进程
– WalReceiver:备库WAL接收进程
– Startup:备库恢复进程
– Background Writer:后台写入进程
1.3 openGauss复制模式详解
openGauss支持三种复制模式:
- 异步复制(Async):主库不等待备库确认,性能最好,但可能丢失数据
- 同步复制(Sync):主库等待备库确认后才提交,数据最安全,但性能受影响
- 半同步复制(Semi-Sync):主库等待至少一个备库确认,平衡性能和安全
Part02-生产环境规划与建议
2.1 openGauss主备环境规划
主备环境规划要点:
1. 服务器规划
– 主库:fgedu-primary 192.168.1.10 16核64GB内存 1TB SSD
– 备库:fgedu-standby 192.168.1.11 16核64GB内存 1TB SSD
2. 操作系统规划
– OS:Oracle Linux 9.3 / RHEL 9.3
– 内核参数:按照openGauss官方推荐配置
– 防火墙:开放5432端口(数据库)和22端口(SSH)
3. 数据库规划
– 版本:openGauss 5.0.0
– 数据目录:/opengauss/fgdata
– 安装目录:/opengauss/app
– 数据库名:fgedudb
4. 用户规划
– 操作系统用户:opengauss
– 数据库管理员:fgedu
– 复制用户:repl
5. 复制规划
– 复制模式:异步复制(生产环境可根据需求调整为同步)
– 复制槽:使用物理复制槽
– 归档:开启归档模式
2.2 openGauss网络规划配置
网络规划配置:
1. IP地址规划
– 主库管理IP:192.168.1.10/24
– 备库管理IP:192.168.1.11/24
– 网关:192.168.1.1
– DNS:8.8.8.8
2. 主机名配置
– 主库主机名:fgedu-primary.fgedu.net.cn
– 备库主机名:fgedu-standby.fgedu.net.cn
3. hosts配置
# /etc/hosts
192.168.1.10 fgedu-primary fgedu-primary.fgedu.net.cn
192.168.1.11 fgedu-standby fgedu-standby.fgedu.net.cn
4. 防火墙配置
# 主库防火墙
firewall-cmd –permanent –add-port=5432/tcp
firewall-cmd –permanent –add-port=22/tcp
firewall-cmd –reload
# 备库防火墙
firewall-cmd –permanent –add-port=5432/tcp
firewall-cmd –permanent –add-port=22/tcp
firewall-cmd –reload
5. 网络测试
# 测试主备连通性
ping -c 4 192.168.1.11
# 测试端口连通性风哥提示:
telnet 192.168.1.11 5432
2.3 openGauss资源配置规划
资源配置规划:
1. 内存配置
– 共享内存:32GB(shared_buffers = 32GB)
– 进程内存:16GB(max_process_memory = 48GB)
– 系统保留:16GB
2. 存储配置
– 数据盘:/dev/sdb 1TB SSD 挂载到/opengauss/fgdata
– 日志盘:/dev/sdc 200GB SSD 挂载到/opengauss/fglog
– 归档盘:/dev/sdd 500GB SATA 挂载到/opengauss/archive
3. CPU配置
– 逻辑核数:16核
– 并行度:max_parallel_workers = 8
– 最大连接数:max_connections = 1000
4. 磁盘挂载
# 数据盘
mkfs.xfs /dev/sdb
mkdir -p /opengauss/fgdata
mount /dev/sdb /opengauss/fgdata
echo ‘/dev/sdb /opengauss/fgdata xfs defaults,noatime 0 0’ >> /etc/fstab
# 日志盘学习交流加群风哥微信: itpux-com
mkfs.xfs /dev/sdc
mkdir -p /opengauss/fglog
mount /dev/sdc /opengauss/fglog
echo ‘/dev/sdc /opengauss/fglog xfs defaults,noatime 0 0’ >> /etc/fstab
Part03-生产环境项目实施方案
3.1 openGauss主库配置部署
3.1.1 主库基础配置
# 步骤1:创建操作系统用户
$ useradd -m -d /home/opengauss -s /bin/bash opengauss
$ passwd opengauss
# 步骤2:配置内核参数
# /etc/sysctl.conf
cat >> /etc/sysctl.conf << EOF
kernel.shmmax = 68719476736
kernel.shmmni = 4096
kernel.shmall = 16777216
kernel.sem = 250 32000 100 128
fs.file-max = 6815744
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 4194304
EOF
$ sysctl -p
# 步骤3:配置资源限制学习交流加群风哥QQ113257174
# /etc/security/limits.conf
cat >> /etc/security/limits.conf << EOF
opengauss soft nproc 65536
opengauss hard nproc 65536
opengauss soft nofile 65536
opengauss hard nofile 65536
opengauss soft memlock unlimited
opengauss hard memlock unlimited
EOF
# 步骤4:创建目录
$ mkdir -p /opengauss/app
$ mkdir -p /opengauss/fgdata
$ mkdir -p /opengauss/fglog
$ mkdir -p /opengauss/archive
$ chown -R opengauss:opengauss /opengauss
# 步骤5:安装openGauss
$ su - opengauss
$ cd /opengauss/app
$ tar -zxvf openGauss-5.0.0-CentOS-64bit.tar.gz
# 步骤6:初始化数据库
$ gs_initdb -D /opengauss/fgdata --nodename=fgedudb -w fgedu_password
The files belonging to this database system will be owned by user "opengauss".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
creating directory /opengauss/fgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:更多视频教程www.fgedu.net.cn
gs_ctl -D /opengauss/fgdata -l logfile start
3.1.2 主库复制配置
# 步骤1:配置postgresql.conf
$ cat >> /opengauss/fgdata/postgresql.conf << EOF
# 流复制配置
wal_level = replica
max_wal_senders = 10
wal_keep_size = 2GB
max_replication_slots = 10
hot_standby = on
# 归档配置
archive_mode = on
archive_command = 'cp %p /opengauss/archive/%f'
archive_timeout = 1800
# 连接配置
listen_addresses = '*'
port = 5432
max_connections = 1000
# 内存配置
shared_buffers = 32GB
max_process_memory = 48GB
work_mem = 64MB
maintenance_work_mem = 2GB
# 日志配置
logging_collector = on
log_directory = '/opengauss/fglog'
log_filename = 'postgresql-%Y-%m-%d.log'
log_rotation_age = 1d
log_rotation_size = 100MB
EOF更多学习教程公众号风哥教程itpux_com
# 步骤2:配置pg_hba.conf
$ cat >> /opengauss/fgdata/pg_hba.conf << EOF
# 复制连接配置
host replication repl 192.168.1.11/32 md5
host all all 192.168.1.0/24 md5
EOF
# 步骤3:创建复制用户
$ gs_ctl start -D /opengauss/fgdata
waiting for server to start.... done
server started
$ gsql -d postgres -c "CREATE USER repl WITH REPLICATION PASSWORD 'repl_pass';
”
CREATE ROLE
# 步骤4:创建复制槽
$ gsql -d postgres -c “SELECT * FROM pg_create_physical_replication_slot(‘fgedu_standby_slot’);
”
slot_name | lsn
——————–+—–
fgedu_standby_slot | 0/0
(1 row)
# 步骤5:重启主库使配置生效
$ gs_ctl restart -D /opengauss/fgdata
waiting for server to shut down…. donefrom DB视频:www.itpux.com
server stopped
waiting for server to start…. done
server started
# 步骤6:验证配置
$ gsql -d postgres -c “SHOW wal_level;
”
wal_level
———–
replica
(1 row)
$ gsql -d postgres -c “SELECT * FROM pg_replication_slots;
”
slot_name | plugin | slot_type | datoid | database | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn
——————–+——–+———–+——–+———-+——–+————+——+————–+————-+———————
fgedu_standby_slot | | physical | | | f | | | | |
(1 row)
3.2 openGauss备库配置部署
3.2.1 备库基础配置
# 步骤1-5:与主库相同(创建用户、配置内核参数、创建目录、安装软件)
# 此处省略…
# 步骤6:使用gs_probackup从主库备份
$ gs_probackup backup -B /opengauss/backup –instance=fgedudb -b FULL –remote-host=192.168.1.10 –remote-user=opengauss
INFO: Backup begin.
INFO: Remote host: 192.168.1.10
INFO: Starting backup…
INFO: Creating backup directory
INFO: Copying data files
INFO: Data files are copied
INFO: Backup completed successfully
# 步骤7:恢复备份到备库
$ gs_probackup restore -B /opengauss/backup –instance=fgedudb -D /opengauss/fgdata
INFO: Restore begin.
INFO: Checking backup
INFO: Backup is valid
INFO: Restoring backup
INFO: Copying data files
INFO: Data files are copied
INFO: Restore completed successfully
# 步骤8:修改权限
$ chown -R opengauss:opengauss /opengauss/fgdata
$ chmod 700 /opengauss/fgdata
3.2.2 备库复制配置
# 步骤1:配置postgresql.conf
$ cat >> /opengauss/fgdata/postgresql.conf << EOF
# 备库配置
hot_standby = on
max_standby_archive_delay = 30s
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 5s
hot_standby_feedback = on
wal_receiver_timeout = 60s
wal_retrieve_retry_interval = 5s
EOF
# 步骤2:配置主库连接信息
$ cat > /opengauss/fgdata/standby.signal << EOF
primary_conninfo = 'host=192.168.1.10 port=5432 user=repl password=repl_pass application_name=fgedu_standby'
primary_slot_name = 'fgedu_standby_slot'
recovery_target_timeline = 'latest'
EOF
# 步骤3:创建standby.signal文件
$ touch /opengauss/fgdata/standby.signal
# 步骤4:配置pg_hba.conf
$ cat >> /opengauss/fgdata/pg_hba.conf << EOF
# 备库访问配置
host all all 192.168.1.0/24 md5
EOF
# 步骤5:启动备库
$ gs_ctl start -D /opengauss/fgdata
waiting for server to start.... done
server started
3.3 openGauss复制关系建立
3.3.1 验证复制状态
# 步骤1:在主库查看复制状态
$ gsql -h 192.168.1.10 -d postgres -c “SELECT * FROM pg_stat_replication;
”
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state
—–+———-+———+——————+————–+—————–+————-+——————–+————–+———+———-+———–+———–+————+———–+———–+————+—————+————
12345| 16384 | repl | fgedu_standby | 192.168.1.11 | | 54322 | 2026-04-09 10:00:00| |streaming| 0/3000100| 0/3000100 | 0/3000100 | 0/3000100 | | | | 0 | async
(1 row)
# 步骤2:在备库查看接收状态
$ gsql -h 192.168.1.11 -d postgres -c “SELECT * FROM pg_stat_wal_receiver;
”
pid | status | receive_start_lsn | receive_start_tli | received_lsn | received_tli | latest_end_lsn | latest_end_time | slot_name
—–+——–+——————-+——————-+————–+————–+—————-+——————–+——————-
12346|streaming| 0/3000000 | 1 | 0/3000100 | 1 | 0/3000100 | 2026-04-09 10:00:00| fgedu_standby_slot
(1 row)
# 步骤3:检查复制延迟
$ gsql -h 192.168.1.11 -d postgres -c “SELECT EXTRACT(EPOCH FROM (now() – pg_last_xact_replay_timestamp())) AS delay_seconds;
”
delay_seconds
—————
0.05
(1 row)
# 步骤4:测试数据同步
# 在主库插入数据
$ gsql -h 192.168.1.10 -d fgedudb -c “INSERT INTO fgedu.fgedu_test VALUES (1, ‘test_data’, now());
”
INSERT 0 1
# 在备库查询数据
$ gsql -h 192.168.1.11 -d fgedudb -c “SELECT * FROM fgedu.fgedu_test;
”
id | data | create_time
—-+———–+———————
1 | test_data | 2026-04-09 10:05:00
(1 row)
# 步骤5:验证备库只读
$ gsql -h 192.168.1.11 -d fgedudb -c “INSERT INTO fgedu.fgedu_test VALUES (2, ‘test_data2’, now());
”
ERROR: cannot execute INSERT in a read-only transaction
Part04-生产案例与实战讲解
4.1 openGauss一主一备部署案例
4.1.1 部署环境
某企业部署一主一备环境,用于核心业务系统。
4.1.2 部署过程
# 环境信息
主库:fgedu-primary 192.168.1.10
备库:fgedu-standby 192.168.1.11
数据库:fgedudb
复制模式:异步复制
# 部署步骤
# 1. 主库配置(已在前文详述)
# 2. 备库配置(已在前文详述)
# 3. 验证复制
# 验证脚本
#!/bin/bash
# check_replication.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: `http://www.fgedu.net.cn`
MASTER_IP=”192.168.1.10″
STANDBY_IP=”192.168.1.11″
echo “=== 主备复制状态检查 ===”
echo “检查时间: $(date)”
echo “”
# 检查主库复制状态
echo “— 主库复制状态 —”
gsql -h $MASTER_IP -d postgres -c “SELECT client_addr, state, sent_lsn, replay_lsn, sync_state FROM pg_stat_replication;
”
# 检查备库接收状态
echo “”
echo “— 备库接收状态 —”
gsql -h $STANDBY_IP -d postgres -c “SELECT status, received_lsn, latest_end_time FROM pg_stat_wal_receiver;
”
# 检查复制延迟
echo “”
echo “— 复制延迟 —”
gsql -h $STANDBY_IP -d postgres -c “SELECT EXTRACT(EPOCH FROM (now() – pg_last_xact_replay_timestamp())) AS delay_seconds;
”
# 测试数据同步
echo “”
echo “— 数据同步测试 —”
gsql -h $MASTER_IP -d fgedudb -c “CREATE TABLE IF NOT EXISTS fgedu.replication_test (id int, test_time timestamp);
”
gsql -h $MASTER_IP -d fgedudb -c “INSERT INTO fgedu.replication_test VALUES (1, now());
”
sleep 1
gsql -h $STANDBY_IP -d fgedudb -c “SELECT * FROM fgedu.replication_test;
”
echo “”
echo “=== 检查完成 ===”
# 执行结果
=== 主备复制状态检查 ===
检查时间: Thu Apr 9 10:30:00 CST 2026
— 主库复制状态 —
client_addr | state | sent_lsn | replay_lsn | sync_state
————–+———–+———–+————+————
192.168.1.11 | streaming | 0/3000200 | 0/3000200 | async
(1 row)
— 备库接收状态 —
status | received_lsn | latest_end_time
———–+————–+———————
streaming | 0/3000200 | 2026-04-09 10:30:00
(1 row)
— 复制延迟 —
delay_seconds
—————
0.02
(1 row)
— 数据同步测试 —
id | test_time
—-+———————
1 | 2026-04-09 10:30:00
(1 row)
=== 检查完成 ===
4.2 openGauss一主多备部署案例
4.2.1 部署环境
某电商平台部署一主三备环境,实现高可用和读写分离。
4.2.2 部署过程
# 环境信息
主库:fgedu-primary 192.168.1.10
备库1:fgedu-standby1 192.168.1.11 (同步复制,优先切换)
备库2:fgedu-standby2 192.168.1.12 (异步复制,读分担)
备库3:fgedu-standby3 192.168.1.13 (异步复制,读分担)
# 主库额外配置
# postgresql.conf
synchronous_commit = remote_write
synchronous_standby_names = ‘fgedu_standby1’
max_wal_senders = 10
max_replication_slots = 10
# 创建多个复制槽
$ gsql -h 192.168.1.10 -d postgres -c “SELECT * FROM pg_create_physical_replication_slot(‘fgedu_standby1_slot’);
”
$ gsql -h 192.168.1.10 -d postgres -c “SELECT * FROM pg_create_physical_replication_slot(‘fgedu_standby2_slot’);
”
$ gsql -h 192.168.1.10 -d postgres -c “SELECT * FROM pg_create_physical_replication_slot(‘fgedu_standby3_slot’);
”
# 备库1配置(同步复制)
primary_conninfo = ‘host=192.168.1.10 port=5432 user=repl password=repl_pass application_name=fgedu_standby1’
primary_slot_name = ‘fgedu_standby1_slot’
# 备库2配置(异步复制)
primary_conninfo = ‘host=192.168.1.10 port=5432 user=repl password=repl_pass application_name=fgedu_standby2’
primary_slot_name = ‘fgedu_standby2_slot’
# 备库3配置(异步复制)
primary_conninfo = ‘host=192.168.1.10 port=5432 user=repl password=repl_pass application_name=fgedu_standby3’
primary_slot_name = ‘fgedu_standby3_slot’
# 验证多备复制
$ gsql -h 192.168.1.10 -d postgres -c “SELECT client_addr, application_name, state, sync_state FROM pg_stat_replication;
”
client_addr | application_name | state | sync_state
————–+——————+———–+————
192.168.1.11 | fgedu_standby1 | streaming | sync
192.168.1.12 | fgedu_standby2 | streaming | async
192.168.1.13 | fgedu_standby3 | streaming | async
(3 rows)
# 读写分离配置
# 使用中间件或应用层实现读写分离
# 写操作 -> 主库 192.168.1.10
# 读操作 -> 备库2 192.168.1.12 或 备库3 192.168.1.13
4.3 openGauss SSL加密复制部署
4.3.1 部署环境
某金融系统要求复制通道加密,部署SSL加密复制。
4.3.2 部署过程
# 步骤1:生成SSL证书
# 在主库生成CA证书
$ openssl req -new -x509 -days 365 -nodes -text -out /opengauss/fgdata/server.crt \
-keyout /opengauss/fgdata/server.key -subj “/CN=fgedu-primary.fgedu.net.cn”
# 生成服务器证书
$ openssl req -new -nodes -text -out /opengauss/fgdata/server.csr \
-keyout /opengauss/fgdata/server.key -subj “/CN=fgedu-primary.fgedu.net.cn”
$ openssl x509 -req -in /opengauss/fgdata/server.csr -text -days 365 \
-CA /opengauss/fgdata/server.crt -CAkey /opengauss/fgdata/server.key \
-CAcreateserial -out /opengauss/fgdata/server.crt
# 步骤2:配置主库SSL
# postgresql.conf
ssl = on
ssl_cert_file = ‘/opengauss/fgdata/server.crt’
ssl_key_file = ‘/opengauss/fgdata/server.key’
ssl_ca_file = ‘/opengauss/fgdata/server.crt’
# 步骤3:配置备库SSL
# 将主库证书复制到备库
$ scp /opengauss/fgdata/server.crt opengauss@192.168.1.11:/opengauss/fgdata/
$ scp /opengauss/fgdata/server.key opengauss@192.168.1.11:/opengauss/fgdata/
# 备库postgresql.conf
ssl = on
ssl_cert_file = ‘/opengauss/fgdata/server.crt’
ssl_key_file = ‘/opengauss/fgdata/server.key’
ssl_ca_file = ‘/opengauss/fgdata/server.crt’
# 步骤4:配置SSL复制连接
# 备库standby.signal
primary_conninfo = ‘host=192.168.1.10 port=5432 user=repl password=repl_pass sslmode=require application_name=fgedu_standby’
# 步骤5:重启主备库
$ gs_ctl restart -D /opengauss/fgdata
# 步骤6:验证SSL连接
$ gsql -h 192.168.1.10 -d postgres -c “SELECT * FROM pg_stat_ssl;
”
pid | ssl | version | cipher | bits | compression | client_dn | client_serial | issuer_dn
—–+—–+———+——–+——+————-+———–+—————+———–
12345| t | TLSv1.2 | ECDHE-RSA-AES256-GCM-SHA384 | 256 | f | | |
(1 row)
Part05-风哥经验总结与分享
5.1 openGauss流复制最佳实践
流复制部署最佳实践总结:
- 硬件一致:主备服务器硬件配置保持一致
- 网络稳定:使用专用网络或高质量网络连接
- 监控完善:建立复制延迟监控和告警
- 定期演练:定期进行故障切换演练
- 文档记录:详细记录部署配置和操作步骤
5.2 openGauss部署检查清单
# 部署前检查
□ 服务器硬件配置确认
□ 操作系统参数配置
□ 网络连通性测试
□ 防火墙端口开放
□ 磁盘挂载和权限
# 部署中检查
□ 数据库安装成功
□ 主库配置正确
□ 复制用户创建
□ 复制槽创建
□ 备库备份恢复
□ 备库配置正确
# 部署后检查
□ 复制状态正常
□ 复制延迟正常
□ 数据同步测试
□ 故障切换测试
□ 监控告警配置
5.3 openGauss常见问题处理
流复制常见问题及解决方案:
- 复制延迟高:检查网络带宽、备库性能、WAL生成速度
- 复制中断:检查网络连接、主备状态、日志错误
- 认证失败:检查pg_hba.conf配置、用户名密码、网络访问
- 复制槽满:检查WAL保留设置、磁盘空间、复制槽状态
- 备库无法启动:检查standby.signal配置、数据目录权限
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
