1 您需要了解
- 数据库及操作系统具体版本请参考博文 G024 单实例
- 请提前完成2台 openEuler 安装,采用
minimal
最小化安装,具体可参考博文 G015 欧拉Linux安装 - 本篇认为您已具备单实例搭建的能力,否则请参考下列相关文章
G024
完成单实例搭建 - 标题带有
主备节点
字样,表示该步骤在主节点和备节点都要操作 - 为有更好的浏览体验,您可以点击文章左上方目录按钮来显示文章整体目录结构
2 环境规划
主机名 | IP地址 | 角色 |
---|---|---|
cloudcsp | 192.168.200.174 | Primary 主库 |
cloudcss | 192.168.200.175 | Standby 备库 |
3 系统环境配置(主备节点)
3.1 IP及主机名
#主节点
[root@cloudcsp ~]# echo '192.168.200.174 cloudcsp' >> /etc/hosts
[root@cloudcsp ~]# echo '192.168.200.175 cloudcss' >> /etc/hosts
[root@cloudcsp ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.174 cloudcsp
192.168.200.175 cloudcss
#备节点
[root@cloudcss ~]# echo '192.168.200.175 cloudcss' >> /etc/hosts
[root@cloudcss ~]# echo '192.168.200.174 cloudcsp' >> /etc/hosts
[root@cloudcss ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.175 cloudcss
192.168.200.174 cloudcsp
3.2 关闭防火墙
#主节点
[root@cloudcsp ~]# systemctl stop firewalld
[root@cloudcsp ~]# systemctl disable firewalld
#备节点
[root@cloudcss ~]# systemctl stop firewalld
[root@cloudcss ~]# systemctl disable firewalld
3.3 设置字符集及其他环境变量
#主节点
[root@cloudcsp ~]# vi /etc/profile
[root@cloudcsp ~]# cat /etc/profile |grep export |tail -n 3
export LANG=en_US.UTF-8
export packagePath=/opt/software/openGauss
export LD_LIBRARY_PATH=$packagePath/script/gspylib/clib:$LD_LIBRARY_PATH
[root@cloudcsp ~]# source /etc/profile
#备节点
[root@cloudcss ~]# vi /etc/profile
[root@cloudcss ~]# cat /etc/profile |grep export |tail -n 3
export LANG=en_US.UTF-8
export packagePath=/opt/software/openGauss
export LD_LIBRARY_PATH=$packagePath/script/gspylib/clib:$LD_LIBRARY_PATH
[root@cloudcss ~]# source /etc/profile
3.4 取消内存限制参数
#主节点
[root@cloudcsp ~]# vi /etc/profile.d/performance.sh
[root@cloudcsp ~]# cat /etc/profile.d/performance.sh
#sysctl -w vm.min_free_kbytes=112640 &> /dev/null
sysctl -w vm.dirty_ratio=60 &> /dev/null
sysctl -w kernel.sched_autogroup_enabled=0 &> /dev/null
#备节点
[root@cloudcss ~]# vi /etc/profile.d/performance.sh
[root@cloudcss ~]# cat /etc/profile.d/performance.sh
#sysctl -w vm.min_free_kbytes=112640 &> /dev/null
sysctl -w vm.dirty_ratio=60 &> /dev/null
sysctl -w kernel.sched_autogroup_enabled=0 &> /dev/null
3.5 更换python版本
安装过程中会配置 openGauss 用户互信,openEuler 需要用到 python 3.7.x 版本命令,但默认版本为 2.7.x ,所以需要更换。
#主节点
[root@cloudcsp ~]# python -V
Python 2.7.18
[root@cloudcsp ~]# mv /usr/bin/python /usr/bin/python.bak
[root@cloudcsp ~]# ln -s /usr/bin/python3 /usr/bin/python
[root@cloudcsp ~]# python -V
Python 3.7.9
#备节点
[root@cloudcss ~]# python -V
Python 2.7.18
[root@cloudcss ~]# mv /usr/bin/python /usr/bin/python.bak
[root@cloudcss ~]# ln -s /usr/bin/python3 /usr/bin/python
[root@cloudcss ~]# python -V
Python 3.7.9
3.6 安装相关软件包
#主节点
[root@cloudcsp ~]# yum install -y libaio* \
tar \
vim \
net*
#备节点
[root@cloudcss ~]# yum install -y libaio* \
tar \
vim \
net*
4 安装 openGauss 数据库(仅主节点)
4.1 上传并解压软件
[root@cloudcsp ~]# mkdir -p /opt/software/openGauss
#上传软件至此目录
[root@cloudcsp ~]# cd /opt/software/openGauss
[root@cloudcsp openGauss]# tar -zxvf openGauss-2.1.0-openEuler-64bit-all.tar.gz
[root@cloudcsp openGauss]# tar -zxvf openGauss-2.1.0-openEuler-64bit-om.tar.gz
4.2 创建 XML 文件
安装前需创建 XML 文件,包含集群名称、服务器名称、安装路径、IP及端口号等,用于告知 openGauss 如何安装,以下为一主一备配置方案。
[root@cloudcsp openGauss]# vim clusterconfig.xml
[root@cloudcsp openGauss]# cat clusterconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<CLUSTER>
<PARAM name="clusterName" value="ClusterPS" />
<PARAM name="nodeNames" value="cloudcsp,cloudcss" />
<PARAM name="gaussdbAppPath" value="/opt/huawei/install/app" />
<PARAM name="gaussdbLogPath" value="/var/log/omm" />
<PARAM name="tmpMppdbPath" value="/opt/huawei/tmp"/>
<PARAM name="gaussdbToolPath" value="/opt/huawei/install/om" />
<PARAM name="corePath" value="/opt/huawei/corefile"/>
<PARAM name="backIp1s" value="192.168.200.174,192.168.200.175"/>
</CLUSTER>
<DEVICELIST>
<DEVICE sn="cloudcsp">
<PARAM name="name" value="cloudcsp"/>
<PARAM name="azName" value="AZ1"/>
<PARAM name="azPriority" value="1"/>
<PARAM name="backIp1" value="192.168.200.174"/>
<PARAM name="sshIp1" value="192.168.200.174"/>
<PARAM name="dataNum" value="1"/>
<PARAM name="dataPortBase" value="15400"/>
<PARAM name="dataNode1" value="/opt/huawei/install/data/dn,cloudcss,/opt/huawei/install/data/dn"/>
<PARAM name="dataNode1_syncNum" value="0"/>
</DEVICE>
<DEVICE sn="cloudcss">
<PARAM name="name" value="cloudcss"/>
<PARAM name="azName" value="AZ1"/>
<PARAM name="azPriority" value="1"/>
<PARAM name="backIp1" value="192.168.200.175"/>
<PARAM name="sshIp1" value="192.168.200.175"/>
</DEVICE>
</DEVICELIST>
</ROOT>
4.3 配置用户和组及互信
[root@cloudcsp openGauss]# python /opt/software/openGauss/script/gs_preinstall -U omm -G dbgrp -X /opt/software/openGauss/clusterconfig.xml
#输出
Parsing the configuration file.
Successfully parsed the configuration file.
Installing the tools on the local node.
Successfully installed the tools on the local node.
Are you sure you want to create trust for root (yes/no)? yes
Please enter password for root.
Password:
Creating SSH trust for the root permission user.
Checking network information.
All nodes in the network are Normal.
Successfully checked network information.
Creating SSH trust.
Creating the local key file.
Successfully created the local key files.
Appending local ID to authorized_keys.
Successfully appended local ID to authorized_keys.
Updating the known_hosts file.
Successfully updated the known_hosts file.
Appending authorized_key on the remote node.
Successfully appended authorized_key on all remote node.
Checking common authentication file content.
Successfully checked common authentication content.
Distributing SSH trust file to all node.
Successfully distributed SSH trust file to all node.
Verifying SSH trust on all hosts.
Successfully verified SSH trust on all hosts.
Successfully created SSH trust.
Successfully created SSH trust for the root permission user.
Setting pssh path
Successfully set core path.
Distributing package.
Begin to distribute package to tool path.
Successfully distribute package to tool path.
Begin to distribute package to package path.
Successfully distribute package to package path.
Successfully distributed package.
Are you sure you want to create the user[omm] and create trust for it (yes/no)? yes
Please enter password for cluster user.
Password:
Please enter password for cluster user again.
Password:
Successfully created [omm] user on all nodes.
Preparing SSH service.
Successfully prepared SSH service.
Installing the tools in the cluster.
Successfully installed the tools in the cluster.
Checking hostname mapping.
Successfully checked hostname mapping.
Creating SSH trust for [omm] user.
Checking network information.
All nodes in the network are Normal.
Successfully checked network information.
Creating SSH trust.
Creating the local key file.
Successfully created the local key files.
Appending local ID to authorized_keys.
Successfully appended local ID to authorized_keys.
Updating the known_hosts file.
Successfully updated the known_hosts file.
Appending authorized_key on the remote node.
Successfully appended authorized_key on all remote node.
Checking common authentication file content.
Successfully checked common authentication content.
Distributing SSH trust file to all node.
Successfully distributed SSH trust file to all node.
Verifying SSH trust on all hosts.
Successfully verified SSH trust on all hosts.
Successfully created SSH trust.
Successfully created SSH trust for [omm] user.
Checking OS software.
Successfully check os software.
Checking OS version.
Successfully checked OS version.
Creating cluster's path.
Successfully created cluster's path.
Set and check OS parameter.
Setting OS parameters.
Successfully set OS parameters.
Warning: Installation environment contains some warning messages.
Please get more details by "/opt/software/openGauss/script/gs_checkos -i A -h cloudcsp,cloudcss --detail".
Set and check OS parameter completed.
Preparing CRON service.
Successfully prepared CRON service.
Setting user environmental variables.
Successfully set user environmental variables.
Setting the dynamic link library.
Successfully set the dynamic link library.
Setting Core file
Successfully set core path.
Setting pssh path
Successfully set pssh path.
Setting Cgroup.
Successfully set Cgroup.
Set ARM Optimization.
No need to set ARM Optimization.
Fixing server package owner.
Setting finish flag.
Successfully set finish flag.
Preinstallation succeeded.
4.4 创建数据库
安装脚本 gs_install 必须以 omm 执行,否则执行脚本会报错。
[root@cloudcsp openGauss]# chmod -R 755 /opt/software/openGauss/script/
[root@cloudcsp openGauss]# su - omm
[omm@cloudcsp ~]$ cd /opt/software/openGauss/script/
[omm@cloudcsp script]$ gs_install -X /opt/software/openGauss/clusterconfig.xml
#输出
Parsing the configuration file.
Check preinstall on every node.
Successfully checked preinstall on every node.
Creating the backup directory.
Successfully created the backup directory.
begin deploy..
Installing the cluster.
begin prepare Install Cluster..
Checking the installation environment on all nodes.
begin install Cluster..
Installing applications on all nodes.
Successfully installed APP.
begin init Instance..
encrypt cipher and rand files for database.
Please enter password for database:
Please repeat for database:
begin to create CA cert files
The sslcert will be generated in /opt/huawei/install/app/share/sslcert/om
Cluster installation is completed.
Configuring.
Deleting instances from all nodes.
Successfully deleted instances from all nodes.
Checking node configuration on all nodes.
Initializing instances on all nodes.
Updating instance configuration on all nodes.
Check consistence of memCheck and coresCheck on database nodes.
Successful check consistence of memCheck and coresCheck on all nodes.
Configuring pg_hba on all nodes.
Configuration is completed.
Successfully started cluster.
Successfully installed application.
end deploy..
4.5 查看集群状态
[omm@cloudcsp script]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
-----------------------------------------------------------------------------------------------
1 cloudcsp 192.168.200.174 15400 6001 /opt/huawei/install/data/dn P Primary Normal
2 cloudcss 192.168.200.175 15400 6002 /opt/huawei/install/data/dn S Standby Normal
4.6 初始化数据库
[omm@cloudcsp script]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:54 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# CREATE DATABASE mydb WITH ENCODING 'GBK' template = template0;
CREATE DATABASE
openGauss=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+-----------+---------+-------+-------------------
mydb | omm | GBK | C | C |
postgres | omm | SQL_ASCII | C | C |
template0 | omm | SQL_ASCII | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | SQL_ASCII | C | C | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
5 主备验证
5.1 主库创建测试表
openGauss=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+-----------+---------+-------+-------------------
mydb | omm | GBK | C | C |
postgres | omm | SQL_ASCII | C | C |
template0 | omm | SQL_ASCII | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | SQL_ASCII | C | C | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
openGauss=# \c mydb
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "mydb" as user "omm".
mydb=# create table abc(id int);
CREATE TABLE
mydb=# insert into abc values(1);
INSERT 0 1
mydb=# insert into abc values(2);
INSERT 0 1
mydb=# insert into abc values(3);
INSERT 0 1
mydb=# select * from abc;
id
----
1
2
3
(3 rows)
5.2 备库开启只读验证
5.2.1 停止主备集群
[omm@cloudcsp dn]$ gs_om -t stop
Stopping cluster.
=========================================
Successfully stopped cluster.
=========================================
End stop cluster.
[omm@cloudcsp dn]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Unavailable
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
-----------------------------------------------------------------------------------------------
1 cloudcsp 192.168.200.174 15400 6001 /opt/huawei/install/data/dn P Down Manually stopped
2 cloudcss 192.168.200.175 15400 6002 /opt/huawei/install/data/dn S Down Manually stopped
5.2.2 修改配置文件
#主库
[omm@cloudcsp dn]$ pwd
/opt/huawei/install/data/dn
[omm@cloudcsp dn]$ vim postgresql.conf
wal_level = hot_standby
hot_standby = on
hot_standby_feedback = on
#备库
[omm@cloudcss ~]$ cd /opt/huawei/install/data/dn/
[omm@cloudcss dn]$ vim postgresql.conf
wal_level = hot_standby
hot_standby = on
hot_standby_feedback = on
5.2.3 启动主备集群
[omm@cloudcsp dn]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] cloudcsp
2022-07-20 00:55:00.463 62d6e1e4.1 [unknown] 140465496884160 [unknown] 0 dn_6001_6002 01000 0 [BACKEND] WARNING: Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (4439 Mbytes) is larger.
[SUCCESS] cloudcss
2022-07-20 00:55:16.928 62d6e1f4.1 [unknown] 140246532486080 [unknown] 0 dn_6001_6002 01000 0 [BACKEND] WARNING: Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (4439 Mbytes) is larger.
=========================================
Successfully started.
[omm@cloudcsp dn]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
-----------------------------------------------------------------------------------------------
1 cloudcsp 192.168.200.174 15400 6001 /opt/huawei/install/data/dn P Primary Normal
2 cloudcss 192.168.200.175 15400 6002 /opt/huawei/install/data/dn S Standby Normal
5.2.4 登录备库验证数据
[omm@cloudcss dn]$ gsql -d mydb -p 15400 -r
gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:54 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
mydb=# select * from abc;
id
----
1
2
3
(3 rows)
5.3 主备切换(switchover)
确保 openGauss 状态正常,所有业务结束后,再进行切换操作。
5.3.1 查看主备状态
[omm@cloudcss ~]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
-----------------------------------------------------------------------------------------------
1 cloudcsp 192.168.200.174 15400 6001 /opt/huawei/install/data/dn P Primary Normal
2 cloudcss 192.168.200.175 15400 6002 /opt/huawei/install/data/dn S Standby Normal
5.3.2 备节点执行切换
[omm@cloudcss ~]$ gs_ctl switchover -D /opt/huawei/install/data/dn/
[2022-07-20 01:07:10.550][33633][][gs_ctl]: gs_ctl switchover ,datadir is /opt/huawei/install/data/dn
[2022-07-20 01:07:10.550][33633][][gs_ctl]: switchover term (1)
[2022-07-20 01:07:10.554][33633][][gs_ctl]: waiting for server to switchover.............
[2022-07-20 01:07:20.662][33633][][gs_ctl]: done
[2022-07-20 01:07:20.662][33633][][gs_ctl]: switchover completed (/opt/huawei/install/data/dn)
[omm@cloudcss ~]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
-----------------------------------------------------------------------------------------------
1 cloudcsp 192.168.200.174 15400 6001 /opt/huawei/install/data/dn P Standby Normal
2 cloudcss 192.168.200.175 15400 6002 /opt/huawei/install/data/dn S Primary Normal
- END