xiongzhibiao


1.环境说明

master:192.168.1.231
slave:192.168.1.232

官方文档
本文参考

2.主服务配置

1.创建wal日志存放路径

mkdir -p /opt/pgsql/pg_archive/

2.创建用户用于复制

CREATE ROLE replica login replication encrypted password \'replica\';

3.配置授权replica用户

vi /etc/postgresql/9.6/main/pg_hba.conf 
host    replication      replica            192.168.1.232/24               md5

4.配置master

listen_addresses = \'*\'
archive_mode = on
#打开wal
archive_command = \'test ! -f /opt/pgsql/pg_archive/%f && cp %p /opt/pgsql/pg_archive/%f\'
#wal备份命令,测试是否存在然后写入
wal_level = hot_standby
#wal等级
max_wal_senders = 32
wal_sender_timeout = 60s
max_connections = 100

5.重启数据库

service postgresql restart              #重启

3.从服务器

1.停止从服务器

service postgresql stop

2.从master拷贝同步初始数据

rm -rf /var/lib/postgresql/9.6/main/*
#删除本地
pg_basebackup -h 192.168.1.231 -U replica -D /var/lib/postgresql/9.6/main -X stream -P
#同步到从服务器

3.配置recovery

cp /usr/share/postgresql/9.6/recovery.conf.sample /var/lib/postgresql/9.6/main/recovery.conf
vi /var/lib/postgresql/9.6/main/recovery.conf 

standby_mode = on
#代表为备节点
primary_conninfo = \'host=192.168.1.231 port=5432 user=replica password=replica\'
recovery_target_timeline = \'latest\'

4.配置从服务器的wal

vi /etc/postgresql/9.6/main/postgresql.conf

wal_level = hot_standby
max_connections = 1000
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
#多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on
#如果有错误的数据复制,是否向主进行反馈

5.创建归档目录

mkdir -p /opt/postgres/archive/

6.启动postgresql

service postgresql start

7.查看同步状态--在master上执行

select client_addr,sync_state from pg_stat_replication;
  client_addr  | sync_state 
---------------+------------
 192.168.1.232 | async
(1 row)

4.如何监控主从同步信息i

参考

1.手动命令监控

1.查看差异的字节

postgres=# \x
扩展显示已打开。
postgres=# SELECT client_hostname,
       client_addr, 
       pg_xlog_location_diff(sent_location, replay_location) AS difference_in_bytes
FROM pg_stat_replication;
-[ RECORD 1 ]-------+--------------
client_hostname     | 
client_addr         | 192.168.1.201
difference_in_bytes | 0

postgres=# 
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 164108
usesysid         | 4923193
usename          | repuser
application_name | walreceiver
client_addr      | 192.168.1.201
client_hostname  | 
client_port      | 50252
backend_start    | 2017-06-26 10:18:14.573118+08
backend_xmin     | 18683256
state            | streaming
sent_location    | D8/748AE0F8
write_location   | D8/748AE0F8
flush_location   | D8/748AE0F8
replay_location  | D8/748AE0F8
sync_priority    | 0
sync_state       | async

2.脚本监控

#!/bin/bash
DB=\'\'
DB_HOST=\'\'
DB_PORT=\'\'
DB_USER=\'\'

result=`psql -U${DB_USER} -h ${DB_HOST} -d ${DB} -p ${DB_PORT} <<EOF
SELECT client_hostname,
       client_addr, 
       pg_xlog_location_diff(sent_location, replay_location) AS difference_in_bytes
FROM pg_stat_replication;
EOF
`
delay=`echo ${result}|awk --lint=false -F \'[\|(]\' \'{print $5}\' 2>/dev/null`

if [ ! -n ${delay} ];then
        echo "can\'t get postgresql replication info."
        exit ${STATE_UNKNOWN}
fi

if [ ${delay} -lt ${WARNING_SIZE} ];then
        echo "postgresql replication is ok."
        exit ${STATE_OK}
elif [ ${delay} -ge ${WARNING_SIZE} -a ${delay} -le ${CRITICAL_SIZE} ];then
        echo "warning postgresl delay ${delay} bytes"
        exit ${STATE_WARNING}
else:
        echo "critical postgresl delay ${delay} bytes"
        exit ${STATE_CRITICAL}
fi

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2019-09-27
  • 2019-10-10
  • 2021-09-22
  • 2021-04-27
猜你喜欢
  • 2021-12-17
  • 2021-12-26
  • 2021-12-26
  • 2021-12-26
  • 2021-12-26
  • 2021-12-26
  • 2021-12-14
相关资源
相似解决方案