master:10.0.1.114

slaver:10.0.1.116

一、yum安装https://blog.csdn.net/weixin_41048363/article/details/80310285

二、主从配置

1、主数据配置(10.0.1.114)

1.1  初始化

/usr/pgsql-9.6/bin/postgresql96-setup initdb

  

1.2 启动postgresql

systemctl start postgresql-9.6

  

1.3 切换到postgres用户(安装好生成默认的用户)

[root@steven1 data]# su - postgres
上一次登录:三 5月 16 09:19:49 UTC 2018pts/0 上
-bash-4.2$
-bash-4.2$ psql 进入数据库 postgres=#

  

1.4 创建账号并授权

postgres=# create role 账户名 login replication encrypted password '密码';

  

1.5  修改/var/lib/pgsql/9.6/data/pg_hba.conf配置文件.

# IPv4 local connections:
host all all 127.0.0.1/32 ident
# 添加如下内容
host replication repl 10.0.1.0/24 md5
host all repl 10.0.1.1/24 trust

  

1.6 修改postgresql.conf

-bash-4.2$ vim postgresql.conf
listen_addresses = '10.0.1.114'
wal_level = hot_standby #热备模式
max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少
wal_keep_segments = 10240 #重要配置
wal_send_timeout = 60s
max_connections = 512 #从库的 max_connections要大于主库
archive_mode = on #允许归档
archive_command = 'cp %p /url/path%f' #根据实际情况设置

  

三、从数据库配置

1、切换到postgres

[root@localhost data]# su - postgres

  

2、拷贝master配置相关文件。

-bash-4.2$ rm -rf /var/lib/pgsql/9.6/data/*
-bash-4.2$ pg_basebackup -h 10.0.1.114 -U repl -D /var/lib/pgsql/9.6/data -X stream -P
-bash-4.2$ cp /usr/pgsql-9.6/share/recovery.conf.sample /var/lib/pgsql/9.6/data/recovery.conf

  

3、修改recovery.conf文件

standby_mode = on
primary_conninfo = 'host=10.0.1.114 port=5432 user=master创建的用户名 password='密码'
trigger_file = '/var/lib/pgsql/9.6/data/trigger.kenyon' #主从切换时后的触发文件
recovery_target_timeline = 'latest'

  

4、修改postgresql.conf文件

listen_addresses = 10.0.1.116
wal_level = hot_standby
max_connections = 1000 #一般从的最大链接要大于主的。
hot_standby = on #说明这台机器不仅仅用于数据归档,也用于查询
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s #多久向主报告一次从的状态。
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行范例

  

四、检测

1、在master数据库中测试

postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-------------+------------
10.0.1.116 | async
(1 row)

  

2、在master数据库中创建test数据库

postgres=# create database test;
CREATE DATABASE
postgres=# \l // 查看当前所有数据库
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows) postgres=#

  

3、在从数据库中查看数据库是否同步。

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

  

参考:https://www.linuxidc.com/Linux/2017-03/142145.htm

postgresql主从配置的更多相关文章

  1. postgresql 主从配置

    安装postgresql 主从是否一定需要分两台机器,主从必须要同一个版本,不然启动会报错. 3. 配置Master数据库 su – postgres /usr/local/pgsql/bin/pg_ ...

  2. CentOS7 PostgreSQL 主从配置( 二)

    同步流复制配置PostgreSql的流复制是异步的,缺点是Standby上的数据落后于主库上的数据,如果使用Hot Standby做读写分离,就会存在数据一致性的问题.PostgreSql9.1版本后 ...

  3. CentOS7 PostgreSQL 主从配置( 一)

    主库配置 pg_hba.conf host replication all 10.2.0.0/0 trust postgresql.conf listen_addresses = '*' max_wa ...

  4. CentOS7 PostgreSQL 主从配置( 三)

    postgres 主备切换 主备查看 方法 ps -ef | grep wal (主库 sender)postgres 27873 27864 0 5月06 ? 00:00:10 postgres: ...

  5. postgresql pgsql最新版安装指南及数据存储路径更改及主从配置

    postgresql pgsql最新版安装指南及数据存储路径更改及主从配置 安装指南 首先在apt的list添加你当前系统版本对应的apt列表 目前官网有16.04,14.04,12.04 分别对应下 ...

  6. postgresql主从同步配置

    前言 不久前,公司的一台物理机器硬件坏了,导致运行在其上的虚拟机都挂了.很不凑巧的是,我负责的那台虚拟机的系统盘坏了(ps:感觉老天在玩我),导致里面的数据永远的离我而去(ps:当时我的内心是崩溃的) ...

  7. 再不了解PostgreSQL,你就晚了之PostgreSQL主从流复制部署

    前言 在MySQL被收购之后,虽然有其替代品为: MariaDB,但是总感觉心里有点膈应.大家发现了另一款开源的数据库: PostgreSQL. 虽然centos自带版本9.2也可以用,但是最近的几次 ...

  8. PostgreSQL9.6主从配置

    参考文档: 备机日志传送:https://www.postgresql.org/docs/9.6/static/warm-standby.html 英文文档:https://www.postgresq ...

  9. redis原理及集群主从配置

    一.简介 存储系统背景 存储系统有三类: RDBMS oracle,dh2,postgresql,mysql,sql server NoSQL: KV NoSQL:redis,memcached 列式 ...

随机推荐

  1. 敏捷项目管理工具-Trello(电子看板)

    Trello简介(https://www.trello.com) A Trello board is a list of lists, filled with cards, used by you a ...

  2. linux 配置阿里云yum库

    备份当前yum库 mv /etc/yum.repos.d /etc/yum.repos.d.backup4comex 新建yum源配置目录 mkdir /etc/yum.repos.d 设置阿里yum ...

  3. ( 递归 )Fractal -- POJ -- 2083

    http://poj.org/problem?id=2083 Fractal Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  4. codeforce868c

    C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. imooc movie

    node+mongodb 建站攻略(一期) 用的都是我熟悉的技术,看了别人的开发过程,自己也学到了一些新的知识 生成配置文件 开发结束后,可以使用bower init来生成前端的配置文件. 不过在bo ...

  6. python 读取hive数据

    话不多说,直接上代码 from pyhive import hivedef pyhive(hql): conn = hive.Connection(host='HiveServer2 host', p ...

  7. linux 查看、关闭 ssh pts/n登录的用户

    1.查看登录用户: [root@TiaoBan- bin]# w :: up days, :, users, load average: 1.90, 1.75, 1.84 USER TTY FROM ...

  8. Excel 多个单元格输入同样内容

    step1: 将这些单元格选定.方法:可以连续选,也可以 ctrl + select不连续选择: step2:输入你想输入的内容,PS:出现在最后选择的单元格中: step3:组合键:ctrl + e ...

  9. c# MVC Action 如何知道 发送方给你的 Json 数据的格式内容是什么

    public class DemoModel { public string Name { get; set; } public int Age { get; set; } } [HttpPost] ...

  10. 为什么要使用Entity Framework

    本文介绍从DDD(Domain-Driven Design[领域驱动设计])的角度来说说为什么要使用Entity Framework(以下都会简称为EF),同时也看出类似Drapper之类的简陋ORM ...