openEuler下opengauss6.0.5主从部署(采用原始方式)
环境:
OS:openEuler 22
opengauss:6.0.5
主库:192.168.1.107
从库:192.168.102
说明:
主库已经部署完成,参考如下连接
https://www.cnblogs.com/hxlasky/p/21007109
############################主库上的操作############################
1.修改初始化参数
pg_hba.conf文件
vi /opt/opengauss/data/pg_hba.conf
host all all 192.168.1.101/32 trust
host all all 192.168.1.102/32 trust
host all all 0.0.0.0/0 md5 ##该句放最后面
postgresql.conf文件
vi /opt/opengauss/data/postgresql.conf
修改如下参数
most_available_sync=on ##该参数需要开启 默认是off,那样的话从库停掉了 主库无法写入数据
主从配置添加项目
主库添加如下项:
vi /opt/opengauss/data/postgresql.conf
在最后添加如下项目
replconninfo1 = 'localhost=192.168.1.107 localport=12211 localheartbeatport=12214 remotehost=192.168.1.102 remoteport=12212 remoteheartbeatport=12215'
localhost为主库IP,remotehost为备库IP
我这里用于同步的通信端口,方便管理,主从都保持一致(官方给的模版是不一致的)
localhost=192.168.1.107 主库的ip
localport=12211 主库用于主从同步通信端口
localheartbeatport=12214 主库用于主从同步心跳端口
remotehost=192.168.1.102 备库的ip
remoteport=12211 备库用于主从同步通信端口
remoteheartbeatport=12214 备库用于主从同步心跳端口
从库到时也会配置replconninfo1 这个参数的,端口都保持一致,只调换ip即可:
replconninfo1 = 'localhost=192.168.1.102 localport=12211 localheartbeatport=12214 remotehost=192.168.1.107 remoteport=12212 remoteheartbeatport=12215'
2.重启数据库
su - omm
gs_ctl stop -D /opt/opengauss/data
gs_ctl start -D /opt/opengauss/data
3.查看状态
[omm@localhost ~]$ gs_ctl query -D /opt/opengauss/data
[2026-07-01 17:17:03.837][4125][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
No information
这里看到Primary 是主库了,主库的端口监听端口已经启动
[omm@localhost ~]$ ss -nlp|grep 12211
tcp LISTEN 0 1200 192.168.1.101:12211 0.0.0.0:* users:(("gaussdb",pid=4062,fd=12))
[omm@localhost ~]$ ss -nlp|grep 12214
tcp LISTEN 0 512 192.168.1.101:12214 0.0.0.0:* users:(("gaussdb",pid=4062,fd=106))
######################备库安装opengauss#################################
1.下载安装介质
下载地址
https://opengauss.org/zh/download.html
我这里下载的是
openGauss-All-6.0.5-openEuler22.03-x86_64.tar.gz
2.将下载安装包上传到服务器 /soft/opengauss 目录下
[root@localhost opengauss]# ls -1
openGauss-All-6.0.5-openEuler22.03-x86_64.tar.gz
[root@localhost opengauss]# yum install tar
[root@localhost opengauss]# tar -xvf openGauss-All-6.0.5-openEuler22.03-x86_64.tar.gz
[root@localhost opengauss]# ls -1
openGauss-All-6.0.5-openEuler22.03-x86_64.tar.gz
openGauss-CM-6.0.5-openEuler22.03-x86_64.sha256
openGauss-CM-6.0.5-openEuler22.03-x86_64.tar.gz
openGauss-OM-6.0.5-openEuler22.03-x86_64.sha256
openGauss-OM-6.0.5-openEuler22.03-x86_64.tar.gz
openGauss-Server-6.0.5-openEuler22.03-x86_64.sha256
openGauss-Server-6.0.5-openEuler22.03-x86_64.tar.bz2
upgrade_sql.sha256
upgrade_sql.tar.gz
我们采用极简版安装,只需要如下安装包即可
openGauss-Server-6.0.5-openEuler22.03-x86_64.tar.bz2
3.安装依赖包
yum install libaio-devel
yum install flex
yum install bison
yum install ncurses-devel
yum install glibc-devel
yum install patch
yum install readline-devel
4.关闭防火墙
systemctl stop firewalld
禁止开机自启
systemctl disable firewalld
5.创建相关目录,用户,组
root账号下操作
[root@host134 ~]#groupadd dbgrp -g 2000
[root@host134 ~]#useradd omm -g 2000 -u 2000
[root@host134 ~]#echo "oracle" | passwd --stdin omm
[root@host134 ~]#mkdir -p /opt/opengauss/software ##软件安装目录
[root@host134 ~]#mkdir -p /opt/opengauss/data ##数据目录
[root@host134 ~]#mkdir -p /opt/opengauss/log ##这个目录是opengauss软件的日志目录,数据库的日志目录还是存在../data/pg_log目录
[root@host134 ~]#chown -R omm:dbgrp /opt/opengauss
6.解压二进制文件
[omm@host134 opengauss]# cd /soft/opengauss
[omm@host134 opengauss]# tar -jxf openGauss-Server-6.0.5-openEuler22.03-x86_64.tar.bz2 -C /opt/opengauss/software/
[root@host134 ~]#chown -R omm:dbgrp /opt/opengauss
7.设置用户omm环境变量
su - omm
cd ~
vi .bashrc
添加如下内容
export GAUSSHOME=/opt/opengauss/software
export GAUSSLOG=/opt/opengauss/log
export PATH=$GAUSSHOME/bin:$PATH
export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH
整个文件内容如下:
[omm@localhost ~]$ more .bashrc
# Source default setting
[ -f /etc/bashrc ] && . /etc/bashrc
# User environment PATH
export GAUSSHOME=/opt/opengauss/software
export GAUSSLOG=/opt/opengauss/log
export PATH=$GAUSSHOME/bin:$PATH
export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH
export PATH
退出后重新登录
8.初始化数据库
su - omm
[omm@localhost ~]$ cd /opt/opengauss/software/bin
[omm@localhost ~]$./gs_initdb --pgdata=/opt/opengauss/data --nodename=slnngkbak --encoding=UTF-8 --locale=en_US.UTF-8
参数说明:
--nodename=NODENAME name of single node initialized
主库取的名字叫slnngk 这里为了区分 取名为slnngkbak
9.修改初始化参数
pg_hba.conf文件
vi /opt/opengauss/data/pg_hba.conf
host all all 192.168.1.107/32 trust
host all all 192.168.1.102/32 trust
host all all 0.0.0.0/0 md5
postgresql.conf文件
vi /opt/opengauss/data/postgresql.conf
port=5432
max_connections = 4000
listen_addresses = '*'
password_encryption_type = 0
most_available_sync=on ##该参数需要开启 默认是off,那样的话从库停掉了 主库无法写入数据
10.添加从库参数
vi /opt/opengauss/data/postgresql.conf
找到replconninfo1的位置,在后面添加
replconninfo1 = 'localhost=192.168.1.102 localport=12211 localheartbeatport=12214 remotehost=192.168.1.107 remoteport=12211 remoteheartbeatport=12214'
这里的localhost与remotehost 跟主库的对调,主从同步的端口都使用12211和12214
10.构建从库
gs_ctl stop -D /opt/opengauss/data ##之前若启动的话需要停掉
gs_ctl build -D /opt/opengauss/data -b full -M standby ##等同于pg中pg_basebackup的操作
发现重新构建会把连接数参数修改了主库的一样了
max_connections = 200(从库之前是设置了4000的)
从库启用的端口(与主库的一致)
[omm@localhost data]$ ss -nlp|grep 5432
u_str LISTEN 0 4096 /tmp/.s.PGSQL.5432 24454 * 0 users:(("gaussdb",pid=2293,fd=15))
tcp LISTEN 0 4096 0.0.0.0:5432 0.0.0.0:* users:(("gaussdb",pid=2293,fd=10))
tcp LISTEN 0 4096 [::]:5432 [::]:* users:(("gaussdb",pid=2293,fd=11))
[omm@localhost data]$ ss -nlp|grep 12211
tcp LISTEN 0 4096 192.168.1.102:12211 0.0.0.0:* users:(("gaussdb",pid=2293,fd=14))
[omm@localhost data]$ ss -nlp|grep 12214
tcp LISTEN 0 512 192.168.1.102:12214 0.0.0.0:* users:(("gaussdb",pid=2293,fd=64))
11.尝试重启从库
gs_ctl stop -D /opt/opengauss/data
gs_ctl start -D /opt/opengauss/data -M standby
必须带上-M standby启动,否则会启动为主库模式
12.查看从库状态
gs_ctl query -D /opt/opengauss/data
[2026-07-02 10:23:30.462][2426][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
receiver_pid : 2425
local_role : Standby
peer_role : Primary
peer_state : Normal
state : Normal
sender_sent_location : 0/4000388
sender_write_location : 0/4000388
sender_flush_location : 0/4000388
sender_replay_location : 0/4000388
receiver_received_location : 0/4000388
receiver_write_location : 0/4000388
receiver_flush_location : 0/4000388
receiver_replay_location : 0/4000388
sync_percent : 100%
channel : 192.168.1.102:52754<--192.168.1.107:12211
13.数据验证
主库写入数据
gsql -U hxl -d db_test -h 127.0.0.1 -p 5432
密码是:Gauss@123456
insert into tb_test values(6,'name6');
insert into tb_test values(7,'name7');
insert into tb_test values(8,'name8');
insert into tb_test values(9,'name9');
insert into tb_test values(10,'name10');
从库查看数据
gsql -U hxl -d db_test -h 127.0.0.1 -p 5432
密码是:Gauss@123456
[omm@localhost ~]$ gsql -U hxl -d db_test -h 127.0.0.1 -p 5432
Password for user hxl:
gsql ((openGauss 6.0.5 build 5ba692ab) compiled at 2026-05-07 12:36:04 commit 0 last mr 9228 )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_test=> \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | tb_test | table | hxl | {orientation=row,compression=no}
(1 row)
db_test=> select * from tb_test;
id | name
----+--------
1 | name1
2 | name2
3 | name3
4 | name4
5 | name5
6 | name6
7 | name7
8 | name8
9 | name9
10 | name10
(10 rows)
发现已经同步
###################################主从切换验证####################################
A.主从切换(switchover)--从库上操作
登陆从库进行切换,我这里登陆到从库192.168.1.102 上操作,将该从库提升为主库
su - omm
gs_ctl switchover -D /opt/opengauss/data
原主库(192.168.1.107)
gs_ctl query -D /opt/opengauss/data
[2026-07-02 11:12:51.551][2790][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
receiver_pid : 2787
local_role : Standby
peer_role : Primary
peer_state : Normal
state : Normal
sender_sent_location : 0/6000788
sender_write_location : 0/6000788
sender_flush_location : 0/6000788
sender_replay_location : 0/6000788
receiver_received_location : 0/6000788
receiver_write_location : 0/6000788
receiver_flush_location : 0/6000788
receiver_replay_location : 0/6000788
sync_percent : 100%
channel : 192.168.1.107:53706<--192.168.1.102:12211
原备库(192.168.1.102,即当前的主库)
[omm@localhost opengauss]$ gs_ctl query -D /opt/opengauss/data
[2026-07-02 11:13:46.023][3182][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
sender_pid : 3172
local_role : Primary
peer_role : Standby
peer_state : Normal
state : Streaming
sender_sent_location : 0/60008A8
sender_write_location : 0/60008A8
sender_flush_location : 0/60008A8
sender_replay_location : 0/60008A8
receiver_received_location : 0/60008A8
receiver_write_location : 0/60008A8
receiver_flush_location : 0/60008A8
receiver_replay_location : 0/60008A8
sync_percent : 100%
sync_state : Sync
sync_priority : 1
sync_most_available : On
channel : 192.168.1.102:12211-->192.168.1.107:53706
Receiver info:
No information
B.主从切换(failover)
通过上面的switchover 192.168.1.102目前是主库
1.关闭主库(192.168.1.102),可以模拟主库down机
gs_ctl stop -D /opt/opengauss/data
这个时候查看从库的状态
[omm@localhost ~]$ gs_ctl query -D /opt/opengauss/data
[2026-07-02 11:23:37.117][3044][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Need repair
detail_information : Disconnected
Senders info:
No information
Receiver info:
No information
2.我们将其中的从库192.168.1.107 failover为主库
gs_ctl failover -D /opt/opengauss/data
查看状态
[omm@localhost ~]$ gs_ctl query -D /opt/opengauss/data
[2026-07-02 11:27:02.890][8655][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
No information
主库(192.168.1.107)模拟写入数据
gsql -U hxl -d db_test -h 127.0.0.1 -p 5432
密码是:Gauss@123456
insert into tb_test values(11,'name11');
insert into tb_test values(12,'name12');
insert into tb_test values(13,'name13');
insert into tb_test values(14,'name14');
insert into tb_test values(15,'name15');
3.这个时候把原来的主库以从库的角色启动
192.168.1.102
gs_ctl start -D /opt/opengauss/data -M standby
查看数据库状态
gs_ctl query -D /opt/opengauss/data
[2026-07-02 11:31:40.053][3322][][gs_ctl]: gs_ctl query ,datadir is /opt/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
receiver_pid : 3321
local_role : Standby
peer_role : Primary
peer_state : Normal
state : Normal
sender_sent_location : 0/6001D68
sender_write_location : 0/6001D68
sender_flush_location : 0/6001D68
sender_replay_location : 0/6001D68
receiver_received_location : 0/6001D68
receiver_write_location : 0/6001D68
receiver_flush_location : 0/6001D68
receiver_replay_location : 0/6001D68
sync_percent : 100%
channel : 192.168.1.102:55914<--192.168.1.107:12211
查看数据是否同步过来
[omm@localhost data]$ gsql -U hxl -d db_test -h 127.0.0.1 -p 5432
Password for user hxl:
gsql ((openGauss 6.0.5 build 5ba692ab) compiled at 2026-05-07 12:36:04 commit 0 last mr 9228 )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
db_test=> select * from tb_test;
id | name
----+--------
1 | name1
2 | name2
3 | name3
4 | name4
5 | name5
6 | name6
7 | name7
8 | name8
9 | name9
10 | name10
11 | name11
12 | name12
13 | name13
14 | name14
15 | name15
(15 rows)
发现数据也同步过来了,说明使用failover切换后,原来的主库以从库身份启动 也会同步目前新主库写入的数据
因为切换后,主从同步信息还存在配置文件,切换过程不会修改配置文件.
浙公网安备 33010602011771号