-
Master-slave introduction
- PostgreSQL streaming replication is asynchronous by default. If the primary server crashes, some of the committed transactions may not have been replicated to the backup server, which can cause data loss. The amount of data lost is proportional to the replication delay during failover.
- Synchronous replication can ensure that all modifications of a transaction can be transmitted to one or more synchronous backup servers. This expands the standard persistence level provided by a transaction commit. This level of protection is called 2-safe replication in computer science theory. When
synchronous_commit
is setremote_write
, then a group-1-safe (group- safe and 1-safe). - When requesting synchronous replication, each commit of a write transaction will wait until it receives a confirmation that the commit has been written to the write-ahead log on disk on both the primary and backup servers. The only possibility that data will be lost is that both the primary server and the backup server crash at the same time. This can provide a higher level of persistence, although only the system administrator is responsible for the placement and management of the two servers. Waiting for confirmation improves the user's confidence that the modification will not be lost, but it also unnecessarily increases the response time to the requested transaction. The minimum waiting time is the round-trip time between the primary server and the backup server.
- Read-only transactions and transaction rollbacks do not need to wait for the reply from the standby server. Sub-transaction commits do not need to wait for the response from the backup server, only top-level commits need to wait. Long-running actions (such as data loading or index building) will not wait for the final commit message. All two-phase submission actions require submission waiting, including preparation and submission.
- The synchronization backup can be a physical replication backup or a logical replication subscriber. It can also be a consumer of any other physical or logical WAL replication stream, and it knows how to send appropriate feedback messages. In addition to the built-in physical and logical replication system, comprising a further
pg_receivewal
andpg_recvlogical
special procedures and the like, as well as some third-party copy system and custom programs. For details of synchronous replication support, please refer to the corresponding documentation.
-
Master-slave machine distribution
-
IP address DB version Master-slave relationship 192.168.63.134 11.6 the Lord 192.168.63.141 11.6 From
-
-
Install postgresql
-
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y yum install postgresql11 -y yum install postgresql11-server -y
-
-
Main library configuration
-
Main library initialization
-
/usr/pgsql-11/bin/postgresql-11-setup initdb systemctl start postgresql-11
-
- Create replication users for master-slave synchronization
- Configure the slave library to use the repl account on the master library
-
vim /var/lib/pgsql/11/data/pg_hba.conf #Only need to add the following two lines to the station, repl is used for backup users, and the following 192.168.63.0/24 is the IP address in the network host replication repl 192.168.63.0/24 md5 host all repl 192.168.63.0/24 trust vim /var/lib/pgsql/11/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart) max_connections = 512 # (change requires restart) shared_buffers = 128MB # min 128kB dynamic_shared_memory_type = posix # the default is the first option wal_level = hot_standby # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = 'cp %p /var/lib/pgsql/11/data/pg_archive/%f' # command to use to archive a logfile segment max_wal_senders = 6 # max number of walsender processes wal_keep_segments = 10240 # in logfile segments, 16MB each; 0 disables wal_sender_timeout = 60s # in milliseconds; 0 disables log_directory = 'log' # directory where log files are written
- After the modification, you need to create some directory structures you just configured:
-
mkdir /var/lib/pgsql/11/data/pg_archive/ chown -R postgres.postgres /var/lib/pgsql/11/data
- Restart the main library service
-
systemctl restart postgresql-11
-
-
-
Slave configuration
- After the installation from the library is complete, do not initialize, if it has been initialized, delete its data directory
-
#Copy all data files of the master node [root@localhost ~]# pg_basebackup -h 192.168.63.134 -U repl -D /var/lib/pgsql/11/data/ -X stream -P Password: 25312/25312 kB (100%), 1/1 table space [root@localhost ~]# ls /var/lib/pgsql/12/data/ backup_label current_logfiles log pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf base global pg_archive pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf
- From the library configuration file, modify according to the following configuration.
[root@localhost ~]# vim /var/lib/pgsql/11/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart) max_connections = 1000 # (change requires restart) shared_buffers = 128MB # min 128kB dynamic_shared_memory_type = posix # the default is the first option wal_level = replica # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = 'cp %p /var/lib/pgsql/12/data/pg_archive/%f' # command to use to archive a logfile segment wal_sender_timeout = 60s # in milliseconds; 0 disables hot_standby = on # "on" allows queries during recovery max_standby_streaming_delay = 30s # max delay before canceling queries wal_receiver_status_interval = 10s # send replies at least this often hot_standby_feedback = on # send info from standby to prevent log_directory = 'log' # directory where log files are written,
- Create a recovery file recovery.conf.
-
[root@localhost 11]# cp /usr/pgsql-11/share/recovery.conf.sample /var/lib/pgsql/11/data/recovery.conf [root@localhost 11]# vim /var/lib/pgsql/11/data/recovery.conf # Adjustment parameters: recovery_target_timeline = 'latest' #Sync to the latest data standby_mode = on #Specify the identity of the slave trigger_file = 'failover.now' primary_conninfo = 'host = 192.168.63.134 port = 5432 user = repl password = 123456' #connect to the main library information
- Start slave
systemctl start postgresql-11
-
- Verify master-slave configuration
- Run the following command on the main library
postgres=# select client_addr,sync_state from pg_stat_replication; client_addr | sync_state ----------------+------------ 192.168.63.141 | async (1 line record)
- Run the following command on the main library
- You can create a database to verify yourself
Please refer to https://www.cnblogs.com/miclis/p/10480979.html for details of master-slave switching