Remember the establishment and configuration of the master-slave synchronization environment of Centos7.x PostgreSql database

One, database installation

Choose installation and operation according to your own environment

1, yum specified directory installation

https://blog.csdn.net/llwy1428/article/details/105143053

2, yum install directly

https://blog.csdn.net/llwy1428/article/details/102486414

3. Compile and install

https://blog.csdn.net/llwy1428/article/details/95444151

4. PostgreSql basic operation

https://blog.csdn.net/llwy1428/article/details/102598732

5. Centos7 yum installation, configuration PgAdmin4

https://blog.csdn.net/llwy1428/article/details/102486511

6, Centos7 PostgreSql database installation extension

https://blog.csdn.net/llwy1428/article/details/105167524

7. Centos7 PostgreSql database uses FDW extension

https://blog.csdn.net/llwy1428/article/details/106291669

8. Centos7 postgresql v11 installs timescale database TimescaleDB

https://blog.csdn.net/llwy1428/article/details/106357900

9. Windows 10 uses PgAdmin to back up PostgreSql database

https://blog.csdn.net/llwy1428/article/details/107031697

Two, virtual machine installation and configuration

1. Select (or install) two Centos7.x virtual machines

System download: http://archive.kernel.org/centos-vault/7.4.1708/isos/x86_64/

2. System installation: https://blog.csdn.net/llwy1428/article/details/89328381

3. Network configuration: https://blog.csdn.net/llwy1428/article/details/85058028

Note: here 192.168.11.11 is the master database node; 192.168.11.12 is the slave database node

4. Firewall: https://blog.csdn.net/llwy1428/article/details/99676257

Both virtual machine firewalls open port: 5432

]# firewall-cmd --add-port=5432/tcp --permanent
]# firewall-cmd --reload

Three, installation and configuration

Note: Here, 192.168.11.11 is the master database; 192.168.11.12 is the slave database

1. Install the database

Install PostgreSql database on the master node

Installation process: https://blog.csdn.net/llwy1428/article/details/105143053

2. Create a backup account and set a password in the main database 192.168.11.11

The system switches to the postgres user

[root@localhost ~]# su - postgres

Open the command line

-bash-4.2$ psql

Create a database user and set a password (letters in the user name should be lowercase)

CREATE USER [Username] REPLICATION LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD'[Password]';

postgres=# CREATE USER dbbak REPLICATION LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD '123456';

3. Modify the postgresql.conf configuration file

Parameters to be modified:

(1) wal_level streaming replication highest level replica

(2) max_wal_senders the number of streaming replication processes, generally the host

(3) wal_keep_segments keeps the number of WALs, which can be increased or decreased according to whether there is batch writing

(4) hot_standby yes/no allows query during recovery

Explanation of other parameters

Switch users and edit configuration files

[root@localhost ~]# su - postgres
-bash-4.2$ vim /data/pgdata/postgresql.conf

wal_level = replica

max_wal_senders = 10

wal_keep_segments = 64

hot_standby = on

4. Edit the hosts mapping

[root@localhost ~]# vim /etc/hosts

5. Modify the pg_hda.conf configuration file

-bash-4.2$ vim /data/pgdata/pg_hba.conf

Add connection configuration for hot standby user dbbak

Note: This should be limited to the connection of the slave node. The third column of this configuration is the hot standby user created in the previous article: "dbbak"

host    replication     dbbak           db.slave.bak           md5

 

Note: The fourth column in the last row is the mapping between the host name and IP of the slave node configured in /etc/hosts    

6. Restart the database service of the main server

[root@localhost ~]# systemctl restart postgresql-11

7. Install PostgreSql database service on the slave node

Create a directory and grant permissions (no need to initialize the database)

8. Use the command to synchronize the data file of the master node to the slave node

/usr/pgsql-11/bin/pg_basebackup -h [Master server address] -p [Master server port] -D [Slave server data directory] -P -U [Master server hot backup user]

Switch user from node

[root@localhost ~]# su - postgres

Execute on the slave node

-bash-4.2$ /usr/pgsql-11/bin/pg_basebackup -h 192.168.11.11 -p 5432 -D /data/pgdata/ -P -U dbbak

Enter the password of the master node server

Password: ***********

View the data directory of the slave node:

-bash-4.2$ ll /data/pgdata/

9. Modify the postgresql.conf configuration file on the slave node (optional)

-bash-4.2$ vim /data/pgdata/postgresql.conf

Modify the slave node database port to 5433

10. Create the configuration file recovery.conf

-bash-4.2$ vim /data/pgdata/recovery.conf

Write content

standby_mode ='on'
primary_conninfo ='host=192.168.11.11 port=5432 user=dbbak password=123456'
trigger_file ='/tmp/pgsql_master'

Description:'host=192.168.11.11 port=5432 user=dbbak password=123456' is the hot standby user and connection information of the master node

11. Start the database service from the node

[root@localhost ~]# systemctl start postgresql-11

12. The sub-table checks the startup status of the database on the master and slave nodes:

Master node:

 

Slave node:

 

13. Execute commands on the main node to check the result of the construction:

[root@localhost ~]# su - postgres
-bash-4.2$ psql
postgres=# \x on
postgres=# select * from pg_stat_activity where usename = 'dbbak';

Four, test:

1. Create a database on the main node:

postgres=# create database dbtest owner postgres;

2. View at the slave node:

 

 

At this point, the Centos7.x PostgreSql database master-slave synchronization environment has been set up and configured!

Guess you like

Origin blog.csdn.net/llwy1428/article/details/107531011