REPMGR-PG高可用搭建(二)
REPMGR搭建步骤
一、介绍
repmgr是第二象限开源的一套流复制集群管理工具,用于管理PostgreSQL服务器群集中的复制和故障转移。
支持故障自动转移和手动切换;支持分布式管理集群节点,易扩展,可以在线增加或者删除节点。
架构图:
repmgr有repmgr和repmgrd两个管理命令:
repmgr:实现对集群节点的管理,比如注册主备节点,克隆节点,promote节点,follow节点,手动切换等
repmgrd:用来启动repmgr系统的守护进程,对集群节点的监控,可在主库故障时自动切换
二、环境搭建
2.1部署规划
主机名 | IP | OS | 角色 | 软件包 |
172-16-104-5 | 172.16.104.5 | Centos 7 | 主服务器primary | PG12.6、repmgr5.2 |
172-16-104-6 | 172.16.104.6 | Centos 7 | 备服务器standby | |
172-16-104-24 | 172.16.104.24 | Centos 7 | 见证服务器witness |
目录规划:
PG数据目录:/data/pgsql12/data
repmgr数据目录:/data/repmgr
PG软件目录:/usr/local/pgsql
repmgr软件目录:/usr/local/repmgr-5.2.1
2.2 PG与repmgr搭建
2.2.1PG安装
3个节点分别执行以下操作:
1.配置host文件
# cat /etc/hosts 172.16.104.5 172-16-104-5 172.16.104.6 172-16-104-6 172.16.104.24 172-16-104-24 2.上传pg并解压 安装上传工具 # yum install lrzsz 解压 # tar zxf postgresql-12.6.tar.gz -C /usr/local 3.安装依赖 # yum install gcc gcc-c++ readline-devel readline readline-dev zlib-devel 4.编译安装 # cd /usr/local/postgresql-12.6/ # ./configure --prefix=/usr/local/pgsql # make && make install 5.添加用户 # groupadd postgres # useradd -g postgres postgres # passwd postgres 6.配置ssh免密互信 --root用户下免密互信 # ssh-keygen # ssh-copy-id ${其他服务器IP} # ssh ${其他服务器IP} --postgres用户下免密互信 # su - postgres $ ssh-keygen $ ssh-copy-id ${其他服务器IP} $ ssh ${其他服务器IP} 5.配置数据、日志目录 # mkdir -p /data/pgsql12/{data,logs,archive} # chown -R postgres:postgres /data/pgsql12/ 6.配置环境变量 # vi /etc/profile export PGHOME=/usr/local/pgsql export PGDATA=/data/pgsql12/data export PATH=$PGHOME/bin:$PATH:/usr/local/repmgr-5.2.1 export LD_LIBRARY_PATH=/usr/local/pgsql/lib # source /etc/profile
配置postgres用户免密执行sudo(3节点都执行):
并不是所有用户都能执行sudo命令,需在/etc/sudoers文件中先进行配置
1.以root用户进入/etc用户,查看sudoers文件权限
2.修改文件权限为可编辑,编辑文件,添加postgres ALL=(ALL) NOPASSWD: ALL
3.保存退出编辑后,将sudoers文件权限再修改为只读
主节点对PG初始化
1.初始化数据库 # su - postgres $ initdb -D /data/pgsql12/data The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "zh_CN.UTF-8". The default database encoding has accordingly been set to "UTF8". initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8" The default text search configuration will be set to "simple". Data page checksums are disabled. fixing permissions on existing directory /data/pgsql12/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Asia/Shanghai creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /data/pgsql12/data -l logfile start 2.启动数据库 $ pg_ctl -D /data/pgsql12/data -l logfile start waiting for server to start.... done server started 3.修改用户密码 $ psql psql (12.6) Type "help" for help. postgres=# alter user postgres password '123'; ALTER ROLE postgres=# \q 修改PG数据目录下的pg_hba.conf文件: local all all md5 host all all 127.0.0.1/32 md5 修改后reload重新加载生效 $ pg_ctl reload server signaled 再次登陆提示需输入密码 $ psql Password for user postgres: 4.创建repmgr用户及数据库 $ psql Password for user postgres: psql (12.6) Type "help" for help. postgres=# create database repmgr; CREATE DATABASE postgres=# create user repmgr with password 'repmgr' superuser login; CREATE ROLE postgres=# alter database repmgr owner to repmgr; ALTER DATABASE