美文网首页
Practice - Postgresql replicatio

Practice - Postgresql replicatio

作者: 华阳_3bcf | 来源:发表于2018-09-10 17:13 被阅读0次

测试环境

Azure VM
OS: redhat 7.3 (RHEL)
Postgresql: 9.2.24 (default)
参考文档 https://www.howtoforge.com/tutorial/how-to-install-and-configure-master-slave-replication-with-postgresql-96-on-centos-7/

过程概括

  • 安装/配置 Postgresql
  • 配置防火墙
  • 配置 Master
  • 配置 Slave
  • 测试

安装/配置 Postgresql (on Master and Slave)

数据库安装

sudo yum install -y postgresql-server postgresql-contrib

初始化数据库

sudo postgresql-setup initdb

配置自启动

$ sudo systemctl start postgresql
$ sudo systemctl enable postgresql
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.

检查服务, 发现5432端口

$ sudo netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      4657/sshd           
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      36607/postgres      
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      4657/sshd           
tcp6       0      0 ::1:5432                :::*                    LISTEN      36607/postgres      

配置防火墙

$ sudo firewall-cmd --add-service=postgresql --permanent
success
$ sudo firewall-cmd --reload
success
$ sudo firewall-cmd --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: eth0
 sources:
 services: dhcpv6-client postgresql ssh
 ports:
 protocols:
 masquerade: no
 forward-ports:
 sourceports:
 icmp-blocks:
 rich rules:

配置 Master

前期准备,创建同步账号和archive目录

$ sudo su - postgres
-bash-4.2$ createuser --replication -P replica
Enter password for new role:
Enter it again:
-bash-4.2$ cd /var/lib/pgsql/
-bash-4.2$ mkdir archive

修改 postgresql.conf

-bash-4.2$ cd /var/lib/pgsql/data
-bash-4.2$ vi postgresql.conf

修改以下部分
Uncomment the 'listen_addresses' line and change value of the Master server IP address

listen_addresses = '10.0.0.4'

Uncomment 'wal_level' line and change the value to 'hot_standby'.

wal_level = hot_standby

For the synchronization level, we will use local sync. Uncomment and change value line as below.

synchronous_commit = local

Enable archiving mode and give the archive_command variable a command as value. 这是手工replication需要的数据,预防自动同步失败

archive_mode = on
archive_command = 'cp %p /var/lib/pgsql/archive/%f'

For the 'Replication' settings, uncomment the 'wal_sender' line and change value to 2 (in this tutorial, we use only 2 servers master and slave), and for the 'wal_keep_segments' value is 10.

max_wal_senders = 2
wal_keep_segments = 10

For the application name, uncomment 'synchronous_standby_names' line and change value to 'pgslave01'.

synchronous_standby_names = 'pgslave01'

保存退出

修改 pg_hba.conf

Paste configuration below to the end of the line.

# Localhost
host    replication     replica          127.0.0.1/32            md5

# PostgreSQL Master IP address
host    replication     replica          10.0.0.4/32            md5

# PostgreSQL SLave IP address
host    replication     replica          10.0.0.5/32            md5

重启服务

systemctl restart postgresql

配置Slave

停服务,把data目录备份

$ sudo systemctl stop postgresql
$ sudo su - postgres
$ cd /var/lib/pgsql
$ mv data{,-backup}

把data 目录从Master 上同步过来。

-bash-4.2$ pg_basebackup -h 10.0.0.4 -U replica -D /var/lib/pgsql/data -P --xlog
Password:
36536/36536 kB (100%), 1/1 tablespace

修改 postgresql.conf
listen_addresses 改成自己的IP

listen_addresses = '10.0.0.5'

Enable hot_standby on slave

hot_standby = on

新建文件 recovery.conf
内容如下:

standby_mode = 'on'
primary_conninfo = 'host=10.0.0.4 port=5432 user=replica password=123456 application_name=pgslave01'
trigger_file = '/tmp/postgresql.trigger.5432'

修改文件权限

chmod 600 recovery.conf

启动服务

systemctl start postgresql

测试

状态检查 on master

psql -c "select application_name, state, sync_priority, sync_state from pg_stat_replication;"
psql -x -c "select * from pg_stat_replication;"

You should see the state value is streaming, and the sync_state is sync.

$ sudo su - postgres
-bash-4.2$ psql -c "select application_name, state, sync_priority, sync_state from pg_stat_replication;"
 application_name |   state   | sync_priority | sync_state
------------------+-----------+---------------+------------
 pgslave01        | streaming |             1 | sync
(1 row)

-bash-4.2$ psql -x -c "select * from pg_stat_replication;"
-[ RECORD 1 ]----+-----------------------------
pid              | 37658
usesysid         | 16384
usename          | replica
application_name | pgslave01
client_addr      | 10.0.0.5
client_hostname  |
client_port      | 49766
backend_start    | 2018-09-10 07:48:45.91729+00
state            | streaming
sent_location    | 0/301BC80
write_location   | 0/301BC80
flush_location   | 0/301BC80
replay_location  | 0/301BC80
sync_priority    | 1
sync_state       | sync

新建数据库和表来测试 on Master

$ createdb roytest
$ psql roytest
roytest=# create table fruits (id SERIAL, name char(20), primary key(id));
NOTICE:  CREATE TABLE will create implicit sequence "fruits_id_seq" for serial column "fruits.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "fruits_pkey" for table "fruits"
CREATE TABLE
roytest=#
roytest=# insert into fruits (name) values('apple');
INSERT 0 1
roytest=# insert into fruits (name) values('pear');
INSERT 0 1
roytest=# select * from fruits;
id |         name         
----+----------------------
  1 | apple               
  2 | pear                
(2 rows)

On Slave, 检查发现新库和表都同步过来了。

-bash-4.2$ psql roytest -c "select * from fruits"
 id |         name
----+----------------------
  1 | apple
  2 | pear
(2 rows)

相关文章

  • Practice - Postgresql replicatio

    测试环境 Azure VMOS: redhat 7.3 (RHEL)Postgresql: 9.2.24 (def...

  • PostgreSQL Practice & Tips -

    什么是执行计划 对于每个收到的查询请求,PostgreSQL 都会为其上设置执行计划(查询计划),能够正确的通过查...

  • PostgreSQL Practice & Tips -

    索引 介绍 索引是数据库提供的一种能够快速查询数据的方法,一般来说索引记录了表中的一列或者多列的值与其存储位置的对...

  • PostgreSQL Practice & Tips -

    几个系统字段的介绍 在面试中,我们经常会遇到这样一个问题: 如果你的表中有十万条数据,当你做了一次查询后,开始依次...

  • PostgreSQL Practice & Tips -

    事务 介绍 事务 transaction 可能是关系型数据库最重要的功能之一,往往我们通过事务可以组织一系列的操作...

  • PostgreSQL Practice & Tips -

    Why 回顾了下自己所在项目中的数据存储技术,在关系数据库方面使用了 MySQL、PostgreSQL 这两种流行...

  • GlusterFS 副本卷

    Replicated volume Replicated模式,也称作AFR(AutoFile Replicatio...

  • MySQL Replication之Performance Sc

    表一览 connection_configuration connection_status replicatio...

  • pt-osc原理

    限制 拒绝操作没有pk、uk的表(--alter) 拒绝操作复制过滤(--[no]check-replicatio...

  • 每日一画视觉实践|Day 11

    2017.10.25 Practice, Practice, Practice. 爱丽丝说“哦,我不能相信那个!”...

网友评论

      本文标题:Practice - Postgresql replicatio

      本文链接:https://www.haomeiwen.com/subject/ecisgftx.html