Postgresql 10 streaming replication configuration process - measured

I am using two NanoPI-installed armbian systems, which is a small ubuntu small system. It is perfect, but the temperature of the chip is a little high. It can be used as the smallest system of UBUNTU. You can use apt- Get can be done, this is another story (-_-)

So I used two small boards to make such a postgresql streaming replication game. This operation is also applicable to ordinary LINUX, because my Postgresql is installed from the downloaded source code. The installation process is very simple. I will start another document later. After finishing, put this last link

I am also a beginner, so I have referenced a lot of setting methods on the Internet. After many tests, I finally set it up successfully, and the master-slave switch has also been tested. The 10 version will automatically synchronize data when switching back and forth, and I don’t need to make it myself like before. The operation of the timeline or something, write it down to record, the second is to refer to if you need it, thank you


One active and standby machine planning

Hostname |IP|Role|Port
:----:|:----:|:----:|:----:|:----:|:- ---:
NanoPI-005|10.10.10.205|Master|5432
NanoPI-006|10.10.10.206|Slave|5432
Premise: Install the pg database on the two hosts respectively, I compiled and installed version 10 using the source code .


2. Create streaming replication
2.1 Set the host
master and slave nodes to operate.
[root@bogon ~]# vim /etc/hosts #Edit
as follows:
10.10.10.205 NanoPI-005
10.10.10.206 NanoPI-006


2.2 In the main library settings
2.2.1 First initialize the new database
postgres@NanoPI-005:~$initdb -D ~/data/ 2.2.2 Start the database and create a synchronization user postgres@NanoPI-005:~$pg_ctl -D ~/data/ -l ~/log/pglog.log start postgres@NanoPI-005:~$psql




postgres=#create role username for synchronization login replication encrypted password 'password';
CREATE ROLE
postgres=#\q --exit psql 2.2.3 Configure ~/data/pg_hba.conf Add the following content created by host replication in the database Username for synchronization Standby IP address or domain name/32 trust 2.2.4 Configure ~/data/postgres.conf Find and modify to the following content Change the monitoring port: Listen_adresses = '*' wal_level = hot_standby /Version after 10 is replica The master-slave is set to hot-blooded mode, and max_wal_senders=2 must be selected for streaming replication / the default is 10. Streaming replication allows connection processes, it is better to have more points. I have not succeeded in 2, so set it to 10. wal_keep_segments = 64 max_connections = 100 default parameters, Non-master-slave configuration related parameters, indicating the number of connections to the database 2.2.5 Restart the master library service to update the configuration















postgres@NanoPI-005:~$pg_ctl -D ~/data/ -l ~/log/pglog.log restart 2.3 Setting 2.3.1 in the slave library does not require initialization, just back it up directly from the master library, if there is DATA, delete it directly Or change the name to postgres@NanoPI-006:~$pg_basebackup -h main database address 10.10.10.205 -p 5432 -U username for synchronization created in the database -F p -P -D ~/data/ may have to enter a password Remarks : -h, main library host, -p, main library service port; -U, replication user; -F, p is the default output format, the output data directory and table space have the same layout, t indicates tar format output; -P, Same as --progress, display the progress; -D, output to the specified directory; 2.3.2 Modify the configuration file from the library postgres@NanoPI-006:~$vi ~/data/postgresql.conf Comment out the following parameters such as wal_level, max_wal_senders  wal_keep_segments Open the following content hot_standby = on #Allow   queries while backing up max_standby_streaming_delay = 30s #Optional , the maximum delay of streaming replication























wal_receiver_status_interval = 10s #Optional , the maximum interval for reporting status from the slave to the master
hot_standby_feedback = on #Optional , feedback to the master when query conflicts
max_connections = 1000 #Default parameter, non-master-slave configuration related parameters, indicating the number of connections to the database, Generally, when the slave library is the main read service, the setting value needs to be higher than that of the master library. 2.3.3 Configuration ~/data/pg_hba.conf Add the following content to the user name for synchronization created in the database by host replication The IP address or domain name of the master library/ 32 trust or md5 #The IP address of the main library maintained in the slave library is for later switching Use 2.3.4 to create a recovery file recovery.conf postgres@NanoPI-006:~$cp /usr/local/postgres/share/recovery.conf .sample ~/data/recovery.conf Modify the parameters in the file postgres@NanoPI-006:~$vi ~/data/postgresql.conf recovery_target_timeline = 'latest' standby_mode = on













primary_conninfo = 'host=main library address 10.10.10.205 port=5432 user=username for synchronization created in the database password=password' Note: vim ~/data/ recovery.conf #When doing basic backups, you can also pass - The R parameter automatically produces a recovery.conf file after the backup. standby_mode = on #Indicate the identity of the slave library primary_conninfo = 'host=10.10.10.205 port=5432 user=username password=password for synchronization' #Connect  to the main library information recovery_target_timeline = 'latest' #Sync to the latest data Specify trigger file. When the file exists, it will trigger the promotion of the slave library to the master library, provided that "standby_mode = on" must be set; if this parameter is not set, "pg_ctl promote" can also be used to trigger Switch from library to main library #trigger_file = '/postgres/data/trigger_activestandby'Because the main library uses md5 authentication, password authentication is required here. 2.3.5 Start the slave database data service postgres@NanoPI-006:~$pg_ctl -D ~/data/ -l ~/log/pglog.log start 2.4 Verify the master-slave configuration 2.4.1 View the master database sender process

















postgres@NanoPI-005:~$ps -ef|grep postgres
View the username for synchronization created in the database with wal sender process Slave library address 2.4.2 View slave library sender process postgres@NanoPI-006:~$ps -ef |grep postgres to check the wal receiver process 2.4.3 Use SQL to see the master-slave status postgres@NanoPI-005:~$psql postgres=#select * from pg_stat_replication; the master library will show that sync_state is async address is the slave Check the status from the library No record 2.5 Streaming replication data synchronization test Start master and slave databases respectively Create a database and temporary table on master postgres@NanoPI-005:~$ psql psql (9.6.1) Type "help" for help. postgres=# \password #Create database password #Create test database postgres=# create database test; CREATE DATABASE postgres=# \c test
























You are now connected to database "test" as user "postgres".
test=# create table tt(id serial not null,name text);
CREATE TABLE
test=# insert into tt(name) values ​​('china');
INSERT 0 1


Query the table and data just created on the slave to determine whether there is data synchronization
postgres@NanoPI-006:~$ psql
psql (9.6.1)
Type "help" for help.


postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# select * from tt;
 id | name  
----+-------
  1 | china
(1 row)


2.6 Master-slave switching mode


2.6.1 Master View the status of the database and standby database


Main database
postgres@NanoPI-005:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in production


备库
postgres@NanoPI-006:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in archive recovery


2.6.2停主库服务
postgres@NanoPI-005:~$ pg_ctl stop -m fast
waiting for server to shut down.... done
server stopped
postgres@NanoPI-005:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               shut down


2.6.3激活备库
postgres@NanoPI-006:~$ pg_ctl promote
waiting for server to promote...... done
server promoted
postgres@NanoPI-006:~$ pg_controldata | grep 'Database cluster state'
Database cluster state:               in production


2.6.4测试在激活的备库中写入数据
postgres@NanoPI-006:~$ psql
psql (10.3)
Type "help" for help.


postgres=# \d
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | test | table | postgres
(1 row)


postgres=# select * from test;
 id | name  | salary
----+-------+--------
  2 | LiShi |  12000
(1 row)


postgres=# insert into  test values (1,'Hallo',20000);
INSERT 0 1
postgres=# select * from test;
 id | name  | salary
----+-------+--------
  2 | LiShi |  12000
  1 | Hallo | 20000
(2 rows)


postgres=#\q
postgres@NanoPI-006:~$ ll data/recovery*
-rw-r--r-- 1 postgres postgres 5824 Apr 24 14:19 data/recovery.done
#The original recovery.conf automatically becomes recovery.done


2.6.5 Simulation At this time, the original main library has been repaired, you need to add the file recovery.conf
vi data/recovery.conf
standby_mode='on'
recovery_target_timeline = 'latest'
primary_conninfo='host= The IP address of the current main library (that is, the previous slave library) port=5432 user=username password=password for synchronization
' #For example: primary_conninfo='host=10.10.10.206 port=5432 user=repl password=repl1234'


#Save After starting the original main library, it becomes the status of the current standby library
postgres@NanoPI-005:~$ pg_ctl -D data -l log/pglog.log start
waiting for server to start.... done
server started #View


just started The original master database status has become a standby database
postgres@NanoPI-005:~$ pg_controldata | grep 'Database cluster state'
Database cluster state: in archive recovery #Check


whether the data in the new standby database is the same as the content of the current main database, versions after 9.6 should automatically synchronize the differences
postgres@NanoPI -005:~$ psql
psql (10.3)
Type "help" for help.


postgres=# \d
        List of relations
 Schema | Name | Type | Owner
--------+------+-- -----+----------
 public | test | table | postgres
(1 row)


postgres=# select * from test;
 id | name | salary
----+----- --+
  ------- 2 | LiShi | 12000
  1 | Hallo | 20000
(2 rows)



Guess you like

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