PostgreSQL master-slave asynchronous streaming replication configuration (2)

Stream Replication

master firewall configuration

The whole set of environment is as follows:
PostgreSQL 9.6 SUSE environment construction (1)
PostgreSQL master-slave asynchronous streaming replication configuration (2)
PostgreSQL master-slave asynchronous and synchronous streaming replication configuration (3) The
server address is as follows:

master :10.10.56.16 
slave  :10.10.56.17
slave  :10.10.56.18
slave  :10.10.56.19 

Building a PostgeSQL the master is based on configuration (1), for details, please refer to Building a PostgreSQL installation environment (1)

After the installation is successful, add the following after the client authentication file pg_hba.conf of the master :

postgres@clw-db1:~> vim /pgdata/9.6/poc/data/pg_hba.conf 
# TYPE   DATABASE   USER         ADDRESS               METHOD
host    replication repl        10.10.56.17/32          md5
host    replication repl        10.10.56.18/32          md5
host    replication repl        10.10.56.19/32          md5

The above means: users with addresses 17, 18, and 19 repl are allowed to replicate from the host through MD5 password authentication.

After modifying the configuration file, if the PostgeSQL service is running, reload it, and if it is not running, start it to make it take effect:

postgres@clw-db1:~> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ reload
server signaled
postgres@clw-db1:~> 

After the master is successfully configured, the basic installation environment of the slave is the same as that of the master. The difference is that the slave does not need to initialize the database with initdb from the library.

slave replicates data

postgres@clw-db2:/pgdata/9.6/poc> /opt/pgsql-9.6/bin/pg_basebackup -h 10.10.56.16 -U repl -W -Fp -Pv -Xs -R -D /pgdata/9.6/poc/data/
Password: 
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
transaction log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
29956/29956 kB (100%), 1/1 tablespace                                         
transaction log end point: 0/20000F8
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
-h 表示主机地址          -W 表示需要密码        -Fp 表示普通文件格式输出 
-Xs 表示通过流复制抓取备份日志     -R 表示在输出目录默认创建一个recovery.conf文件
-u 表示用户     -D 指定数据库存放的目录

The above means to synchronize the data from the master database master to the slave

View the recovery configuration file of the slave

postgres@clw-db2:/pgdata/9.6/poc/data> cat recovery.conf
standby_mode = 'on'
primary_conninfo = 'user=repl password=123456 host=10.10.56.16 port=5432 sslmode=disable sslcompression=1'

start slave

postgres@clw-db2:/home/postgres> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ start
server starting
postgres@clw-db2:/home/postgres> FATAL:  data directory "/pgdata/9.6/poc/data" has group or world access
DETAIL:  Permissions should be u=rwx (0700).

Solution:

postgres@clw-db2:/pgdata/9.6/poc> chmod 0700 data

start PG server

postgres@clw-db2:/pgdata/9.6/poc> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ start
server starting
postgres@clw-db2:/pgdata/9.6/poc> 

Seeing the following log output indicates that the startup was successful:

postgres@clw-db2:/pgdata/9.6/poc> tail -f /pgdata/9.6/poc/data/pg_log/postgresql-2018-05-03_172139.csv
2018-05-03 17:21:39.838 CST,,,30454,,5aead4a2.76f6,2,,2018-05-03 17:21:38 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""

Check whether the master and slave are connected

postgres@clw-db1:~> /opt/pgsql-9.6/bin/psql -p 5432 -U postgres pocdb
psql (9.6.8)
Type "help" for help.

pocdb=# \x
Expanded display is on.
pocdb=#                                                 
pocdb=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 21072
usesysid         | 16385
usename          | repl
application_name | walreceiver
client_addr      | 10.10.56.17
client_hostname  | 
client_port      | 43200
backend_start    | 2018-05-03 17:21:39.842751+08
backend_xmin     | 
state            | streaming
sent_location    | 0/3000220
write_location   | 0/3000220
flush_location   | 0/3000220
replay_location  | 0/3000220
sync_priority    | 0
sync_state       | async

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325626958&siteId=291194637