postgresql 流复制切换

来源:互联网 发布:telnet在端口连接失败 编辑:程序博客网 时间:2024/06/08 11:15
主备端:alter system set wal_log_hints = on;alter system set max_replication_slots = 10;SELECT * FROM pg_create_physical_replication_slot('node_a_slot');select slot_name,slot_type,active,active_pid,restart_lsn from pg_replication_slots;vi $PGDATA/pg_hba.conf流复制的IP权限修改为同一网段,而不是仅指定一个IP地址。重启主备数据库。主端db1关闭数据库pg_ctl stop -m f 备端db2升为主库:pg_ctl promote -D $PGDATA查看db2是否升为主端:pg_controldata | grep clusterDatabase cluster state:               in production在db2插入测试数据:[postgres@hgdb1 ~]$ psqlpostgres=#  create table test_2(id int4);CREATE TABLEpostgres=# insert into test_2(id) select n from generate_series(1,10000) n;INSERT 0 10000在db1上运行pg_rewindpg_rewind --target-pgdata $PGDATA --source-server='host=DB2IP  port=5866 user=postgres dbname=postgres' -P The servers diverged at WAL position 0/1300CEB0 on timeline 5.Rewinding from last common checkpoint at 0/1200008C on timeline 5reading source file listreading target file listreading WAL in targetneed to copy 59 MB (total source directory size is 76 MB)61185/61185 kB (100%) copiedcreating backup label and updating control fileDone!备注:pg_rewind 成功。cd $PGDATAmv recovery.done recovery.confcat recovery.confstandby_mode = 'on'primary_conninfo = 'user=replica password=replica host=DB2IP  port=5866 sslmode=prefer sslcompression=1'primary_slot_name = 'node_a_slot'recovery_target_timeline = 'latest'pg_ctl startpsqlpostgres=# select count(*) from test_2; count ------- 10000(1 row)三、  pg_rewind原理     基本思想是将所有内容从新集群复制到旧集群,除了我们知道的块是一样的。    1)扫描旧集群的WAL日志,从新集群的时间轴历史记录从旧集群分出的点之前的最后一个检查点开始。       对于每个WAL记录,记下被触摸的数据块。这将产生一个列表,在新集群分支关闭后,旧集群中已   更改的所有数据块的列表。    2)将所有这些更改的块从新集群复制到旧集群。    3)将所有其他文件(如clog,conf文件等)从新集群复制到旧集群。除关系文件外的所有内容。    4)从在故障切换中创建的检查点开始,从新集群应用WAL。 (严格来说,pg_rewind不适用于WAL,它       只是创建一个备份标签文件,指示当PostgreSQL启动时,它将从该检查点开始重播并应用所有所需   的WAL)。