1.环境配置
ip |
虚拟ip |
版本 |
|
master |
192.168.20.71 |
192.168.20.99 |
postgresql v14.2 keepalived-1.3.5-19.el7.x86_64 |
slave |
192.168.20.75 |
postgresql v14.2 keepalived-1.3.5-19.el7.x86_64 |
2.安装步骤
安装postgresql
yum install -y postgresql14-server
以下命令在使用解压缩方式安装时执行,使用yum安装时不需要执行。
命令补充,如果使用解压缩方式安装,则需要创建postgres用户 并且安装postgresql过程中需要手动安装如下依赖工具 |
启动方法命令:
su - postgres
pg_ctl start
pg_ctl restart
pg_ctl stop
安装完以后,安装路径如下:
/var/lib/pgsql/14
进入postgres用户
su - postgres
通过vi ~/.bash_profile查看安装路径和PDATA的配置
[ -f /etc/profile ] && source /etc/profile
PATH=/usr/pgsql-14/bin:$PATH
export PATH
PGDATA=/var/lib/pgsql/14/data
export PGDATA
# If you want to customize your settings,
# Use the file below. This is not overridden
# by the RPMS.
[ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile
可以看到postgresql安装路径在/usr/pgsql-14/bin下,PGDATA在路径/var/lib/pgsql/14/data下。
创建数据库Keepalived,并且创建表探测表sr_delay,后续Keepalived探测,刷新sr_delay表的last_alive字段为当前探测时间。这张表用来判断主备延迟情况,数据库故障切换时会用到这张表。
数据库配置:
使用psql命令进入postgres数据库后台
-bash-4.2$ psql
psql (14.2)
Type "help" for help.
创建测试表test
postgres=# create table test(id int);
创建keepalived用户和数据库,用于监测主备机器数据库启动情况
postgres=# create user keepalived password 'sw12345' CONNECTION LIMIT 4 ;
CREATE ROLE
postgres=# create database keepalived owner keepalived;
CREATE DATABASE
postgres=# \c keepalived keepalived
You are now connected to database "keepalived" as user "keepalived".
keepalived=> create table sr_delay(id int4, last_alive timestamp(0) without time zone);
CREATE TABLE
表sr_delay只允许写入一条记录,并且不允许删除此表数据,通过触发器实现。创建触发器函数,如下所示:
CREATE FUNCTION cannt_delete ()
RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'Table sr_delay can not delete !';
END;
$$;
创建触发器:
CREATE TRIGGER trigger_sr_delay_del
BEFORE DELETE ON sr_delay
FOR EACH ROW EXECUTE PROCEDURE cannt_delete() ;
CREATE TRIGGER trigger_sr_delay_tru BEFORE TRUNCATE ON sr_delay FOR STATEMENT EXECUTE PROCEDURE cannt_delete() ;
<