Postgresql 9.6 搭建 异步流复制 和 同步流复制 详细教程
Basic Replication
If you’re feeling overwhelmed, try setting up a slave to see how easy it is! We’ll assume that you have a running PostgreSQL installation on the IP 10.0.0.10 and that you’re setting up a slave at 10.0.0.11, with both running PostgreSQL 9.5.4.
Master Setup
First, have a look at the master’s settings (postgresql.conf) and update them if needed (and restart the server for changes to take effect):
# The WAL level should be hot_standby or logical.
wal_level = hot_standby
# Allow up to 8 standbys and backup processes to connect at a time.
max_wal_senders = 8
# Retain 1GB worth of WAL files. Adjust this depending on your transaction rate.
max_wal_size = 1GB
Next, we need to create a user. This streaming replication client in the slave node will connect to the master as this user.
$ sudo -u postgres psql
postgres=# create user rep superuser password '111111';
postgres=# \q #(ctrl+d)
In pg_hba.conf, allow this user to connect for replication.
# TYPE DATABASE USER ADDRESS METHOD
host replication rep 10.0.0.11/32 md5
The user repluser is a regular Postgres user, so feel free to use your standard authentication practices. Remember to reload (sudo systemctl reload postgresql) for changes to take effect.
Slave Setup
We need to initialize the slave with a backup of the master’s databases. We’ll do this directly from the slave. Note that this process will wipe all the data on the slave.
First, let’s stop the slave:
$ sudo systemctl stop postgresql
Next, we’ll take the backup:
$./pg_basebackup -h10.0.0.10-U rep -F p -x -P -R -D/var/lib/pgsql/9.6/db/-l replbackup
This will connect to the master as the user we just created, and take a backup of the master’s databases and write it to /var/lib/pgsql/9.6/db/. Now let’s restore this:
We need to create a file called recovery.conf in the data directory of the slave. This will tell the slave how to connect to the master.
And finally, we’ll enable the slave to serve as a read replica. To do this, enable hot_standby in postgresql.conf:
hot_standby = on
Now we’re ready to start the slave:
$sudo -u postgres ./pg_ctl start -D /var/lib/pgsql/9.6/db/ #https://www.postgresql.org/docs/9.5/static/app-pg-ctl.html
The slave should startup, init the streaming replication and get into hot standby mode.
Congratulations! You have now setup a slave node that uses streaming replication to stay in sync with the master.
Here is how the slave behaves when you try to make changes:
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
t
(1 row)
postgres=# create database testme;
ERROR: cannot execute CREATE DATABASE in a read-only transaction
Multiple Slaves
You can repeat the steps for the slave setup on another node, to bring up another slave. Ensure that max_wal_senders is high enough to accomodate all the slaves and the occasional backup or administrative process that connects to the master.
Synchronous Replication
To enable synchronous replication with one more more standby’s, first ensure that each standby has a name. You can specify the name as application_name in the primary_conninfo parameter in the standby’s recovery.conf, like this:
# In the file /var/lib/postgresql/9.6/db/recovery.conf, include the
# application name in the primary_conninfo:
standby_mode = 'on'
primary_conninfo = 'user=rep password=111111 host=10.0.0.10 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres application_name=kzwr'
Once each standby has a name that the master can refer to, you can edit the master’s postgresql.conf:
# Wait for standbys to finish writing out WALs to disk, before returning from
# the commit.
synchronous_commit = remote_write
# These are the standbys that need to acknowledge the write. You can also use
# '*' to indicate all the standbys.
synchronous_standby_names = 'kzwr'
Naturally, synchronous commits take more time to complete, but provide increased safety against crashes of the master server.
Cascading Replication
Rather than replicating two slaves off the same master, you can replicate one slave off another. This is usually called “cascading replication”.
A hot standby can serve as a master itself, and permit replicating off it. Nothing extra needs to be configured, other than the master settings like max_wal_senders and pg_hba.conf authentication.
Cascading replication is always asynchronous.
Slave Promotion
Slaves can be promoted to be a master. This can be done by running the command:
$ sudo pg_ctlcluster 9.5 main promote
You can also specify a file name (called a “trigger file”) in recovery.conf:
# In the file /var/lib/postgresql/9.5/main/recovery.conf, include a
# trigger file name:
trigger_file = '/tmp/pg-trigger-failover-now'
Postgres will do a failover and promote the slave if it finds that this file has been created.
After a failover, the recovery.conf file will be renamed to recovery.done. The contents of the file will not be read and considered by Postgres after that.
Before attempting the failover, ensure that the slave node has an identical configuration (including pb_hba.conf with the proper IPs) to the existing master. Also, after a successful failover, the old master should be taken out of service (“shoot the other node in the head”). Applications should not attempt to talk to the retired master.
Postgresql 9.6 搭建 异步流复制 和 同步流复制 详细教程的更多相关文章
- 最新的Windows环境搭建zeroMQ并使用java代码运行zeromq详细教程
最近项目要用zeromq,linux上很好配置使用,但是windows上配置与使用没有找到合适的解决方案,看的很头疼,这里自己总结下供大家参考 准备工作: 1.libzmq下载地址:https://g ...
- 1018关于MySQL复制搭建[异步复制和半同步复制]
转自:http://www.cnblogs.com/ivictor/p/5735580.html 搭建MySQL数据库的主从架构,还是蛮简单的.重要的几个命令整理一下. 主从服务器上: SHOW VA ...
- postgresql从库搭建
1 复制类型 PostgreSQL支持物理复制(流复制)及逻辑复制2种.通过流复制技术,可以从实例级复制出一个与主库一模一样的实例级的从库.流复制同步方式有同步.异步两种. 另一种复制方式为逻辑复制, ...
- MySQL系列详解八:MySQL多线程复制演示-技术流ken
前言 Mysql 采用多线程进行复制是从 Mysql 5.6 开始支持的内容,但是 5.6 版本下有缺陷,虽然支持多线程,但是每个数据库只能一个线程,也就是说如果我们只有一个数据库,则主从复制时也只有 ...
- Nginx+PostgreSQL+Django+UWSGI搭建
最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx.常见的 django webapp 部署方式采用FCGI 或 WSGI ...
- Java IO流之【缓冲流和文件流复制文件对比】
与文件流相比,缓冲流复制文件更快 代码: package Homework; import java.io.BufferedOutputStream; import java.io.File; imp ...
- mysql主从复制的异步复制与同步复制
异 步复制:MySQL本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时 – 最重要的是这意味着当应用系统的事务提交已经确认时数据并不能在同一时刻拷贝/应用到从机 ...
- [MGR——Mysql的组复制之多主模式 ] 详细搭建部署过程
组复制可以在两种模式下运行. 1.在单主模式下,组复制具有自动选主功能,每次只有一个 server成员接受更新.2.在多主模式下,所有的 server 成员都可以同时接受更新. 组复制与异步主从复 ...
- 使用文件流与使用缓冲流完成文件的复制操作性能对比,文件流 FileInputStream FileOutputStream 缓冲流: BufferedInputStream BufferedOutputStream
package seday06; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOExc ...
随机推荐
- C++11新特性介绍 01
阅读目录 1. 概述 2. long long 类型 3. 列表初始化 4. nullptr 空指针 5. constexpr变量 6. constexpr函数 7. using类型别名 8. aut ...
- 将Hive统计分析结果导入到MySQL数据库表中(一)——Sqoop导入方式
https://blog.csdn.net/niityzu/article/details/45190787 交通流的数据分析,需求是对于海量的城市交通数据,需要使用MapReduce清洗后导入到HB ...
- Struts2单例和多例
struts2中action是多例的,即一个session产生一个action如果是单例的话,若出现两个用户都修改一个对象的属性值,则会因为用户修改时间不同,两个用户访问得到的属性不一样,操作得出的结 ...
- instr 函数从后往前计数 instr(spell,' ',-1)
update CY set last=substr(spell,instr(spell,' ',-1));
- jsp 学习 第2步 - tag 使用
tag 类似 asp.net 用户控件,用于动态显示HTML 我首先在项目 /WebContent/WEB-INF/ 建立 tags目录 用于存放 tag文件 新建一个message.tag 文件 ...
- Maven配置本地仓库
当我们在myeclipse中update maven时可能会报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml,以致于 ...
- sqlserver数据库导出成insert语句
点击数据库名称右键=========>任务========>生成脚本 一.表结构导出成sql语句 二.数据导出成sql语句
- 动量Momentum梯度下降算法
梯度下降是机器学习中用来使模型逼近真实分布的最小偏差的优化方法. 在普通的随机梯度下降和批梯度下降当中,参数的更新是按照如下公式进行的: W = W - αdW b = b - αdb 其中α是学习率 ...
- asp.net页如何获取母版页控件
获取母版页的相关内容有两种方法 1 通过findcontrol找控件ID需要在此事件中~因为Page_load中时是先内容页加载然后才是母版页加载 protected void Page_LoadCo ...
- 使用mybatis提供的各种标签方法实现动态拼接Sql。这里演示where标签和if标签实现使用姓名的模糊查询和性别查询用户列表,当用户没有选择姓名以及性别时查询出所有的记录。
1.需求: 使用姓名的模糊查询和性别查询用户列表,当用户没有选择姓名以及性别时查询出所有的记录. 2.在UserMapper接口中定义方法: public List<User> findU ...