导读

使用PostgreSQL 11.3 创建两个节点:node1 和 node2; 配置主从流复制,然后做手动切换(failover)。为了配置过程简单,两个节点在同一台物理机器上。

首先建立主从同步流复制,起初,node1作为主节点(Primary),node2作为从节点(Standby)。

接下来,模拟主节点(node1)无法工作,把从节点(node2)手动切换成主节点。然后把原来的主节点(node1)作为从节点加入系统。 此时,node2是主 (Primary), node1是从(Standby)。

最后,模拟主节点(node2)无法工作,把从节点(node1)手动切换成主节点。然后把node2作为从节点加入系统。 此时,node1是主 (Primary), node2是从(Standby)。

创建node1和node2,配置主从流复制

上级目录是:

/Users/tom

两个子目录,testdb113,testdb113b, 分别属于两个节点, node1, node2:

testdb113 --> node1

testdb113b --> node2

创建主节点(Primary) node1

mkdir testdb113

initdb -D ./testdb113

pg_ctl -D ./testdb113 start

node1: 创建数据库账号,用于流复制,创建账号 'replicauser',该账号存在于node1和node2 。

CREATE ROLE replicauser WITH REPLICATION LOGIN ENCRYPTED PASSWORD 'abcdtest';

node1: 创建归档目录

mkdir ./testdb113/archive

node1: 在配置文件中(postgresql.conf)设定参数

#for the primary

wal_level = replica

synchronous_commit = remote_apply

archive_mode = on

archive_command = 'cp %p /Users/tom/testdb113/archive/%f'

max_wal_senders = 10

wal_keep_segments = 10

synchronous_standby_names = 'pgslave001'

#for the standby

hot_standby = on

node1: 编辑文件pg_hba.conf,设置合法地址:

add following to then end of pg_hba.conf

host replication replicauser 127.0.0.1/32 md5

host replication replicauser ::1/128 md5\

node1: 预先编辑文件:recovery.done

注意: 该文件后来才会用到。当node1成为从节点时候,需要把recovery.done重命名为recovery.conf

standby_mode = 'on'

recovery_target_timeline = 'latest'

primary_conninfo = 'host=localhost port=6432 user=replicauser password=abcdtest application_name=pgslave001'

restore_command = 'cp /Users/tom/testdb113b/archive/%f %p'

trigger_file = '/tmp/postgresql.trigger.5432'

创建和配置从节点(node2)

mkdir testdb113b

pg_basebackup -D ./testdb113b

chmod -R 700 ./testdb113b

node2: 编辑文件postgresql.conf,设定参数,这个文件来自node1(由于使用了pg_basebackup),只需要更改其中的个别参数。

#for the primary

port = 6432

wal_level = replica

synchronous_commit = remote_apply

archive_mode = on

archive_command = 'cp %p /Users/tom/testdb113b/archive/%f'

max_wal_senders = 2

wal_keep_segments = 10

synchronous_standby_names = 'pgslave001'

#for the standby

hot_standby = on

node2: 建立归档目录,确保归档目录存在

mkdir ./testdb113b/archive

node2: 检查/编辑文件pg_hba.conf,这个文件来自node1,不需要编辑

node2: 创建/编辑文件recovery.conf

PG启动时,如果文件recovery.conf存在,则工作在recovery模式,当作从节点。如果当前节点升级成主节点,文件recovery.conf将会被重命名为recovery.done。

standby_mode = 'on'

recovery_target_timeline = 'latest'

primary_conninfo = 'host=localhost port=5432 user=replicauser password=abcdtest application_name=pgslave001'

restore_command = 'cp /Users/tom/testdb113/archive/%f %p'

trigger_file = '/tmp/postgresql.trigger.6432'

简单测试

重新启动主节点(node1):

pg_ctl -D ./testdb113 restart

启动从节点(node2)

pg_ctl -D ./testdb113b start

在node1上做数据更改,node1支持数据的写和读

psql -d postgres

postgres=# create table test ( id int, name varchar(100));

CREATE TABLE

postgres=# insert into test values(1,'1');

INSERT 0 1

节点node2读数据,node2是从节点(Standby),只能读,不能写。

psql -d postgres -p 6432

postgres=# select * from test;

id | name

----+------

1 | 1

(1 row)

postgres=# insert into test values(1,'1');

ERROR: cannot execute INSERT in a read-only transaction

postgres=#

模拟主节点node1不能工作时,把从节点node2提升到主节点

停止主节node1,node1停止后,只有node2工作,仅仅支持数据读。

pg_ctl -D ./testdb113 stop

尝试连接到节点node1时失败

因为node1停止了。

Ruis-MacBook-Air:tom$ psql -d postgres

psql: could not connect to server: No such file or directory

Is the server running locally and accepting

connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

尝试连接到node2,成功;插入数据到node2,失败

node2在工作,但是只读

Ruis-MacBook-Air:~ tom$ psql -d postgres -p 6432

psql (11.3)

Type "help" for help.

postgres=# insert into test values(1,'1');

ERROR: cannot execute INSERT in a read-only transaction

postgres=#

node2: 把node2提升到主

提升后,node2成为了主(Primary),既能读,也能写。

touch /tmp/postgresql.trigger.6432

node2: 插入数据到node2,阻塞等待

node2是主节点(Primary),能够成功插入数据。但是由于设置了同步流复制,node2必须等待某个从节点把数据更改应用到从节点,然后返回相应的LSN到主节点。

postgres=# insert into test values(2,'2');

node1: 创建文件recovery.conf

使用之前建立的文件recovery.done作为模版,快速创建recovery.conf。

mv recovery.done recovery.conf

node1: 启动node1,作为从节点(Standby)

启动后,系统中又有两个节点:node2是主,node1是从。

pg_ctl -D ./testdb113 start

插入数据到node2(见3.3)成功返回

由于节点node1作为从节点加入系统,应用主节点的数据更改,然后返回相应WAL的LSN到主节点,使主节点等到了回应,从而事务处理得以继续。

postgres=# insert into test values(2,'2');

INSERT 0 1

postgres=#

分别在node1和node2读取数据

由于两个节点都只是数据读,而且流复制正常工作,所有读取到相同的结果

ostgres=# select * from test;

id | name

----+------

1 | 1

2 | 2

(2 rows)

模拟node2无法工作时,提升node1成为主(Primary)

停止node2

pg_ctl -D ./testdb113b stop

尝试访问node2,失败

因为node2已经停止了.

Ruis-MacBook-Air:~ tom$ psql -d postgres -p 6432

psql: could not connect to server: No such file or directory

Is the server running locally and accepting

connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

node1: 连接到node1,成功;插入数据到node1,失败

node1只读

Ruis-MacBook-Air:~ tom$ psql -d postgres

psql (11.3)

Type "help" for help.

postgres=# insert into test values(3,'3');

ERROR: cannot execute INSERT in a read-only transaction

postgres=#

node1: 提升node1,成为主(Primary)

touch /tmp/postgresql.trigger.5432

node1: 插入数据到node1,阻塞等待

node1成功插入数据,但是要等待从节点的 remote-apply.

postgres=# insert into test values(3,'3');

node2: 创建文件recovery.conf

mv recovery.done recovery.conf

node2: 启动node2,作为从节点(Standby)

pg_ctl -D ./testdb113b start

node1: 插入数据(见4.3)成功返回。

postgres=# insert into test values(3,'3');

INSERT 0 1

分别在node1和node2读取数据

由于两个节点都只是数据读,而且流复制正常工作,所有读取到相同的结果

postgres=# select * from test;

id | name

----+------

1 | 1

2 | 2

3 | 3

(3 rows)

自动化解决方案

当主节点不能工作时,需要把一个从节点提升成为主节点。 如果是手工方式提升,可以使用trigger文件,也可以使用命令'pg_ctl promote' 。

目前也有很多种自动化监控和切换的方案,在网上都能搜索到。或者,可以自己动手写一个自动化监控和切换的方案。

postgresql主从备份_PostgreSQL主从流复制与手动主备切换架构相关推荐

  1. pgpool mysql_PGPool-II+PG流复制实现HA主备切换

    基于PG的流复制能实现热备切换,但是是要手动建立触发文件实现,对于一些HA场景来说,需要当主机down了后,备机自动切换,经查询资料知道pgpool-II可以实现这种功能.本文基于PG流复制基础上 , ...

  2. PGPool-II+PG流复制实现HA主备切换

    基于PG的流复制能实现热备切换,但是是要手动建立触发文件实现,对于一些HA场景来说,需要当主机down了后,备机自动切换,经查询资料知道pgpool-II可以实现这种功能.本文基于PG流复制基础上 , ...

  3. CentOS下PostgreSQL 主从实现之异步流复制(Hot Standby)

    Standby数据库原理 简单介绍一些基础概念与原理,首先我们做主从同步的目的就是实现db服务的高可用性,通常是一台主数据库提供读写,然后把数据同步到另一台从库,然后从库不断apply从主库接收到的数 ...

  4. PostgreSQL12主从流复制(一主两从)

    PostgreSQL12主从流复制(一主两从) 一.简介 流复制就是备服务器通过tcp流从主服务器中同步相应的数据,主服务器在WAL记录产生时即将它们以流式传送给备服务器,而不必等到WAL文件被填充. ...

  5. redis主从读写分离replication复制数据+sentienl哨兵集群主备切换

    说明:最近公司在自己搭建了一套redis主从读写分离+sentinel哨兵集群主备切换,通过手工去搭建replication复制+主从架构+读写分离+哨兵集群+高可用redis集群架构 公司的已经搭建 ...

  6. PostgreSQL主备切换

    PG主备切换 1)关闭主库,建议使用-m fast模式关闭 2)在备库上执行pg_ctl promote命令激活备库,如果recovery.conf变成recovery.done表示备库已经切换成主库 ...

  7. mysql 主从备份_mysql 主从备份(一)

    以mysql5.7为例 # master ## 修改配置文件 > sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 1. bind-address视情况是否 ...

  8. 使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群

  9. 【MySQL】MySQL复制原理与主备一致性同步工作原理解析(原理篇)(MySQL专栏启动)

最新文章

  1. php 将二维数组合并,PHP二维数组合并排重的两种方式
  2. Linux监控平台 zabbix介绍和安装
  3. Java内部类引用外部类中的局部变量为何必须是final问题解析
  4. 公布 | 中国图象图形学学会首批Fellow名单公布
  5. 加权回归估计_比率估计与回归估计
  6. IDA保存修改的寄存器值
  7. 计算机专业小米笔记本推荐,小米笔记本哪款好
  8. 华为手机刷机后显示无服务器,华为手机刷机后,无法开机怎么办?
  9. md5加密特征码java,讨论:加密算法特征码及其识别
  10. win+shift+s截图保存在哪_用惯了QQ/微信截图,你不知道的截图神器还有这个
  11. 浏览器报错 CORS 请求不是 http
  12. FDTD的PML设置
  13. python launcher卸载不了_python2的卸载
  14. 基于单片机的温湿度控制系统
  15. 12AU7设计中的一个小技巧
  16. window电脑拖动直接安装apk应用的.bat
  17. Vue的缓存方法localstorage、sessionStorage
  18. 移动、ipad、PC端浏览器的判断
  19. 服务器开超线程性能提升多少,超线程能提升处理器效率
  20. 你所不知道的口吃——口吃了该怎么办?

热门文章

  1. mysql group by与order by的研究--分类中最新的内容
  2. 查询数据库中所有表的行数(sqlserver 2000)
  3. Python模块包中__init__.py文件的作用(转载)
  4. 对象并不一定都是在堆上分配内存的
  5. 我为什么要学习C++反汇编
  6. Android中Service的启动方式及Activity与Service的通信方式
  7. Java+大数据开发——Hadoop集群环境搭建(二)
  8. Linux字符设备驱动框架
  9. Atom ctrl+atl+b 快捷键修复
  10. ssh报错:Could not load host key:/etc/ssh/ssh_host_rsa_keyssh_host_ecdsa_keyssh_host_ed25519_key...