码迷,mamicode.com
首页 > 其他好文 > 详细

pg流复制_一主多备

时间:2021-02-06 12:14:28      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:list   ica   listening   数据   tor   future   gcc   conf   生效   

PG可以一主多备,目前pg93s是主,pg93和pg93s2是备节点,在主上查询:

postgres=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93pri          | 192.168.150.130 | streaming | async      |             0
 repuser | pg93std2         | 192.168.150.134 | streaming | async      |             0
(2 rows)
postgres=# select name,setting from pg_settings where name =‘synchronous_standby_names‘;
           name            | setting 
---------------------------+---------
 synchronous_standby_names | 
(1 row)
  • 基于优先级的同步备库:

在主库上配置synchronous_standby_names

--主上postgresql.conf配置参数
synchronous_standby_names = ‘first 1 (pg93pri,pg93std2)‘
--reload生效
postgres=# show synchronous_standby_names;
 synchronous_standby_names  
----------------------------
 first 1 (pg93pri,pg93std2)
(1 row)
--可见sync_state和sync_priority发生了变化,potential为潜在同步备库
--主alert日志
2021-02-01 19:15:44.211 CST [4530] LOG:  standby "pg93pri" is now a synchronous standby with priority 1
2021-02-01 19:23:24.703 CST [4617] LOG:  standby "pg93std2" is now a synchronous standby with priority 2
--sql query
postgres=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93pri          | 192.168.150.130 | streaming | sync       |             1
 repuser | pg93std2         | 192.168.150.134 | streaming | potential  |             2
(2 rows)

--关闭节点pg93pri后,pg93std2变成了sync
postgres=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93std2         | 192.168.150.134 | streaming | sync       |             2
(1 row)

--停掉最后一个备节点后,主上执行操作会挂起
[postgres@pg93s2 ~]$ pg_ctl stop -D /pgdata
waiting for server to shut down.... done
server stopped
--hanging...
findb=# drop table delay ;

--启动一个节点后,挂起的操作恢复正常
[postgres@pg93 ~]$ pg_ctl start -D /pgdata
waiting for server to start....2021-02-01 19:25:42.630 CST [2922] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), 64-bit
2021-02-01 19:25:42.630 CST [2922] LOG:  listening on IPv4 address "192.168.150.130", port 6000
2021-02-01 19:25:42.638 CST [2922] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.6000"
2021-02-01 19:25:42.657 CST [2922] LOG:  redirecting log output to logging collector process
2021-02-01 19:25:42.657 CST [2922] HINT:  Future log output will appear in directory "log".
 done
server started
findb=# drop table delay ;
DROP TABLE
findb=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93pri          | 192.168.150.130 | streaming | sync       |             1
(1 row)

 

  • Quorum同步

--主配置参数
synchronous_standby_names = ‘any 1 (pg93pri,pg93std2)‘
--reload生效
postgres=# select pg_reload_conf();
 pg_reload_conf 
----------------
 t
(1 row)

postgres=# show synchronous_standby_names;
 synchronous_standby_names 
---------------------------
 any 1 (pg93pri,pg93std2)
(1 row)
--sync_state为quorum
postgres=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93pri          | 192.168.150.130 | streaming | quorum     |             1
 repuser | pg93std2         | 192.168.150.134 | streaming | quorum     |             1
(2 rows)
--与first优先级别的区别是,只需要指定数量的备(目前指定为1)接收到日志并定入WAL后即可通知主提交成功,下为测试
--停节点pg93pri后主可继续操作
findb=# create table quorum_test as select * from pg_class ;
SELECT 427
findb=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93std2         | 192.168.150.134 | streaming | quorum     |             1
(1 row)
--启动pg93pri并关闭pg93std2,主可继续正常操作
findb=# drop table quorum_test;
DROP TABLE
findb=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93pri          | 192.168.150.130 | streaming | quorum     |             1
(1 row)
--两个备节点都关闭后,主上的操作将会挂起

 

  • 级联复制

--配置方法和普通异步流复制并没有区别,区别在于下游节点的primary_connec_info连接的是级联备节点
--主查看级联备节点:
postgres@postgres:select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | walreceiver      | 192.168.150.132 | streaming | async      |             0
(1 row)
--级联备节点查看下游节点
postgres=# select usename,application_name,client_addr,state,sync_state,sync_priority from pg_stat_replication;
 usename | application_name |   client_addr   |   state   | sync_state | sync_priority 
---------+------------------+-----------------+-----------+------------+---------------
 repuser | pg93std2         | 192.168.150.134 | streaming | async      |             0
(1 row)

--主创建表测试
postgres@findb:create table cascade_test as select * from pg_class ;
SELECT 427
postgres@findb:select count(*) from cascade_test;
 count 
-------
   427
(1 row)
--级联备查看数据
findb=# select count(*) from cascade_test;
 count 
-------
   427
(1 row)
--级联下游节点查看数据
fin_er@findb:select count(*) from cascade_test;
ERROR:  relation "cascade_test" does not exist
LINE 1: select count(*) from cascade_test;
                             ^
fin_er@findb:select count(*) from cascade_test;
 count 
-------
   427
(1 row)

pg流复制_一主多备

标签:list   ica   listening   数据   tor   future   gcc   conf   生效   

原文地址:https://www.cnblogs.com/leadx/p/14380303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!