repmgr 구성 백업 데이터베이스에 오류를 처리하는 파일의 예가 있습니다.

배경

프로덕션에서는 repmgr을 사용하여 PostgreSQL 데이터베이스 용 고 가용성 클러스터를 만들었습니다. 프로덕션에서 대기 복제본이 고 가용성 데이터베이스로 변환 된 것으로 확인되면 오류가보고되고 데이터베이스를 재현 할 수 없습니다. 오류 내용은 다음과 같습니다.
repmgr 구성 백업 데이터베이스에 오류를 처리하는 파일의 예가 있습니다.

이유

PG와 pg_basebackup에 더 익숙한 학생들은 스스로 해결책을 찾을 수 있고 계속 읽을 필요가 없기 때문에 그 이유에 대해 먼저 이야기하겠습니다. 그 이유는 생성 된 독립 테이블 스페이스의 지정된 디렉토리가 $ PGDATA 디렉토리에 위치하기 때문입니다. repmgr의 대기 복제본은 pg_basebackup을 호출하고 출력 형식이 지정되지 않았습니다. 기본값은 일반입니다. 메인 라이브러리 디렉토리가 복사되면, PGDATA 디렉토리, 디렉토리 및 독립 테이블 스페이스 디렉토리의 모든 파일이므로 파일이 존재 함 오류가보고 됩니다.

해결책

  1. 메인 라이브러리의 독립 테이블 스페이스를 PGDATA 이외의 디렉토리로 마이그레이션 (쓰기가 차단됨)
  2. 새 디렉터리를 대기 복제로 지정하고 복제가 완료된 후 파일을 실제 PGDATA 디렉터리로 이동합니다.

첫 번째 솔루션은 기본 라이브러리에 대한 작업을 포함하므로 응용 프로그램에 미치는 영향에 신경 쓰지 않는 한 프로덕션 작업을 수행하지 않는 것이 좋습니다.

테스트 계획 2

노드 1 작업

테이블 스페이스 추가, 데이터베이스 생성, 테이블 쓰기

postgres=# create user pguser login  password 'pguser';
CREATE ROLE
postgres=# create tablespace tbs_mydb owner pguser location '/home/postgres/data/pg_tbs/tbs_mydb';
WARNING:  tablespace location should not be inside the data directory
CREATE TABLESPACE
postgres=# create  database mydb with owner=pguser template=template0 encoding='UTF8' tablespace =tbs_mydb;
CREATE DATABASE
postgres=# grant all on database mydb to pguser with grant option;
GRANT
postgres=# grant all on tablespace tbs_mydb to pguser;
GRANT
postgres=# \c mydb pguser
You are now connected to database "mydb" as user "pguser".
mydb=> create table t1 (id int);
CREATE TABLE
mydb=> insert into t1 values(1);
INSERT 0 1
mydb=> select * from t1;
 id 
----
  1
(1 row)

노드 2 작업

처음으로 대기 클론을 시도했을 때 프로덕션과 일치 하는 오류 보고서가 있었고 오류 메시지가 프로덕션과 일치했습니다.

INFO: checking and correcting permissions on existing directory "/home/postgres/data"
NOTICE: starting backup (using pg_basebackup)...
HINT: this may take some time; consider using the -c/--fast-checkpoint option
INFO: executing:
  /usr/local/pgsql/bin/pg_basebackup -l "repmgr base backup"  -D /home/postgres/data -h 192.168.56.111 -p 6000 -U repmgr -X stream 
pg_basebackup: could not create directory "/home/postgres/data/pg_tbs": File exists
pg_basebackup: removing contents of data directory "/home/postgres/data"
pg_basebackup: changes to tablespace directories will not be undone
ERROR: unable to take a base backup of the source server
HINT: data directory ("/home/postgres/data") may need to be cleaned up manually

repmgr.conf에서 data_directory = '/ home / postgres / repmgr'수정

대기 클론을 다시 시도하십시오. 성공

[postgres@repmgr2 ~]$ repmgr -h 192.168.56.111 -U repmgr -d repmgr -f ~/repmgr.conf standby clone -p6000 
NOTICE: destination directory "/home/postgres/repmgr" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.56.111 user=repmgr port=6000 dbname=repmgr
DETAIL: current installation size is 45 MB
DEBUG: 1 node records returned by source node
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.111 port=6000 fallback_application_name=repmgr options=-csearch_path="
DEBUG: upstream_node_id determined as 111
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
NOTICE: checking for available walsenders on the source node (2 required)
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: checking and correcting permissions on existing directory "/home/postgres/repmgr"
NOTICE: starting backup (using pg_basebackup)...
HINT: this may take some time; consider using the -c/--fast-checkpoint option
INFO: executing:
  /usr/local/pgsql/bin/pg_basebackup -l "repmgr base backup"  -D /home/postgres/repmgr -h 192.168.56.111 -p 6000 -U repmgr -X stream 
DEBUG: create_recovery_file(): creating "/home/postgres/repmgr/recovery.conf"...
DEBUG: recovery.conf line: standby_mode = 'on'

DEBUG: recovery.conf line: primary_conninfo = 'host=192.168.56.111 user=repmgr port=6000 application_name=repmgr2 connect_timeout=2'

DEBUG: recovery.conf line: recovery_target_timeline = 'latest'

NOTICE: standby clone (using pg_basebackup) complete
NOTICE: you can now start your PostgreSQL server
HINT: for example: pg_ctl -D /home/postgres/repmgr start
HINT: after starting the server, you need to register this standby with "repmgr standby register"

repmgr.conf를 원래 구성으로 수정하고 repmgr 디렉토리의 모든 파일을 데이터 디렉토리로 mv

data_directory='/home/postgres/data'

[postgres@repmgr2 repmgr]$ mv * ~/data/
mv: cannot move ‘pg_tbs’ to ‘/home/postgres/data/pg_tbs’: File exists

구성 파일에서 cluster_name 매개 변수를 수정하고 데이터베이스를 시작하십시오.

[postgres@repmgr2 data]$ pg_ctl -D /home/postgres/data/ start
waiting for server to start....2021-02-28 10:09:15.905 CST [3498] LOG:  listening on IPv4 address "0.0.0.0", port 6000
2021-02-28 10:09:15.912 CST [3498] LOG:  listening on Unix socket "/tmp/.s.PGSQL.6000"
2021-02-28 10:09:15.949 CST [3498] LOG:  redirecting log output to logging collector process
2021-02-28 10:09:15.949 CST [3498] HINT:  Future log output will appear in directory "log".
. done
server started

대기 데이터베이스를 성공적으로 등록했습니다.

[postgres@repmgr2 data]$ repmgr -f ../repmgr.conf standby register
INFO: connecting to local node "repmgr2" (ID: 113)
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.113 port=6000 fallback_application_name=repmgr options=-csearch_path="
INFO: connecting to primary database
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.111 port=6000 fallback_application_name=repmgr options=-csearch_path="
WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID 111)
INFO: standby registration complete
NOTICE: standby node "repmgr2" (ID: 113) successfully registered

클러스터 상태 확인

[postgres@repmgr2 data]$ repmgr -f ../repmgr.conf cluster show
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.113 port=6000 fallback_application_name=repmgr options=-csearch_path="
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.111 port=6000 fallback_application_name=repmgr options=-csearch_path="
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.113 port=6000 fallback_application_name=repmgr options=-csearch_path="
DEBUG: connecting to: "user=repmgr connect_timeout=2 dbname=repmgr host=192.168.56.111 port=6000 fallback_application_name=repmgr options=-csearch_path="
 ID  | Name    | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                        
-----+---------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------
 111 | repmgr1 | primary | * running |          | default  | 100      | 5        | host=192.168.56.111 port=6000 user=repmgr dbname=repmgr connect_timeout=2
 113 | repmgr2 | standby |   running | repmgr1  | default  | 100      | 5        | host=192.168.56.113 port=6000 user=repmgr dbname=repmgr connect_timeout=2

데이터 동기화 테스트

기본 라이브러리 테스트에 데이터 추가

mydb=> insert into t1 values(2);
INSERT 0 1
mydb=> select * from t1;
 id 
----
  1
  2
(2 rows)

라이브러리에서 쿼리

[postgres@repmgr2 data]$ psql
psql (10.11)
Type "help" for help.

postgres=# \c mydb pguser
You are now connected to database "mydb" as user "pguser".
mydb=> select * from t1;
 id 
----
  1
  2
(2 rows)

끝에 쓰기

사실, 독립적 인 테이블 스페이스를 생성 할 때 PG는 테이블 스페이스가 DATA 디렉토리에서 사용되지 않는다는 것을 이미 상기시켜 놓았으므로 위의 오류는 전임자의 구덩이에 빠지고 있습니다.

WARNING:  tablespace location should not be inside the data directory

옵션 1을 시도하고 싶다면 몇 가지 아이디어를 제공 할 수 있습니다.

#新创建一个表空间
postgres=# create tablespace zhijian  owner pguser location '/data/pgdata/11/pg_tbs/tbs_zhijian';
CREATE TABLESPACE
#更改数据库的表空间
mydb=> \c postgres postgres
postgres=# alter database mydb set  tablespace zhijian;
ALTER DATABASE

추천

출처blog.51cto.com/hbxztc/2641590