架构说明:
架构 | 操作系统 | IP | 主机名 | PG版本 | 端口 | repmgr插件版本 | 磁盘空间 | 内存 | CPU |
---|---|---|---|---|---|---|---|---|---|
主从 | centos7 | 192.168.1.10、192.168.1.11 | pgserver01、pgserver02 | PostgreSQL 14.7 | 5433 | repmgr-5.3.1 | 50G | 4G | 2 |
备注:
pgsql主从先搭建单机环境再配置主从模式。
此处搭建不再说明,可看之前的单机搭建博客。
postgreSQL单机部署:https://blog.csdn.net/qq_41270538/article/details/131852097
只修改主库,因为备库后面会克隆主库的文件过来。
修改前备份上个版本的配置文件
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)
pg_ctl start
[root@pgserver01 ~]# vi /etc/sudoers
## 允许“postgres”用户组的成员运行所有命令,且运行时不需要输入密码
...
%postgres ALL=(ALL) NOPASSWD: ALL
...
[root@pgserver01 ~]# echo "test" |passwd --stdin postgres
更改用户 postgres 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@pgserver02 postgresql-14.7]# echo "test" |passwd --stdin postgres
更改用户 postgres 的密码 。
passwd:所有的身份验证令牌已经成功更新。
当前密码修改为 test
[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
[email protected]'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.
[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
两个节点都需要安装
[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目录下
如果没有用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)
创建 repmgr 用户,权限为 superuser
postgres=# CREATE USER repmgr WITH SUPERUSER PASSWORD 'test_2023';
CREATE ROLE
创建repmgr库
postgres=# create database repmgr owner repmgr;
CREATE DATABASE
[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
节点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
编辑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
[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
节点二上配置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
[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 *
[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
[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” 说明克隆完成
/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
[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
[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
[postgres@pgserver02 data]$ psql -h 192.168.111.10 -U repmgr
psql (14.7)
Type "help" for help.
repmgr=#
[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