# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
sudo yum install -y postgresql14-server
# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
vim /var/lib/pgsql/14/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
vim /var/lib/pgsql/14/data/pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
host all all 0.0.0.0/0 md5
重启命令:systemctl restart postgresql-14
#下列了解不用操作
启动命令:systemctl start postgresql-14
停止命令:systemctl stop postgresql-14
[root@localhost ~]# su - postgres
-bash-4.2$ psql
psql (14.5)
输入 "help" 来获取帮助信息.
postgres=# ALTER USER postgres ENCRYPTED PASSWORD 'postgres';
ALTER ROLE
postgres=# \q #按ctrl+d即可退出(下同)
-bash-4.2$ logout
登出
#关闭防火墙
systemctl stop firewalld.service
#设置开机禁用
systemctl disable firewalld.service
至此postgresql安装完成,可以测试连接
主库 | 从库1 | 从库2 |
---|---|---|
192.20.48.146 | 192.20.48.147 | 192.20.48.148 |
主库
参数文件 postgresql.conf
(这里我们只修改一些必须的参数,非生产环境最优化的设置)vim /var/lib/pgsql/14/data/postgresql.conf
wal_level = replica # minimal, replica, or logical
# (change requires restart)
repl
[root@localhost ~]# su - postgres
上一次登录:一 4月 17 10:37:34 CST 2023pts/0 上
-bash-4.2$ psql
psql (14.5)
输入 "help" 来获取帮助信息.
postgres=# create role repl login replication encrypted password 'repl';
CREATE ROLE
postgres=# \q
-bash-4.2$ logout
登出
vim /var/lib/pgsql/14/data/pg_hba.conf
#ip是两个从节点的ip
host replication repl 192.20.48.147/32 md5
host replication repl 192.20.48.148/32 md5
systemctl restart postgresql-14
systemctl stop postgresql-14
data
[root@localhost ~]# cd /var/lib/pgsql/14
[root@localhost 14]# rm -rf data
注意此处同步的ip是主库的ip
[root@localhost 14]# pg_basebackup -h 192.20.48.146 -p 5432 -U repl -R -F p -P -D data
参数说明:
选项 说明
-p 主库数据库端口
-U 流复制用户
-W 使用密码验证,要用replica的密码
-Fp 备份输出正常的数据库目录
-Xs 使用流复制的方式进行复制
-Pv 输出复制过程的详细信息
-R 为备库创建recovery.conf文件。但是pgsql 10以后的新版本的pgsql不需要这个文件了。
-D 指定创建的备库的数据库目录
[root@localhost 14]# chown -R postgres:postgres /var/lib/pgsql/14/data
[root@localhost 14]# systemctl start postgresql-14
至此简单的主从流复制配置完成,可连接数据库进行操作进行检验,从库为只读库,可以在主库执行下列命令查询从库相关信息
select pid,state,client_addr,sync_priority,sync_state,sent_lsn,write_lsn from pg_stat_replication;
/etc/hosts
,每台节点配置 ip 及别名的对应关系,添加映射关系vim /etc/hosts
192.20.48.146 node1
192.20.48.147 node2
192.20.48.148 node3
#切换到root用户下
su root
#输入visudo指令
visudo
#在文件中新增
postgres ALL=(ALL) NOPASSWD:ALL
[root@node1 ~]# passwd postgres
更改用户 postgres 的密码 。
新的 密码:
无效的密码: 密码包含用户名在某些地方
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
su postgres # 切换到 postgres 用户
ssh-keygen # 生成密钥-公钥对
ssh-copy-id node1 # 将公钥保存到 node1 节点
ssh-copy-id node2 # 将公钥保存到 node2 节点
ssh-copy-id node3 # 将公钥保存到 node3 节点
修改 ssh 配置文件:etc/ssh/sshd_config
vim etc/ssh/sshd_config
#开启或修改为yes
PubkeyAuthentication yes
文件夹需归属于 postgres 用户,且 ssh 对文件夹权限有要求
chown -R postgres:postgres /var/lib/pgsql
chmod 700 /var/lib/pgsql/.ssh
chmod 600 /var/lib/pgsql/.ssh/authorized_keys
原因: .ssh 目录没有 ssh_home_t 标签,通过下列命令重置
restorecon -r -vv /var/lib/pgsql/.ssh
systemctl restart sshd
#切换至 postgres 用户
su - postgres
#进行远程连接测试
ssh node1
ssh node2
ssh node3
curl https://dl.2ndquadrant.com/default/release/get/14/rpm | sudo bash
yum install repmgr14
yum install -y rsync
vim /etc/profile
#最后一行添加
export PATH=/usr/pgsql-14/bin:$PATH
#重新加载配置文件
source /etc/profile
/var/lib/pgsql/14/data/postgresql.conf
,将下述属性进行配置vim /var/lib/pgsql/14/data/postgresql.conf
listen_addresses = '*'
max_wal_senders = 10
max_replication_slots = 10
wal_level = hot_standby
hot_standby = on
archive_mode = always
archive_command = '/bin/true'
shared_preload_libraries = 'repmgr'
修改配置文件/var/lib/pgsql/14/data/pg_hba.conf
vim /var/lib/pgsql/14/data/pg_hba.conf
local all postgres peer
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr 192.20.48.0/24 trust
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host repmgr repmgr 192.20.48.0/24 trust
#重启数据库
systemctl restart postgresql-14
/etc/repmgr/14/repmgr.conf
,在末尾处添加vim /etc/repmgr/14/repmgr.conf
node_id=1
node_name='node1'
conninfo='host=node1 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr'
data_directory='/var/lib/pgsql/14/data'
failover=automatic
promote_command='/usr/pgsql-14/bin/repmgr standby promote -f /etc/repmgr/14/repmgr.conf --log-to-file'
follow_command='/usr/pgsql-14/bin/repmgr standby follow -f /etc/repmgr/14/repmgr.conf --log-to-file --upstream-node-id=%n'
service_start_command = 'sudo systemctl start postgresql-14'
service_stop_command = 'sudo systemctl stop postgresql-14'
service_restart_command = 'sudo systemctl restart postgresql-14'
service_reload_command = 'sudo systemctl reload postgresql-14'
repmgrd_pid_file='/tmp/repmgrd.pid'
log_file='/tmp/repmgrd.log'
priority=100
su - postgers
createuser -s repmgr
createdb repmgr -O repmgr
[root@localhost ~]# su - postgres
-bash-4.2$ psql
psql (14.5)
输入 "help" 来获取帮助信息.
postgres=# ALTER USER repmgr ENCRYPTED PASSWORD 'repmgr';
ALTER ROLE
postgres=# \q
-bash-4.2$ exit
登出
su - postgres
#注册主节点
-bash-4.2$ repmgr primary register
#查看主节点
-bash-4.2$ repmgr cluster show
若未配置环境变量,需到对应文件夹下执行相关操作
-bash-4.2$ cd /usr/pgsql-14/bin/
-bash-4.2$ ./repmgr -f /etc/repmgr/14/repmgr.conf primary register
注册完成后主机配置结束,可以查看注册结果
-bash-4.2$ ./repmgr cluster show -f /etc/repmgr/14/repmgr.conf
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+------------------------------------------------------------------------
1 | node1 | primary | * running | | default | 100 | 1 | host=node1 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr
至此主节点注册完成
/var/lib/pgsql/14/data
)[root@node3 ~]# su - postgres
上一次登录:一 4月 17 11:37:48 CST 2023pts/0 上-bash-4.2$ psql 'host=node1 user=repmgr dbname=repmgr connect_timeout=2'
#密码为repmgr
用户 repmgr 的口令:
psql (14.5)
输入 "help" 来获取帮助信息.
repmgr=#
/etc/repmgr/14/repmgr.conf
,在末尾处添加vim /etc/repmgr/14/repmgr.conf
node_id=3
node_name='node3'
conninfo='host=node3 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr'
data_directory='/var/lib/pgsql/14/data'
failover=automatic
promote_command='/usr/pgsql-14/bin/repmgr standby promote -f /etc/repmgr/14/repmgr.conf --log-to-file'
follow_command='/usr/pgsql-14/bin/repmgr standby follow -f /etc/repmgr/14/repmgr.conf --log-to-file --upstream-node-id=%n'
service_start_command = 'sudo systemctl start postgresql-14'
service_stop_command = 'sudo systemctl stop postgresql-14'
service_restart_command = 'sudo systemctl restart postgresql-14'
service_reload_command = 'sudo systemctl reload postgresql-14'
repmgrd_pid_file='/tmp/repmgrd.pid'
log_file='/tmp/repmgrd.log'
priority=100
--dry-run
参数尝试克隆服务器,并进行克隆cd /usr/pgsql-14/bin/
./repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr/14/repmgr.conf standby clone --dry-run
报错信息提示未提供密码
-bash-4.2$ cd /usr/pgsql-14/bin/
-bash-4.2$ ./repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr/14/repmgr.conf standby clone --dry-run
NOTICE: destination directory "/var/lib/pgsql/14/data" provided
INFO: connecting to source node
DETAIL: connection string is: host=node1 user=repmgr dbname=repmgr
ERROR: connection to database failed
DETAIL:
connection to server at "node1" (192.20.48.146), port 5432 failed: fe_sendauth: no password supplied
解决方法:在对应的家目录下新建 .pgpass 文件配置密码
cd ~
touch .pgpass
vim ~/.pgpass
#在新建的文件中写入配置
#hostname:port:database:username:password
node1:5432:repmgr:repmgr:repmgr
#文件权限设置
chmod 0600 ~/.pgpass
#在 repmgr 配置文件中新增密码文件的路径配置
vim /etc/repmgr/14/repmgr.conf
passfile='/var/lib/pgsql/.pgpass'
重新进行尝试
cd /usr/pgsql-14/bin/
./repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr/14/repmgr.conf standby clone --dry-run
测试无异常报错,则可实际执行克隆操作
./repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr/14/repmgr.conf standby clone
启动 pg 数据库服务
sudo systemctl start postgresql-14
以 standby 的身份注册集群
./repmgr -f /etc/repmgr/14/repmgr.conf standby register
分别按上述流程对 node2 和 node3 进行集群注册,注册成功后,检查集群状态
-bash-4.2$ repmgr cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+------------------------------------------------------------------------
1 | node1 | primary | * running | | default | 100 | 1 | host=node1 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr
2 | node2 | standby | running | node1 | default | 100 | 1 | host=node2 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr
3 | node3 | standby | running | node1 | default | 100 | 1 | host=node3 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr
对主库进行数据库操作,查看到从库实现数据同步
repmgrd 是一个守护进程,它主动监视复制集群中的服务器并支持以下任务:
分别在三个节点处启动守护进程 repmgrd
su - postgres
cd /usr/pgsql-14/bin/
./repmgrd -f /etc/repmgr/14/repmgr.conf