本篇为在CentOS7 环境下PostgreSQL 15数据库主备模式部署
在线安装参考官网安装步骤即可,王孙较慢的可以离线安装 参考 章节3 安装包下载 提取码:pg15
系统环境准备: CentOS7系统 X86 架构
1系统环境配置
1.1系统配置
#系统为CentOS7 X86架构,2C/4G
cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
uname -r
3.10.0-1160.el7.x86_64
cat /proc/cpuinfo | grep 'processor' | wc -l
2
free -h
total used free shared buff/cache available
Mem: 3.7G 251M 3.0G 11M 491M 3.2G
Swap: 3.9G 0B 3.9G
1.2主机,ip规划
hostname | ip | 角色 |
---|---|---|
postgres1 | 192.168.1.11 | master |
postgres2 | 192.168.1.12 | slave |
# 配置hostname,ip
nmcli connection modify ens33 ipv4.method manual ipv4.addresses "192.168.1.11/24" ipv4.gateway 192.168.1.254 ipv4.dns "8.8.8.8,114.114.114.114" connection.autoconnect yes
hostnamectl set-hostname postgres1
nmcli connection modify ens33 ipv4.method manual ipv4.addresses "192.168.1.12/24" ipv4.gateway 192.168.1.254 ipv4.dns "8.8.8.8,114.114.114.114" connection.autoconnect yes
hostnamectl set-hostname postgres2
2在线安装postgres单节点模式
根据官网文档进行安装
官网安装说明
2.1安装
# Install the repository RPM:
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
#查看系统是否有自带的软件和用户并卸载删除
yum remove postgresql
userdel postgres
#不创建用户,安装 postgres 软件时,系统也会自动创建 postgres,家目录在/var/...下,我们自己创建用户并设置密码
useradd postgres
passwd postgres
# Install PostgreSQL:
yum install -y postgresql15-server
安装包下载 提取码:pg15
rpm -ivh libzstd-1.5.2-1.el7.x86_64.rpm
yum install -y postgresql15-server
配置本地yum源
cd /etc/yum.repos.d/
cat > local.repo << 'EOF'
[dvd]
name=dvd
baseurl=file:///mnt
enabled=1
gpgcheck=0
EOF
echo '/media/CentOS-7-x86_64-Everything-2009.iso /mnt iso9660 defaults 0 0' >> /etc/fstab
mount -a
yum -y install libicu
yum -y install postgresql15-server.x86_64
Running transaction
正在安装 : postgresql15-libs-15.7-1PGDG.rhel7.x86_64 1/3
正在安装 : postgresql15-15.7-1PGDG.rhel7.x86_64 2/3
正在安装 : postgresql15-server-15.7-1PGDG.rhel7.x86_64 3/3
验证中 : postgresql15-server-15.7-1PGDG.rhel7.x86_64 1/3
验证中 : postgresql15-15.7-1PGDG.rhel7.x86_64 2/3
验证中 : postgresql15-libs-15.7-1PGDG.rhel7.x86_64 3/3
已安装:
postgresql15-server.x86_64 0:15.7-1PGDG.rhel7
作为依赖被安装:
postgresql15.x86_64 0:15.7-1PGDG.rhel7 postgresql15-libs.x86_64 0:15.7-1PGDG.rhel7
完毕!
2.2创建数据目录并初始化
mkdir -p /u01/apps/pgsql/data
chown -R postgres.postgres /u01/apps/pgsql/
#初始化 postgres,此步骤也可按照官网初始化数据目录。默认目录在 /var/lib/pgsql/15/data/
[root@postgres1 ~]# su - postgres
[postgres@postgres1 ~]$ /usr/pgsql-15/bin/initdb -D /u01/apps/pgsql/data/
2.3用root用户修改服务启动配置
#修改数据目录,和初始化数据目录对应
[root@postgres1 ~]# vim /usr/lib/systemd/system/postgresql-15.service
# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/15/data/
Environment=PGDATA=/u01/apps/pgsql/data/
[root@postgres1 ~]# systemctl daemon-reload
2.4修改postgres配置
2.4.1修改pg_hba.conf
#末尾添加一行
[postgres@postgres1 ~]$ vim /u01/apps/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 md5
2.4.2修改postgresql.conf
#修改pgdata中的postgresql.conf内容,参考网站建议参数修改:
① 外网访问 https://pgtune.leopard.in.ua/#/
② 在其中输入自己服务器的参数,可以自动生成postgresql.conf配置文件中修改项
# DB Version: 15
# OS Type: linux
# DB Type: mixed
# Total Memory (RAM): 4 GB
# CPUs num: 2
# Connections num: 1000
# Data Storage: ssd
max_connections = 1000
shared_buffers = 1GB
effective_cache_size = 3GB
maintenance_work_mem = 256MB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 1.1
effective_io_concurrency = 200
work_mem = 262kB
huge_pages = off
min_wal_size = 1GB
max_wal_size = 4GB
其他配置
listen_addresses = '*'
port = 25432
#日志的配置项
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
2.5启动postgres
[root@postgres1 ~]# systemctl start postgresql-15.service
2.6登录并测试
# 如果没有修改默认端口号,直接输入 psql 即可进入,注意是postgres 登录
[postgres@postgres1 ~]$ psql -p 25432
-- 设置密码
postgres=# \password
Enter new password for user "postgres":
Enter it again:
-- 测试
postgres=# create database test;
postgres=# \l
postgres=# \c test
-- 创建schema
test=# create schema test;
test=# \dn
-- 创建表
test=# create table test.test ("name" varchar(64),"age" varchar(3));
CREATE TABLE
test=# select * from pg_tables where schemaname='test';
-- 退出
test=# \q
至此,postgres1节点上单节点模式部署完毕
3部署postgres2(离线安装)
#安装包
[root@postgres2 ~]# ll
总用量 15212
-rw-------. 1 root root 1257 3月 29 2023 anaconda-ks.cfg
-rw-r--r-- 1 root root 7202728 5月 14 16:45 libicu-50.2-4.el7_7.x86_64.rpm
-rw-r--r-- 1 root root 287912 5月 14 16:45 libzstd-1.5.2-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 1648204 5月 14 16:45 postgresql15-15.7-1PGDG.rhel7.x86_64.rpm
-rw-r--r-- 1 root root 292876 5月 14 16:45 postgresql15-libs-15.7-1PGDG.rhel7.x86_64.rpm
-rw-r--r-- 1 root root 6129544 5月 14 16:45 postgresql15-server-15.7-1PGDG.rhel7.x86_64.rpm
[root@postgres2 ~]# useradd postgres
[root@postgres2 ~]# passwd postgres
[root@postgres2 ~]# rpm -ivh libzstd-1.5.2-1.el7.x86_64.rpm
[root@postgres2 ~]# rpm -ivh libicu-50.2-4.el7_7.x86_64.rpm
[root@postgres2 ~]# rpm -ivh postgresql15-libs-15.7-1PGDG.rhel7.x86_64.rpm
[root@postgres2 ~]# rpm -ivh postgresql15-15.7-1PGDG.rhel7.x86_64.rpm
[root@postgres2 ~]# rpm -ivh postgresql15-server-15.7-1PGDG.rhel7.x86_64.rpm
4主从模式部署
postgres1节点为master服务器,postgres2节点为slave服务器
4.1配置master
4.1.1修改配置文件
# 添加配置
编辑 /u01/apps/pgsql/data/pg_hba.conf 添加一行,设置一个replica的用户用于同步数据
host replication replica 0.0.0.0/0 md5
编辑 /u01/apps/pgsql/data/postgresql.conf
# 同步备节点名称为任意
synchronous_standby_names = '*'
# 开启热备,实时同步数据
hot_standby = on
4.1.2进入master ,创建用户 replica 及其密码
[postgres@postgres1 ~]$ psql -p 25432
-- 默认为 on,在 on 模式下事务需要等备份数据库一起提交
postgres=# set synchronous_commit = off;
SET
-- 创建replica的角色用于同步数据
postgres=# create role replica login replication encrypted password '1';
CREATE ROLE
-- 查看用户
postgres=# \du
角色列表
角色名称 | 属性 | 成员属于
----------+--------------------------------------------+----------
postgres | 超级用户, 建立角色, 建立 DB, 复制, 绕过RLS | {}
replica | 复制 | {}
postgres=# \q
4.1.3重启master
[root@postgres1 ~]# systemctl restart postgresql-15.service
4.2配置slave
4.2.1同步数据到slave节点
[root@postgres2 ~]# mkdir -p /u01/apps/pgsql/data
[root@postgres2 ~]# chown -R postgres.postgres /u01/apps/pgsql/
[root@postgres2 ~]# su - postgres
[postgres@postgres2 ~]$ pg_basebackup -h 192.168.1.11 -p 25432 -U replica -F p -X stream -P -R -v -D /u01/apps/pgsql/data/
Password:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_17963"
31177/31177 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/2000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed
# -h –指定作为主服务器的主机。
# -D –指定数据目录。
# -U –指定连接用户。
# -P –启用进度报告。
# -v –启用详细模式。
# -R–启用恢复配置的创建:创建一个 standby.signal 文件,并将连接设置附加到数据目录下的postgresql.auto.conf。
# -X–用于在备份中包括所需的预写日志文件(WAL文件)。流的值表示在创建备份时流式传输WAL。
# -C –在开始备份之前,允许创建由-S选项命名的复制插槽。
# -S –指定复制插槽名称。
4.2.2修改启动配置文件
#修改数据目录
[root@postgres2 ~]# vim /usr/lib/systemd/system/postgresql-15.service
# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/15/data/
Environment=PGDATA=/u01/apps/pgsql/data/
[root@postgres2 ~]# systemctl daemon-reload
4.2.3配置standby.signal
# 此文件为同步数据时自动生成,standby.signal 文件内容参考此文件书写
[postgres@postgres2 ~]$ cat /u01/apps/pgsql/data/postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = 'user=replica password=1 channel_binding=prefer host=192.168.1.11 port=25432 sslmode=prefer sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'
# 修改 standby.signal 配置
[postgres@postgres2 ~]$ vim /u01/apps/pgsql/data/standby.signal
# 热备,实时同步
standby_mode = 'on'
primary_conninfo = 'user=replica password=1 channel_binding=prefer host=192.168.1.11 port=25432 sslmode=prefer sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'
4.2.4启动slave
[root@postgres2 ~]# chmod 700 /u01/apps/pgsql/data/
[root@postgres2 ~]# systemctl start postgresql-15.service
4.3验证主从
-- master上创建表
test=# create table test.renyuan(id uuid,name varchar(20));
-- slave查看,注意切换到test库
test=# select * from pg_tables where schemaname='test';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-----------+------------+------------+------------+----------+-------------+-------------
test | test | postgres | | f | f | f | f
test | renyuan | postgres | | f | f | f | f
(2 rows)
验证成功