主机:172.21.98.130
从机:172.21.98.131
前面安装按照安装手册实行,并启动主数据服务,从数据库安装好不启动;
在主机上执行;
/var/lib/pgsql/12/data/
修改postgresql.conf
wal_level = hot_standby # 这个是设置主为wal的主机
max_wal_senders = 5 # 这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
wal_keep_segments = 128 # 设置流复制保留的最多的xlog数目
wal_sender_timeout = 60s # 设置流复制主机发送数据的超时时间
max_connections = 200 # 一般查多于写的应用从库的最大连接数要比较大
hot_standby = on # 说明这台机器不仅仅是用于数据归档,也用于数据查询
max_standby_streaming_delay = 30s # 数据流备份的最大延迟时间
wal_receiver_status_interval = 10s # 多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on # 如果有错误的数据复制,是否向主进行反馈
wal_log_hints = on # also do full page writes of non-critical updates
/var/lib/pgsql/12/data/
修改pg_hba.conf
local all all trust
host all all 0.0.0.0/0 md5
host all all ::1/128 trust
host replication replica 0.0.0.0/0 md5
添加流复制用户
create role replica with replication login password ‘123456’;
alter user replica with password ‘123456’;
从机执行;
pg_basebackup -h 172.21.98.201 -p 5432 -U replica -W -Fp -Xs -Pv -R -D /data/pgsql/12/data
设置data权限
chown -R postgres:postgres /data/pgsql/*
启动
systemctl enable postgresql-12
systemctl start postgresql-12
5.查看状态
查看主/从库状态S
/usr/pgsql-12/bin/pg_controldata -D /var/lib/pgsql/12/data
/usr/pgsql-12/bin/pg_controldata -D /var/lib/pgsql/12/data/ | grep ‘Database cluster state’
#主库显示:Database cluster state: in production
#从库显示:Database cluster state: in archive recovery
6、检查异步流复制的情况:
可以使用主库上的视图pg_stat_replication查看流复制的信息:
postgres=# select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;
pid | state | client_addr | sync_priority |sync_state
------±----------±------------±--------------±-----------
9552 | streaming | 10.0.2.61 | 0 | async
(1 row)
查看备库落后主库多少字节的WAL日志:
postgres=# select pg_xlog_location_diff(pg_current_xlog_location(),replay_location)from pg_stat_replication; pg_xlog_location_diff
0(1 row)
7、查看备库状态: 如何判断数据库处于备库状态: 1)hotstandby 状态: 可以连接数据库执行pg_is_in_recovery()函数,主库上返回False,standby上返回true; 主库: postgres=# select pg_is_in_recovery();
f (1 row) standby库: postgres=# select pg_is_in_recovery();
t
(1 row)
6.主从切换
主库停止服务
systemctl stop postgresql-12
从库执行
su postgres
sudo -i -u postgres /usr/pgsql-12/bin/pg_ctl promote -D /data/pgsql/12/data/
#waiting for server to promote… done
#server promoted
原主库添加添加standby.signal
sudo -i -u postgres touch /data/pgsql/12/data/standby.signal
原主库重启
systemctl restart postgresql-12
全备命令
pg_basebackup -h localhost -D /var/lib/pgsql/12/backups/ -Ft -R -z -v --checkpoint=fast
keepalived应用
/etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
reboot 重启机器
设置postgresql-12开机不自启
systemctl disable postgresql-12
修改postgresql.conf
wal_level = hot_standby # 这个是设置主为wal的主机
max_wal_senders = 5 # 这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
wal_keep_segments = 128 # 设置流复制保留的最多的xlog数目
wal_sender_timeout = 60s # 设置流复制主机发送数据的超时时间
max_connections = 200 # 一般查多于写的应用从库的最大连接数要比较大
hot_standby = on # 说明这台机器不仅仅是用于数据归档,也用于数据查询
max_standby_streaming_delay = 30s # 数据流备份的最大延迟时间
wal_receiver_status_interval = 10s # 多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on # 如果有错误的数据复制,是否向主进行反馈
wal_log_hints = on # also do full page writes of non-critical updates
安装keepalived
yum install keepalived -y
安装nmap
yum install nmap -y
设置不抢占
配置/etc/keepalived/keepalived.conf
! Configuration File for keepalived \注释信息
global_defs {
router_id Coordinate_1
script_user root
enable_script_security
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 93
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.21.98.130
}
notify_master “/etc/keepalived/notify.sh master”
notify_backup “/etc/keepalived/notify.sh backup”
notify_fault “/etc/keepalived/notify.sh fault”
}
vrrp_script chk_recovery {
script “/etc/keepalived/check_recovery.sh”
interval 10
weight -5
}
vrrp_instance VI_2 {
state MASTER
interface ens192
virtual_router_id 94
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.21.98.131
}
track_script {
chk_recovery
}
}
创建/etc/keepalived/Coordinate.sh
#!/bin/bash
#定时Coordinate端口
CN_OK=1
cn_num=nmap 127.0.0.1 -p 5432|sed -n "6p"|grep open|wc -l
function check_cn_helth (){
if [ $cn_num -lt 1 ]
then
CN_OK=0
else
CN_OK=1
fi
return $CN_OK
}
check_cn_helth
if [ $CN_OK -eq 0 ]
then
exit 1
fi
创建/etc/keepalived/notify.sh
#!/bin/bash
#检查服务是否启动
CN_Svr=1
cn_num=service postgresql-12 status | grep "running"|grep running|wc -l
if [ $cn_num -eq “0” ]
then
CN_Svr=0
fi
case $1 in
master){
#检查服务是否启动
svc=“postgresql-12”
if [ $CN_Svr -eq “1” ]
then
#判断是否在备机模式
if [ -f “/data/pgsql/12/data/standby.signal” ];then
#切换为主机模式
sudo -i -u postgres /usr/pgsql-12/bin/pg_ctl promote -D /data/pgsql/12/data/
fi
else
if [ -f “/data/pgsql/12/data/standby.signal” ];then
#删除备文件
sudo -i -u postgres rm -f /data/pgsql/12/data/standby.signal
fi
#启动服务
systemctl start postgresql-12
fi
}
;;
*){
if [ ! -f “/data/pgsql/12/data/recovery.done” ];then
#创建备文件
sudo -i -u postgres touch /data/pgsql/12/data/standby.signal
fi
#检查服务是否启动
svc=“postgresql-12”
if [ $CN_Svr -eq “1” ]
then
#重启服务
echo “重启服务”
systemctl restart postgresql-12
else
#启动服务
echo “启动服务”
systemctl start postgresql-12
fi
}
;;
esac
创建/etc/keepalived/check_recovery.sh
#!/bin/bash
cn_num=sudo -i -u postgres psql -c "select pg_is_in_recovery();" | grep "t"|grep t|wc -l
if [ $cn_num -eq “1” ]
then
exit 0
else
exit 1
fi
修改权限
chmod +x /etc/keepalived/*
设置自启动
systemctl enable keepalived
查看keepalived工作情况
yum install tcpdump -y
tcpdump -i ens192 vrrp -n