PostgreSQL Tutorial: Master-Slave (Master-Slave) Failover

Master-slave switching (don’t play it this way)

In fact, the essence of master-slave is that the slave node goes to the master node to continuously back up new data.

There are actually two configuration file systems:

  • standby.signalFile, this is to start the backup from the node
  • postgresql.auto.conffile, this slave node specifies the address information of the master node

Switching means that the original owner adds the above configuration and the original slave deletes the above configuration.

1. All master and slave nodes stop:………………

2. Delete the above configuration from the original:…………

3. Start the service from the new master:…………

4. Back up the data from the original master and the new slave: pg_basebackup operation, decompress at the same time, and then modify the postgresql.conf file and standby.signal configuration file

5. Start the original owner and new slave to view information

Master-slave failover

By default, the master-slave backup here is asynchronous, which leads to a problem. If the data written by the master node has not been backed up to the slave node, the master node suddenly goes down. As a result, if the master-slave switch is implemented based on the above method, the data will be lost. May be lost.

PGSQL provides an pg_rewindoperation after version 9.5, which helps us make a comparison based on archive logs to compare the archive logs to see if there is a time difference conflict.

Implement operations:

1. Rewind needs to enable a configuration before it can be used.

postgresql.confUnder modificationwal_log_hints = 'on'

2. In order to use rewind more conveniently, you need to set the environment variables of /usr/pgsql-12/bin/

vi /etc/profile
  追加信息
  export PATH=/usr/pgsql-12/bin/:$PATH
source /etc/profile

3. Simulate the main database downtime and shut down the main database directly.

4. Switch from slave node to master node

# 因为他会去找$PGDATA,我没配置,就基于-D指定一下PGSQL的data目录
pg_ctl promote -D ~/12/data/

5. Power on the original master node, execute the command, and complete the synchronization of the archive logs.

  • Start virtual machine
  • Stop PGSQL service
    pg_ctl stop -D ~/12/data
    
  • Join the cluster based on pg_rewind
    pg_rewind -D ~/12/data/ --source-server='host=192.168.11.66 user=postgres password=postgres'
    
  • If the above command fails, you need to start and shut down PGSQL and execute it to complete the synchronization of the archive log.
    pg_ctl start -D ~/12/data
    pg_ctl stop -D ~/12/data
    pg_rewind -D ~/12/data/ --source-server='host=192.168.11.66 user=postgres password=postgres'
    

6. Modify the configuration of the new slave node and then start it

  • Build standby.signal
    standby_mode = 'on'
    
  • Modify the postgresql.auto.conf file
    # 注意ip地址
    primary_conninfo = 'user=postgres password=postgres host=192.168.11.66 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'
    restore_command = 'cp /archive/%f %p'
    
  • Start a new slave node
    pg_ctl start -D ~/12/data/
    

Guess you like

Origin blog.csdn.net/a772304419/article/details/132930644