PostgreSQL同步複製搭建

1、初始化master節點

1. 安裝PostgreSQL
2. 初始化db
initdb -D /data/pg940_data

2、配置master節點

1. 修改postgresql.conf
$ vim /data/pg940_data/postgresql.conf
wal_level = hot_standby
fsync = on
synchronous_commit = on    #同步複製必須設置爲on
wal_sync_method = fdatasync
max_wal_senders = 4        #配置的是兩個slave節點,最好設置大一點,方便之後添加slave節點,pg_basebackup使用stream方式須要佔用2個槽
wal_keep_segments = 100

2. 修改白名單
$ vim /data/pg940_data/pg_hba.conf
host    all                 all             172.17.0.0/16           md5
host    replication         all             172.17.0.0/16           md5

3. 啓動master節點
$ pg_ctl -D /data/pg940_data start

3、製做slave節點

1. 獲取master節點的基礎備份
$ sudo mkdir -p /data/pg940_data
$ sudo chown -R postgres:postgres /data/pg940_data
$ pg_basebackup -X s -h 172.17.5.45 -U postgres -D /data/pg940_data

2. 配置postgresql.conf
hot_standby = on
max_standby_archive_delay = 30s
max_standby_streaming_delay = 30s

3. 配置recovery.conf
standby_mode = 'on'
primary_conninfo = 'host=172.17.5.45 port=5432 user=postgres password=xxxxxx application_name=node2'
restore_command = ''
archive_cleanup_command = ''

4. 啓動slave節點
$ pg_ctl -D /data/pg940_data start
$ psql -U postgres
postgres=# select * from pg_stat_replication ;
  pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_location | write_location | fl
ush_location | replay_location | sync_priority | sync_state 
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+---
-------------+-----------------+---------------+------------
 12262 |       10 | postgres | node3            | 172.17.5.47 |                 |       48691 | 2015-09-06 14:44:52.674976+08 |       400592 | streaming | 1/74000210    | 1/74000210     | 1/
74000210     | 1/74000210      |             0 | async
 12263 |       10 | postgres | node2            | 172.17.5.46 |                 |       35271 | 2015-09-06 14:44:52.677004+08 |       400592 | streaming | 1/74000210    | 1/74000210     | 1/
74000210     | 1/74000210      |             0 | async
(2 rows)

能夠看出兩個節點如今都仍是異步的 node

4、修改成同步複製

$ vim /data/pg940_data/postgresql.conf
synchronous_standby_names = 'node2, node3'    # reload配置就行,不須要重啓
$ psql -U postgres
psql (9.4.4)
Type "help" for help.
postgres=# show synchronous_standby_names;
 synchronous_standby_names 
---------------------------
 
(1 row)
postgres=# select pg_reload_conf();
 pg_reload_conf 
----------------
 t
(1 row)
postgres=# show synchronous_standby_names;
 synchronous_standby_names 
---------------------------
 node2, node3
(1 row)
postgres=# select * from pg_stat_replication ;
  pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_location | write_location | fl
ush_location | replay_location | sync_priority | sync_state 
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+---
-------------+-----------------+---------------+------------
 12262 |       10 | postgres | node3            | 172.17.5.47 |                 |       48691 | 2015-09-06 14:44:52.674976+08 |       400592 | streaming | 1/740002E8    | 1/740002E8     | 1/
740002E8     | 1/740002E8      |             2 | potential
 12263 |       10 | postgres | node2            | 172.17.5.46 |                 |       35271 | 2015-09-06 14:44:52.677004+08 |       400592 | streaming | 1/740002E8    | 1/740002E8     | 1/
740002E8     | 1/740002E8      |             1 | sync
(2 rows)

能夠看出如今node2是同步複製的,node3是備用同步複製節點 sql

相關文章
相關標籤/搜索