在Docker中快速体验openGauss主备环境数据库
参考:
https://hub.docker.com/r/enmotech/opengauss
创建主从复制的openGauss容器
- 拉取容器镜像
- 运行脚本create_master_slave.sh,按照提示输入所需参数,或者直接使用默认值,即可自动创建openGauss一主一备架构的两个容器。
上述脚本有多个自定义参数,以下为这些参数的参数名称(解释)[默认值]。
OG_SUBNET (容器所在网段) [172.11.0.0/24]
GS_PASSWORD (定义数据库密码)[Enmo@123]
MASTER_IP (主库IP)[172.11.0.101]
SLAVE_1_IP (备库IP)[172.11.0.102]
MASTER_HOST_PORT (主库数据库服务端口)[5432]
MASTER_LOCAL_PORT (主库通信端口)[5434]
SLAVE_1_HOST_PORT (备库数据库服务端口)[6432]
SLAVE_1_LOCAL_PORT (备库通信端口)[6434]
MASTER_NODENAME (主节点名称)[opengauss_master]
SLAVE_NODENAME (备节点名称)[opengauss_slave1]
使用示例
拉取镜像
docker pull enmotech/opengauss:latest
获取创建主备容器脚本并运行
wget https://raw.githubusercontent.com/enmotech/enmotech-docker-opengauss/master/create_master_slave.sh
chmod +x create_master_slave.sh
docker network rm opengaussnetwork
# 全部回车默认,最后版本的地方输入 latest
[root@docker35 ~]# chmod +x create_master_slave.sh
[root@docker35 ~]# ./create_master_slave.sh
Please input OG_SUBNET (容器所在网段) [172.11.0.0/24]:
OG_SUBNET set 172.11.0.0/24
Please input GS_PASSWORD (定义数据库密码)[Enmo@123]:
GS_PASSWORD set Enmo@123
Please input MASTER_IP (主库IP)[172.11.0.101]:
MASTER_IP set 172.11.0.101
Please input SLAVE_1_IP (备库IP)[172.11.0.102]:
SLAVE_1_IP set 172.11.0.102
Please input MASTER_HOST_PORT (主库数据库服务端口)[5432]:
MASTER_HOST_PORT set 5432
Please input MASTER_LOCAL_PORT (主库通信端口)[5434]:
MASTER_LOCAL_PORT set 5434
Please input SLAVE_1_HOST_PORT (备库数据库服务端口)[6432]:
SLAVE_1_HOST_PORT set 6432
Please input SLAVE_1_LOCAL_PORT (备库通信端口)[6434]:
SLAVE_1_LOCAL_PORT set 6434
Please input MASTER_NODENAME [opengauss_master]:
MASTER_NODENAME set opengauss_master
Please input SLAVE_NODENAME [opengauss_slave1]:
SLAVE_NODENAME set opengauss_slave1
Please input openGauss VERSION [1.1.0]: latest
openGauss VERSION set latest
starting
cf57b86811530658bafb592d3427bd0058a590bcbf9169607d2f83446c3b1a83
OpenGauss Database Network Created.
2d78115504cf26d5074d0a85112f34425531abcbd26c099b40f4f480c73f351a
OpenGauss Database Master Docker Container created.
1a485df6d6a885cb427262727560509148f5fb3744dc58f358f6968b391be4e1
OpenGauss Database Slave1 Docker Container created.
验证主备状态
主库
# docker exec -it opengauss_master /bin/bash
# su - omm
omm@opengauss_master:~$ gs_ctl status -D /var/lib/opengauss/data/
[2022-01-07 04:09:26.656][354][][gs_ctl]: gs_ctl status,datadir is /var/lib/opengauss/data
gs_ctl: server is running (PID: 1)
/usr/local/opengauss/bin/gaussdb "-M" "primary"
omm@opengauss_master:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:09:43.897][358][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
sender_pid : 345
local_role : Primary
peer_role : Standby
peer_state : Normal
state : Streaming
sender_sent_location : 0/4000268
sender_write_location : 0/4000268
sender_flush_location : 0/4000268
sender_replay_location : 0/4000268
receiver_received_location : 0/4000268
receiver_write_location : 0/4000268
receiver_flush_location : 0/4000268
receiver_replay_location : 0/4000268
sync_percent : 100%
sync_state : Sync
sync_priority : 1
sync_most_available : On
channel : 172.11.0.101:5434-->172.11.0.102:49112
Receiver info:
No information
备库
# docker exec -it opengauss_slave1 /bin/bash
# su - omm
omm@opengauss_slave1:/var/lib/opengauss/data$ gs_ctl status -D /var/lib/opengauss/data/
[2022-01-07 04:08:49.365][365][][gs_ctl]: gs_ctl status,datadir is /var/lib/opengauss/data
gs_ctl: server is running (PID: 1)
/usr/local/opengauss/bin/gaussdb "-M" "standby"
omm@opengauss_slave1:~$
omm@opengauss_slave1:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:10:29.842][374][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
receiver_pid : 360
local_role : Standby
peer_role : Primary
peer_state : Normal
state : Normal
sender_sent_location : 0/4000268
sender_write_location : 0/4000268
sender_flush_location : 0/4000268
sender_replay_location : 0/4000268
receiver_received_location : 0/4000268
receiver_write_location : 0/4000268
receiver_flush_location : 0/4000268
receiver_replay_location : 0/4000268
sync_percent : 100%
channel : 172.11.0.102:49112<--172.11.0.101:5434
配置远程访问
alter system set password_policy=0;
create user lhr with password 'lhr' sysadmin ;
grant all PRIVILEGES to lhr;
psql -d postgres -U lhr -h 192.168.66.35 -p5432
psql -d postgres -U lhr -h 192.168.66.35 -p6432
C:\Users\lhrxxt>psql -d postgres -U lhr -h 192.168.66.35 -p5432
Password for user lhr:
psql (14.0, server 9.2.4)
Type "help" for help.
postgres=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
omm | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
主从切换
主从Switchover切换
在备机做切换:
omm@opengauss_slave1:~$ gs_ctl switchover -D /var/lib/opengauss/data/
[2022-01-07 04:16:30.442][389][][gs_ctl]: gs_ctl switchover ,datadir is /var/lib/opengauss/data
[2022-01-07 04:16:30.442][389][][gs_ctl]: switchover term (1)
[2022-01-07 04:16:30.454][389][][gs_ctl]: waiting for server to switchover.................
[2022-01-07 04:16:44.682][389][][gs_ctl]: done
[2022-01-07 04:16:44.682][389][][gs_ctl]: switchover completed (/var/lib/opengauss/data)
omm@opengauss_slave1:~$
omm@opengauss_slave1:~$
omm@opengauss_slave1:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:16:51.025][422][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
sender_pid : 417
local_role : Primary
peer_role : Standby
peer_state : Normal
state : Streaming
sender_sent_location : 0/40017B8
sender_write_location : 0/40017B8
sender_flush_location : 0/40017B8
sender_replay_location : 0/40017B8
receiver_received_location : 0/40017B8
receiver_write_location : 0/40017B8
receiver_flush_location : 0/40017B8
receiver_replay_location : 0/40017B8
sync_percent : 100%
sync_state : Sync
sync_priority : 1
sync_most_available : On
channel : 172.11.0.102:6434-->172.11.0.101:51326
Receiver info:
No information
omm@opengauss_master:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:17:11.064][421][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
receiver_pid : 420
local_role : Standby
peer_role : Primary
peer_state : Normal
state : Normal
sender_sent_location : 0/40017B8
sender_write_location : 0/40017B8
sender_flush_location : 0/40017B8
sender_replay_location : 0/40017B8
receiver_received_location : 0/40017B8
receiver_write_location : 0/40017B8
receiver_flush_location : 0/40017B8
receiver_replay_location : 0/40017B8
sync_percent : 100%
channel : 172.11.0.101:51326<--172.11.0.102:6434
主从Failover切换
在备机操作:
omm@opengauss_master:~$ gs_ctl failover -D /var/lib/opengauss/data/
[2022-01-07 04:18:03.280][426][][gs_ctl]: gs_ctl failover ,datadir is /var/lib/opengauss/data
[2022-01-07 04:18:03.280][426][][gs_ctl]: failover term (1)
[2022-01-07 04:18:03.293][426][][gs_ctl]: waiting for server to failover...
.[2022-01-07 04:18:04.311][426][][gs_ctl]: done
[2022-01-07 04:18:04.311][426][][gs_ctl]: failover completed (/var/lib/opengauss/data)
omm@opengauss_master:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:18:08.197][445][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
No information
omm@opengauss_slave1:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:18:26.231][431][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
No information
Receiver info:
No information
可见,现在为2个主库,重启“slave1”之后,“slave1”为standby,对于“slave1”应该为备库,状态为“Need repair”,所以,这里只能重建:
gs_ctl restart -D /var/lib/opengauss/data -M standby
omm@opengauss_slave1:~$ gs_ctl query -D /var/lib/opengauss/data/
[2022-01-07 04:20:29.775][218][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Standby
static_connections : 1
db_state : Need repair
detail_information : WAL segment removed
Senders info:
No information
Receiver info:
No information
-- 重新构建主备关系
gs_ctl build -D /var/lib/opengauss/data -b full -M standby
但是,由于是docker环境,所以,该命令不能执行,更多过程可以参考:安装openGauss极简版单节点和一主一备节点环境:https://www.xmmup.com/anzhuangopengaussjijianbandanjiedianheyizhuyibeijiedianhuanjing.html 有详细的切换流程。
- 点赞
- 收藏
- 关注作者
评论(0)