postgreSQL master-slave replication

I. Introduction

After 9.0 postgres introduced from the main flow of the replication mechanism, a so-called stream reproduction, the corresponding data is synchronized from the master server from the server via tcp stream. So that when the primary server data from the server when the backup is still missing.
Compared with file-based log shipping, replication flow is allowed to remain updated from the server. Connection from the master server, which records the generated stream to WAL from the server, without waiting for the primary file server WAL finished.
PostgreSQL streaming replication is asynchronous by default. Filed on the primary server transaction and there is a small delay between changes seen from the server, this delay is much less than a log-based file transfer, typically 1 second to complete. If the primary server crashes suddenly, there may be a small amount of data loss.
Synchronous replication master server and must wait before committing a transaction from the server written WAL. So to some extent, will increase the response time of the transaction.
Note: This experiment is based on the completion of docker

Two, postgresql installation

docker pull postgresql:9.4
docker images
REPOSITORY                                               TAG                 IMAGE ID            CREATED             SIZE
docker.io/postgres                                       9.4                 36726735dc3c        2 weeks ago         206 MB
docker run -it --name postgresql postgres:9.4 bash
su postgres
cd /usr/lib/postgresql/9.4/bin

Presence / var / lib / postgresql / data directory, initialize the database

./initdb -D /var/lib/postgresql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Etc/UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /var/lib/postgresql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

        ./postgres -D /var/lib/postgresql/data
or
        ./pg_ctl -D /var/lib/postgresql/data -l logfile start

Here pg database installation is complete
open the database, if you do not log can not add logfile

./pg_ctl start -D /var/lib/postgresql/data

Also start a standby database

docker run -it --name postgresql2 postgres:9.4 bash

Step same as above

Third, the operation of the primary server

The primary server is 172.18.0.4
create a new directory for the archive log, I've actually does not archive log, on-demand ask.

mkdir /opt/pgsql/pg_archive

1. First you need to create a database user master-slave synchronization. Create a user replica, and give login and copy rights.

postgres# CREATE ROLE replica login replication encrypted password 'replica'

2. Modify pg_hba.conf, allows the user to synchronize the replica.
Add two lines in pg_hba.conf:

host all all 172.18.0.5/32 trust # 0.5 allows a server connected to a main
host replication replica 172.18.0.5/32 md5 # 0.5 allows the user to copy using the replica

Thus, the user can set the replica copy request from the streaming 172.18.0.4.
* Note: The second field must be filled Replication
4. Modify postgresql.conf

listen_addresses = '*' # listens on all the IP
archive_mode = ON # allows archiving
archive_command = 'cp% p / opt / pgsql / pg_archive /% f' # logfile segment used to archive this command, the on-demand requirements.
= hot_standby wal_level
max_wal_senders = # 32 this set can connect up to several streams replication, almost several slave, to set several, preferably set slightly larger.
wal_keep_segments = 256 # xlog maximum number of copy reservations set stream, a segment of 16MB, try to set a large value to prevent the main library generation has not had time to a fast log hot_standy was covered.
wal_sender_timeout = 60s # set timeout period. replicate master transmission data
max_connections = 100 # to be noted that under this setting, the library must be greater than max_connections main library

After two configuration file to restart the server.

pg_ctl stop -D /var/lib/postgresql/data
pg_ctl start -D /var/lib/postgresql/data

0.5 0.4 3. Test database can connect. Run the following commands on 0.5:

psql -h 172.18.0.4 -U postgres

See if you can access to the database. If so, then normal.

Fourth, the operation from the server

1. copy data from the master node to the slave node

SU - Postgres
RM -rf / opt / pgSQL / data / * first data in the data directory # emptied
pg_basebackup -h 172.18.0.4 -U replica -D / var / lib / postgresql / data -X stream -P # from 0.4 to 0.5 copy data (based backup)
mkdir / opt / pgSQL / pg_archive

2. Configure recovery.conf
copy /usr/share/postgresql/9.4/recovery.conf.sample to /var/lib/postgresql/data/recovery.conf

cp /usr/share/postgresql/9.4/recovery.conf.sample /var/lib/postgresql/data/recovery.conf

Modify recovery.conf

standby_mode = on # Description of the node from the server
primary_conninfo = 'host = 172.18.0.4 port = 5432 user = replica password = replica' # master server and the user connection information
recovery_target_timeline = 'latest'

3. Configure postgresql.conf

= hot_standby wal_level
max_connections = 1000 # write more general search applications than larger maximum number of connections from the library
hot_standby = on # description This machine is not only used for data archiving, data is also used to query
max_standby_streaming_delay = 30s # data streams the maximum delay time of the backup
wal_receiver_status_interval = 10s # how long the main report from the time, of course, will copy the data from each state to the main report, but here the longest set time interval
hot_standby_feedback = on # if there is an error in data replication, whether feedback to the master

After configuration, reboot from the server

pg_ctl stop -D /var/lib/postgresql/data
pg_ctl start -D /var/lib/postgresql/data

V. Verify successful deployment

Executed in the main node database:

select client_addr,sync_state from pg_stat_replication;

The results are as follows:

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

postgres=# 

0.5 Description from the server, upon receiving the stream, and the stream is asynchronous replication.
You can also separately in the main, run from the node ps aux | grep postgres to see the process:
the primary server (0.4):

ps aux | grep postgres
root 210 0.0 0.0 48508 1548 ? S 06:34 0:00 su postgres
postgres 211 0.0 0.1 19864 2256 ? S 06:34 0:00 bash
postgres 250 0.0 0.9 273940 17632 ? S 06:41 0:00 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/data
postgres 252 0.0 0.2 274044 3800 ? Ss 06:41 0:00 postgres: checkpointer process
postgres 253 0.0 0.1 274072 3216 ? Ss 06:41 0:00 postgres: writer process
postgres 254 0.0 0.3 273940 6108 ? Ss 06:41 0:00 postgres: wal writer process
postgres 255 0.0 0.1 274348 2656 ? Ss 06:41 0:00 postgres: autovacuum launcher process
postgres 256 0.0 0.0 129220 1836 ? Ss 06:41 0:00 postgres: stats collector process
postgres 276 0.0 0.1 274480 3164 ? Ss 06:57 0:00 postgres: wal sender process replica 172.18.0.5(42834) streaming 0/3019C90
postgres 391 0.0 0.0 38296 1752 ? R+ 07:36 0:00 ps aux
postgres 392 0.0 0.0 12772 692 ? S+ 07:36 0:00 grep postgres

We can see there is a wal sender process.

From the server (94):

ps aux | grep postgres
root 394 0.0 0.0 48508 1548 ? S 06:42 0:00 su postgres
postgres 395 0.0 0.1 19884 2320 ? S 06:42 0:00 bash
postgres 488 0.0 2.3 314268 45052 ? S 06:57 0:00 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/data
postgres 489 0.0 0.2 314452 4904 ? Ss 06:57 0:00 postgres: startup process recovering 000000010000000000000003
postgres 490 0.0 0.1 314388 3524 ? Ss 06:57 0:00 postgres: checkpointer process
postgres 491 0.0 0.1 314268 2956 ? Ss 06:57 0:00 postgres: writer process
postgres 492 0.0 0.0 129220 1848 ? Ss 06:57 0:00 postgres: stats collector process
postgres 493 0.0 0.2 319036 4384 ? Ss 06:57 0:01 postgres: wal receiver process streaming 0/3019C90
508 0.0 0.0 38 296 1756 Postgres? R + 07:37 0:00 PS the AUX
Postgres 0.0 12772 509 0.0 700? S + 07:37 0:00 grep Postgres
can see there is a wal receiver process.
So far, PostgreSQL installation is deployed from the main stream replication.
Inserting data on the primary server, or delete data on the server can be seen from the corresponding changes. Only query from the server, you can not be inserted or deleted.

On Main:

postgres=# \c test;
You are now connected to database "test" as user "postgres".
test=# create table company(
test(# id int primary KEY NOT NULL,
test(# name TEXT NOT NULL,
test(# age INT NOT NULL,
test(# address CHAR(50),
test(# salary REAL,
test(# join_date DATE
test(# );
CREATE TABLE
test=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.00,'2001-07-13');
INSERT 0 1
test=#
test=#
test=# select * from company;
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
1 | Paul | 32 | California | 20000 | 2001-07-13
(1 row)

from up:

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

postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# select * from company
test-# ;
 id | name | age |                      address                       | salary | join_date  
----+------+-----+----------------------------------------------------+--------+------------
  1 | Paul |  32 | California                                         |  20000 | 2001-07-13
(1 row)
s

carry out!

Guess you like

Origin blog.51cto.com/13670314/2449031