一.单机主从部署

0.前提条件  官方指导文档

已完成用户组和普通用户的创建。
所有服务器操作系统和网络均正常运行。
普通用户必须有数据库包解压路径、安装路径的读、写和执行操作权限,并且安装路径必须为空。
普通用户对下载的openGauss压缩包有执行权限。
安装前请检查指定的openGauss端口矩阵中所有端口是否被占用,如果被占用请更改端口或者停止当前使用端口进程。

1.主备部署形态

主备模式相当于两个数据副本,主机和备机各一个数据副本,备机接受日志、执行日志回放。

对外监听端口5432,主机监听端口5432,备机监听端口5632;复制通道主机端口5433,备机端口随机。

grep 'port =' /gauss/gaussdb/data/master/postgresql.conf
grep 'port =' /gauss/gaussdb/data/slave/postgresql.conf

上一套openGauss数据库,如何规划部署?(续)---单机主从_open高斯数据库

2.部署

#master
su - gauss 
cd /gauss/soft
wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.0.0/x86/openGauss-3.0.0-CentOS-64bit-all.tar.gz
tar xf openGauss-3.0.0-CentOS-64bit-all.tar.gz 
tar xf openGauss-3.0.0-CentOS-64bit.tar.bz2 -C /gauss/gaussdb/
cd /gauss/gaussdb/simpleInstall
#指定gauss数据库密码安装-w Alibaby@007  --multinode多节点
sh install.sh -w Alibaby@007 --multinode
#提示Would you like to create a demo database (yes/no)? 输入yes
#安装完成后提示
primary: gs_ctl start|stop|restart -D $GAUSSHOME/data/master -M primary
standby: gs_ctl start|stop|restart -D $GAUSSHOME/data/slave -M standby
#检查
ps ux | grep gaussdb
netstat -tlunp | grep 5432 #对外监听端口无需配置,postgresql.conf中会自动添加listen_addresses = 'localhost,192.168.77.161'
gs_ctl query -D $GAUSSHOME/data/master
gs_ctl query -D $GAUSSHOME/data/slave

#启停没有严格上的限制,又不会主备切换,当primary停了,standby还是standby
#启
先启主库,再启备库
gs_ctl start -D $GAUSSHOME/data/master
gs_ctl start -D $GAUSSHOME/data/slave
#停
先停备库,再停主库
gs_ctl stop -D $GAUSSHOME/data/slave
gs_ctl stop -D $GAUSSHOME/data/master
#重启
gs_ctl restart -D $GAUSSHOME/data/slave
gs_ctl restart -D $GAUSSHOME/data/master

主库状态Primary

#主库状态
gs_ctl -D query /gauss/gaussdb/data/master

HA state:           
	local_role                     : Primary
	static_connections             : 1
	db_state                       : Normal
	detail_information             : Normal

 Senders info:       
	sender_pid                     : 4748
	local_role                     : Primary
	peer_role                      : Standby
	peer_state                     : Normal
	state                          : Streaming
	sender_sent_location           : 0/403D5C0
	sender_write_location          : 0/403D5C0
	sender_flush_location          : 0/403D5C0
	sender_replay_location         : 0/403D5C0
	receiver_received_location     : 0/403D5C0
	receiver_write_location        : 0/403D5C0
	receiver_flush_location        : 0/403D5C0
	receiver_replay_location       : 0/403D5C0
	sync_percent                   : 100%
	sync_state                     : Sync
	sync_priority                  : 1
	sync_most_available            : Off
	channel                        : 192.168.77.161:5433-->192.168.77.161:38616

备库状态Standby

#gs_ctl query -D /gauss/gaussdb/data/slave
HA state:           
	local_role                     : Standby
	static_connections             : 1
	db_state                       : Normal
	detail_information             : Normal

 Senders info:       
No information 
 Receiver info:      
	receiver_pid                   : 4746
	local_role                     : Standby
	peer_role                      : Primary
	peer_state                     : Normal
	state                          : Normal
	sender_sent_location           : 0/403D5C0
	sender_write_location          : 0/403D5C0
	sender_flush_location          : 0/403D5C0
	sender_replay_location         : 0/403D5C0
	receiver_received_location     : 0/403D5C0
	receiver_write_location        : 0/403D5C0
	receiver_flush_location        : 0/403D5C0
	receiver_replay_location       : 0/403D5C0
	sync_percent                   : 100%
	channel                        : 192.168.77.161:38616<--192.168.77.161:5433

3.验证主从复制

#登录
gsql -h 127.0.0.1 -p 5432 -Ugauss -d postgres -W Alibaby@007
#创建用户并授权
create user alibaby with password 'Alibaby@007';
grant all privileges to alibaby;
create database alibabydb owner alibaby;
grant all privileges on all tables in schema public to alibaby;
grant all privileges on database alibabydb to alibaby;
\q
#配置文件修改访问控制
#主
sed -i '90a\host alibabydb alibaby 192.168.77.0/24 sha256' /gauss/gaussdb/data/master/pg_hba.conf
#追加到后面未生效
cat >>/gauss/gaussdb/data/master/pg_hba.conf<<'EOF'
host alibabydb alibaby 192.168.77.0/24 sha256
EOF
#备
sed -i '90a\host alibabydb alibaby 192.168.77.0/24 sha256' /gauss/gaussdb/data/slave/pg_hba.conf
#追加到后面未生效
cat >>/gauss/gaussdb/data/slave/pg_hba.conf<<'EOF'
host alibabydb alibaby 192.168.77.0/24 sha256
EOF
#重启
gs_ctl restart -D $GAUSSHOME/data/slave
gs_ctl restart -D $GAUSSHOME/data/master
#alibaby用户登录
gsql -h 192.168.77.161 -p 5432 -Ualibaby -d alibabydb -W Alibaby@007
#创建测试数据
#创建表
CREATE TABLE student
(
    std_id INT PRIMARY KEY,
    std_name VARCHAR(20) NOT NULL,
    std_sex VARCHAR(6),
    std_birth DATE,
    std_in DATE NOT NULL,
    std_address VARCHAR(100)
);
#插入数据
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (1,'张一','男','1993-01-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (2,'张二','男','1993-01-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (3,'张三','男','1993-01-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (4,'张四','男','1993-01-04','2011-09-01','江苏省南京市雨花台区');
#查询
select * from student;
#退出
\q
#查看备机同步进度,看到进度100%
gs_ctl query -D /gauss/gaussdb/data/slave | grep sync_percent
#alibaby登录备机
gsql -h 192.168.77.161 -p 5632 -Ualibaby -d alibabydb -W Alibaby@007
#验证查询
select * from student;
\q

报错:

gsql: FATAL:  Forbid remote connection with trust method!

解决:

#配置文件修改访问控制
#主
sed -i '90a\host alibabydb alibaby 192.168.77.0/24 sha256' /gauss/gaussdb/data/master/pg_hba.conf
#备
sed -i '90a\host alibabydb alibaby 192.168.77.0/24 sha256' /gauss/gaussdb/data/slave/pg_hba.conf
#重启
gs_ctl restart -D $GAUSSHOME/data/slave
gs_ctl restart -D $GAUSSHOME/data/master