postgreSQL主从部署

一、环境准备

架构说明:

架构操作系统IP主机名PG版本端口repmgr插件版本磁盘空间内存CPU
主从centos7192.168.1.10、192.168.1.11pgserver01、pgserver02PostgreSQL 14.75433repmgr-5.3.150G4G2

备注:

pgsql主从先搭建单机环境再配置主从模式。
此处搭建不再说明,可看之前的单机搭建博客。
postgreSQL单机部署:https://blog.csdn.net/qq_41270538/article/details/131852097

1、修改postgresql.conf配置文件

只修改主库,因为备库后面会克隆主库的文件过来。
修改前备份上个版本的配置文件

1.1 关闭数据库

pg_ctl stop
mv /data/pgsql/data/postgresql.conf /data/pgsql/data/postgresql.confbak

其中以下参数需要根据机器配置和项目修改
cluster_name=‘repmgr01’ #实例名称
port=5433 shared_buffers=1GB # 物理内存的1/4
effective_cache_size=4GB # 调整为与内存一样大, 或者略小(减去shared_buffer)

1.2 启动数据库

pg_ctl start

二、安装集群软件

2、给postgres用户sudo权限(root用户执行)

[root@pgserver01 ~]# vi /etc/sudoers
## 允许“postgres”用户组的成员运行所有命令,且运行时不需要输入密码
...
%postgres        ALL=(ALL)       NOPASSWD: ALL
...

3、postgres用户配置免密

3.1 修改postgres密码

[root@pgserver01 ~]# echo "test" |passwd --stdin postgres
更改用户 postgres 的密码 。
passwd:所有的身份验证令牌已经成功更新。

[root@pgserver02 postgresql-14.7]# echo "test" |passwd --stdin postgres
更改用户 postgres 的密码 。
passwd:所有的身份验证令牌已经成功更新。

当前密码修改为 test

3.2 配置ssh信任(2主机都执行)

[postgres@pgserver01 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/postgres/.ssh/id_rsa):
Created directory '/home/postgres/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/postgres/.ssh/id_rsa.
Your public key has been saved in /home/postgres/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YlG2nPqfcQPhH0QswHUi/bV0j4xHevT0bv8LAQqPan8 postgres@pgserver01
The key's randomart image is:
+---[RSA 2048]----+
|       .=+o.o    |
|       +.+o+. = o|
|      ..+ .+.O *o|
|       o+..o= * +|
|      +.Soo .+ . |
|     ..o   o .. o|
|     o  . . +. ..|
|    . .  E + .. .|
|       .. o    .+|
+----[SHA256]-----+
[postgres@pgserver01 ~]$
[postgres@pgserver01 ~]$ ssh-copy-id 192.168.111.11
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/postgres/.ssh/id_rsa.pub"
The authenticity of host '192.168.111.11 (192.168.111.11)' can't be established.
ECDSA key fingerprint is SHA256:WtSxeJu+dlxUX3tht440s/LNaVjGsaSqkrq8q+DoO+g.
ECDSA key fingerprint is MD5:e7:13:db:79:1b:2d:d4:12:13:8c:9f:23:ca:aa:2a:89.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
postgres@192.168.111.11's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.111.11'"
and check to make sure that only the key(s) you wanted were added.

-----------------------------------------------------------------------------------------

[postgres@pgserver02 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/postgres/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/postgres/.ssh/id_rsa.
Your public key has been saved in /home/postgres/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WM4fuBxVfoTL6nDJGRJa58wDaw96ihwsMiRxPi5edE0 postgres@pgserver02
The key's randomart image is:
+---[RSA 2048]----+
|            ...  |
|. .    E+ .o..   |
| +    oo.O.....  |
|..o. ..*=o* o.   |
|o..o. .oS=.*     |
|+ o.o ..o+B.     |
|.+.o o oo+.      |
| .  o .   .      |
|                 |
+----[SHA256]-----+
[postgres@pgserver02 ~]$ ssh-copy-id 192.168.111.10
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/postgres/.ssh/id_rsa.pub"
The authenticity of host '192.168.111.10 (192.168.111.10)' can't be established.
ECDSA key fingerprint is SHA256:xxjO/yA4TEftK+HNga4C1HPUWOqrL7V7fNTzYn49tls.
ECDSA key fingerprint is MD5:1e:cc:3b:4f:9d:0a:83:2b:3d:7f:e6:47:b9:30:85:fb.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
postgres@192.168.111.10's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.111.10'"
and check to make sure that only the key(s) you wanted were added.

3.3 测试通信

[postgres@pgserver01 ~]$ ssh 192.168.111.11 date
2023年 12月 27日 星期三 16:27:32 CST

[postgres@pgserver02 ~]$ ssh 192.168.111.10 date
2023年 12月 27日 星期三 16:27:46 CST

4、安装repmgr软件

两个节点都需要安装

4.1 编译安装

[postgres@pgserver01 soft]$ tar -zxvf /data/soft/repmgr-5.3.1.tar.gz -C /data/soft/
[postgres@pgserver01 soft]$ mkdir /data/repmgr/
mkdir: 无法创建目录"/data/repmgr/": 文件已存在
[postgres@pgserver01 soft]$ cd /data/soft/repmgr-5.3.1
[postgres@pgserver01 repmgr-5.3.1]$ ./configure --prefix=/data/repmgr/
checking for a sed that does not truncate output... /bin/sed
checking for pg_config... /data/pgsql/install/bin/pg_config
configure: building against PostgreSQL 14.7
checking for gnused... no
checking for gsed... no
checking for sed... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating Makefile.global
config.status: creating config.h

[postgres@pgserver01 repmgr-5.3.1]$ make -j24 && make install -j24

注:repmgr安装后命令在/data/pgsql/install/bin目录下

5、在主库创建用户

如果没有用postgres用户,则创建 postgres 超级用户
CREATE USER postgres WITH SUPERUSER PASSWORD ‘test_2023’;

如果有postgres用户,修改密码
alter user postgres with password ‘test_2023’;

[postgres@pgserver01 repmgr-5.3.1]$ psql -U postgres
psql (14.7)
Type "help" for help.

postgres=# alter user postgres with password 'test_2023';
ALTER ROLE

**查询密码 **
select * from pg_shadow where usename = ‘postgres’;

postgres=# select * from pg_shadow where usename = 'postgres';
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |                                                                passwd
 | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+--------------------------------------------------------------------------------------------------------------------------------------
-+----------+-----------
 postgres |       10 | t           | t        | t       | t            | SCRAM-SHA-256$4096:I1dRMAoE3Pcgrd3ZjhQj7A==$W9joDJihqueb1KX6CEYZ1kASFHguYHZ2qFJqq4RpOX4=:mitiPSbUjvB3hW1RW5kBiempCi2TsvRRG0uhbK7zLKo=
 |          |
(1 row)

6、主库创建repmgr库存储元数据

创建 repmgr 用户,权限为 superuser

postgres=# CREATE USER repmgr WITH SUPERUSER PASSWORD 'test_2023';
CREATE ROLE

创建repmgr库

postgres=# create database repmgr owner repmgr;
CREATE DATABASE

7、主库节点编辑pg_hba.conf配置repmgr用户认证

[postgres@pgserver01 repmgr-5.3.1]$ cd /data/pgsql/data/
[postgres@pgserver01 data]$ cp pg_hba.conf pg_hba.confbak
[postgres@pgserver01 data]$ cat pg_hba.conf
#将以下内容添加到文件中
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication   repmgr                              trust
host    replication   repmgr      127.0.0.1/32            trust
host    replication   repmgr      0.0.0.0/0               trust

local   repmgr        repmgr                              trust
host    repmgr        repmgr      127.0.0.1/32            trust
host    repmgr        repmgr      0.0.0.0/0               trust

重新加载配置文件

[postgres@pgserver01 data]$ pg_ctl reload
server signaled

8、Repmgr主节点配置文件

节点1上配置repmgr.conf 以下三个字段需要修改:
node_id:本机节点的id,一般取IP最后一位
node_name:本机节点名称,需要和数据库配置文件中的cluster_name保持一致
conninfo:将host值替换为本地IP

vi /data/repmgr/repmgr.conf
#将以下内容复制到文件中
node_id=10
node_name='repmgr01'
conninfo='host=192.168.111.10 port=5433 user=repmgr dbname=repmgr connect_timeout=2'
data_directory= '/data/pgsql/data'
replication_user= 'repmgr'
replication_type= 'physical'
repmgr_bindir= '/data/pgsql/install/bin'
pg_bindir= '/data/pgsql/install/bin'
monitoring_history=yes
monitor_interval_secs=5
log_level='info'
log_file='/data/repmgr/repmgr.log'
failover='automatic'
connection_check_type=ping
reconnect_attempts=3
reconnect_interval=10

配置文件主要功能:定义数据库连接信息、命令路径、检测机制和服务脚本。
给脚本执行权限(验证是否需要)

cd /data/repmgr
chmod +x *.sh

9、PG配置文件添加Repmgr插件

编辑postgresql.conf文件,加入以下内容,表示当pg启动的时候载入repmgr组件

su - postgres
vi /data/pgsql/data/postgresql.conf
#450行左右,将repmgr增加到shared_preload_libraries中
shared_preload_libraries = 'passwordcheck,pg_stat_statements,auto_explain,repmgr'

重启数据库

[postgres@pgserver01 repmgr]$ pg_ctl restart
waiting for server to shut down...... done
server stopped
waiting for server to start....2023-12-27 16:56:04.188 CST [46501] LOG:  00000: redirecting log output to logging collector process
2023-12-27 16:56:04.188 CST [46501] HINT:  Future log output will appear in directory "log".
2023-12-27 16:56:04.188 CST [46501] LOCATION:  SysLogger_Start, syslogger.c:674
 done
server started

10、主节点注册到集群

[postgres@pgserver01 repmgr]$ repmgr -f /data/repmgr/repmgr.conf primary register
INFO: connecting to primary database...
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
NOTICE: primary node record (ID: 10) registered

注册后查看

[postgres@pgserver01 repmgr]$ repmgr -f /data/repmgr/repmgr.conf cluster show
 ID | Name     | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
----+----------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------
 10 | repmgr01 | primary | * running |          | default  | 100      | 1        | host=192.168.111.10 port=5433 user=repmgr dbname=repmgr connect_timeout=2

在数据库中查看集群信息

[postgres@pgserver01 repmgr]$ psql
psql (14.7)
Type "help" for help.

postgres=# select datname from pg_database;
  datname
-----------
 postgres
 template1
 template0
 repmgr
(4 rows)

postgres=# \c repmgr
You are now connected to database "repmgr" as user "postgres".
repmgr=# \x
Expanded display is on.
#切换到repmgr模式
repmgr=# set search_path to 'repmgr';
SET
# 在数据库中查看remgr集群的相关表
repmgr=# \d
List of relations
-[ RECORD 1 ]--------------
Schema | repmgr
Name   | events
Type   | table
Owner  | repmgr
-[ RECORD 2 ]--------------
Schema | repmgr
Name   | monitoring_history
Type   | table
Owner  | repmgr
-[ RECORD 3 ]--------------
Schema | repmgr
Name   | nodes
Type   | table
Owner  | repmgr
-[ RECORD 4 ]--------------
Schema | repmgr
Name   | replication_status
Type   | view
Owner  | repmgr
-[ RECORD 5 ]--------------
Schema | repmgr
Name   | show_nodes
Type   | view
Owner  | repmgr
-[ RECORD 6 ]--------------
Schema | repmgr
Name   | voting_term
Type   | table
Owner  | repmgr
# 在数据库中查看repmgr注册信息
repmgr=# select * from repmgr.nodes;
-[ RECORD 1 ]----+--------------------------------------------------------------------------
node_id          | 10
upstream_node_id |
active           | t
node_name        | repmgr01
type             | primary
location         | default
priority         | 100
conninfo         | host=192.168.111.10 port=5433 user=repmgr dbname=repmgr connect_timeout=2
repluser         | repmgr
slot_name        |
config_file      | /data/repmgr/repmgr.conf

11、备节点配置

节点二上配置repmgr.conf

su - postgres
vi /data/repmgr/repmgr.conf

node_id=11
node_name='repmgr02'
conninfo='host=192.168.111.11 port=5433 user=repmgr dbname=repmgr connect_timeout=2'
data_directory= '/data/pgsql/data'
replication_user= 'repmgr'
replication_type= 'physical'
repmgr_bindir= '/data/pgsql/install/bin'
pg_bindir= '/data/pgsql/install/bin'
monitoring_history=yes
monitor_interval_secs=5
log_level='info'
log_file='/data/repmgr/repmgr.log'
failover='automatic'
connection_check_type=ping
reconnect_attempts=3
reconnect_interval=10

增加执行权限

cd /data/repmgr
chmod +x *.sh

12、备节点克隆主节点数据

12.1 从节点1安装PostgreSQL后,安装Repmgr跟节点1的步骤相似,现在要删除节点二上的目录

[postgres@pgserver02 repmgr]$ pg_ctl stop
waiting for server to shut down....2023-12-27 17:11:19.835 CST [24556] LOG:  received fast shutdown request
2023-12-27 17:11:19.836 CST [24556] LOG:  aborting any active transactions
2023-12-27 17:11:19.838 CST [24556] LOG:  background worker "logical replication launcher" (PID 24563) exited with exit code 1
2023-12-27 17:11:19.838 CST [24558] LOG:  shutting down
2023-12-27 17:11:19.843 CST [24556] LOG:  database system is shut down
 done
server stopped

#删除数据文件
cd /data/pgsql/data
rm -rf *
#删除归档文件
cd /data/pgsql/archive
rm -rf *

12.2 --dry-run表示命令测试,并不会实际执行,可用于验证是否会出现一些基本错误

[postgres@pgserver02 archive]$ repmgr -h 192.168.111.10 -p5433 -U repmgr -d repmgr -f /data/repmgr/repmgr.conf standby clone --dry-run
NOTICE: destination directory "/data/pgsql/data" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.111.10 port=5433 user=repmgr dbname=repmgr
DETAIL: current installation size is 33 MB
INFO: "repmgr" extension is installed in database "repmgr"
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
INFO: parameter "max_wal_senders" set to 32
NOTICE: checking for available walsenders on the source node (2 required)
INFO: sufficient walsenders available on the source node
DETAIL: 2 required, 32 available
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: required number of replication connections could be made to the source server
DETAIL: 2 replication connections required
NOTICE: standby will attach to upstream node 10
HINT: consider using the -c/--fast-checkpoint option
INFO: would execute:
  /data/pgsql/install/bin/pg_basebackup -l "repmgr base backup"  -D /data/pgsql/data -h 192.168.111.10 -p 5433 -U repmgr -X stream
INFO: all prerequisites for "standby clone" are met

12.3 实际执行pg的克隆操作

[postgres@pgserver02 archive]$ repmgr -h 192.168.111.10 -p5433 -U repmgr -d repmgr -f /data/repmgr/repmgr.conf standby clone
NOTICE: destination directory "/data/pgsql/data" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.111.10 port=5433 user=repmgr dbname=repmgr
DETAIL: current installation size is 33 MB
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
NOTICE: checking for available walsenders on the source node (2 required)
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: checking and correcting permissions on existing directory "/data/pgsql/data"
NOTICE: starting backup (using pg_basebackup)...
HINT: this may take some time; consider using the -c/--fast-checkpoint option
INFO: executing:
  /data/pgsql/install/bin/pg_basebackup -l "repmgr base backup"  -D /data/pgsql/data -h 192.168.111.10 -p 5433 -U repmgr -X stream
WARNING:  skipping special file "./.s.PGSQL.5433"
WARNING:  skipping special file "./.s.PGSQL.5433"
NOTICE: standby clone (using pg_basebackup) complete
NOTICE: you can now start your PostgreSQL server
HINT: for example: pg_ctl -D /data/pgsql/data start
HINT: after starting the server, you need to register this standby with "repmgr standby register"

出现"after starting the server, you need to register this standby with “repmgr standby register” 说明克隆完成

12.4 克隆完成后修改配置文件

/data/pgsql/data/postgresql.conf
修改实例名称为备库的实例名称
cluster_name=‘repmgr02’
启动数据库
pg_ctl start

[postgres@pgserver02 data]$ pg_ctl start
waiting for server to start....2023-12-27 17:20:11.537 CST [40523] LOG:  00000: redirecting log output to logging collector process
2023-12-27 17:20:11.537 CST [40523] HINT:  Future log output will appear in directory "log".
2023-12-27 17:20:11.537 CST [40523] LOCATION:  SysLogger_Start, syslogger.c:674
 done
server started

12.5 注册本地服务为备库

[postgres@pgserver02 data]$ repmgr -f /data/repmgr/repmgr.conf standby register
INFO: connecting to local node "repmgr02" (ID: 11)
INFO: connecting to primary database
WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID: 10)
INFO: standby registration complete
NOTICE: standby node "repmgr02" (ID: 11) successfully registered

12.6 在主库上查看数据库集群状态

[postgres@pgserver01 ~]$ repmgr -f /data/repmgr/repmgr.conf cluster show
 ID | Name     | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
----+----------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------
 10 | repmgr01 | primary | * running |          | default  | 100      | 1        | host=192.168.111.10 port=5433 user=repmgr dbname=repmgr connect_timeout=2
 11 | repmgr02 | standby |   running | repmgr01 | default  | 100      | 1        | host=192.168.111.11 port=5433 user=repmgr dbname=repmgr connect_timeout=2

12.7 在备库使用主库的IP登陆验证

[postgres@pgserver02 data]$ psql  -h 192.168.111.10 -U repmgr
psql (14.7)
Type "help" for help.

repmgr=#

12.8 查看集群信息

[postgres@pgserver02 data]$ repmgr -f /data/repmgr/repmgr.conf cluster show
 ID | Name     | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string
----+----------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------
 10 | repmgr01 | primary | * running |          | default  | 100      | 1        | host=192.168.111.10 port=5433 user=repmgr dbname=repmgr connect_timeout=2
 11 | repmgr02 | standby |   running | repmgr01 | default  | 100      | 1        | host=192.168.111.11 port=5433 user=repmgr dbname=repmgr connect_timeout=2

13 至此postgreSQL主从部署完成。

  • 23
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL主从复制是一种常见的高可用性和数据备份解决方案。它可以确保在主节点故障时,从节点可以接管并继续提供服务。 要配置PostgreSQL主从复制,需要进行以下步骤: 1. 确保主节点和从节点上都已安装了PostgreSQL。 2. 在主节点上修改postgresql.conf文件,启用WAL(Write-Ahead Logging)并配置适当的参数。找到并修改以下参数: ``` wal_level = replica max_wal_senders = 10 wal_keep_segments = 10 ``` 这些参数将确保主节点生成WAL日志,并允许从节点连接并接收这些日志。 3. 在主节点上修改pg_hba.conf文件,允许从节点连接。找到并添加以下行: ``` host replication <从节点用户名> <从节点IP地址>/32 md5 ``` `<从节点用户名>`是从节点连接时使用的用户名,`<从节点IP地址>`是从节点的IP地址。 4. 在主节点上重启PostgreSQL服务以使更改生效。 5. 在从节点上创建一个空的数据目录。 6. 在从节点上创建一个recovery.conf文件,指定主节点的连接信息和复制模式。将以下内容添加到recovery.conf文件中: ``` standby_mode = 'on' primary_conninfo = 'host=<主节点IP地址> port=5432 user=<从节点用户名> password=<从节点密码>' trigger_file = '/path/to/trigger/file' ``` `<主节点IP地址>`是主节点的IP地址,`<从节点用户名>`和`<从节点密码>`是用于连接主节点的凭据。 7. 启动从节点上的PostgreSQL服务。 完成上述步骤后,主节点和从节点之间的复制将开始运行。你可以通过在主节点上执行一些写操作,然后在从节点上查看数据是否同步来验证复制是否成功。 请注意,以上步骤仅适用于基本的主从复制配置。在实际部署中,你可能还需要考虑故障转移、监控、自动故障恢复等方面的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值