主な展開の展開からのpostgresql 11.6(アーカイブモード)

環境:
OS:CentOSの7つ
のPostgres-11.6

1.インストール手順
1.1デプロイメント環境
データベース展開
ノードのIP役割
Host01 192.168.1.130マスター
Host02 192.168.1.131から

1.2と同等の接続設定
我々は秘密SCP無料、プライマリ・データベースのアーカイブ・ログを転送する必要があるためライブラリの準備
同等の構成を参照に接続することができます
https://www.cnblogs.com/hxlasky/p/12204180.html

1.3マスターリポジトリのインストール
1.3.1準備インストールメディアの
ダウンロード:https://www.postgresql.org/ftp /ソース/
私は、次のメディアをバージョン11.6をダウンロード:
PostgreSQLは、11.6.tar.gz

ここにソースコードをダウンロードし、次のステップは、ソースインストールされている
1.3.2依存関係をインストール
yumをインストールreadlineの
インストールGCC yumを
インストール-Y yumを- readlineのdevel-Y
yumをインストールしたzlib-develの


1.3.3コンパイルとインストール
[ルート@ localhostのソフト]#タール-xvf PostgreSQLの-11.6.tar.gz
[ルート@ localhostのソフト] #mkdir -p /opt/postgresql-11.6
[ルート@ localhostのソフト]#CDのPostgreSQL-11.6
[ソフト@ localhostのルート]#。/設定--prefix =は/ opt / PostgreSQLの-11.6
[ルート@ localhostのソフト] #make
[ルート@ localhostのソフト] #makeインストール


対応するユーザ作成1.3.4を
[ルートを@ localhostのオプトイン]# groupaddのPostgresの
[ルート@ localhostのオプトイン]#useraddの-gのPostgresのPostgres

1.3.5データを作成し、ディレクトリを記録し、適切な承認を行うには
、[ローカルホストソフト@ルート] #mkdir -p /opt/postgresql-11.6/{data,log}
[ソフト@ localhostのルート] -R&LT #chownのPostgres:Postgresの/opt/postgresql-11.6

1.3.6データベースの初期化
#su - Postgresの
[Postgresの@ localhostのビン] $ CDの/opt/postgresql-11.6/bin
[Postgresの@ localhostのビン] $ ./initdb -D /opt/postgresql-11.6/data/


1.3.7データベースを開始
/opt/postgresql-11.6/bin [Postgresの@ localhostのビン] $のCDを
[Postgresの@ localhostのビン] $。 /にpg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.logスタート


1.3.8の変更、環境変数
[Postgresの@ localhostの〜] $よりの.bash_profile
#.bash_profileの

で#取得関数とエイリアス
IF [-f〜/ .bashrcの];その後、
        〜/ .bashrcに。
Fi回線

#とユーザー環境固有のスタートアッププログラム

PATH PATH = $:$ HOME / .localの/ binに:$ HOME / binにします。/ opt / postgresql- 11.6 / binに

エクスポートPATH

1.3.9は、使用を上陸させた
/opt/postgresql-11.6/bin [Postgresの@ localhostのビン] $のCD
[Postgresの@ localhostのビン] $ ./psql
のpsql(11.6)
。のための「ヘルプ」のヘルプを入力

のPostgres =#\デュ
                                   役割のリスト
 役割名を|属性|会員の
----------- ---------------- + -------------------------------------------- + ----- ------
 はpostgres |スーパーユーザーは、役割を作成し、DB、複製、バイパスRLSを作成| {}

1.3.10 postgresのアクセスパスワードを変更するには、ユーザとデータベースのテストテーブルの建設建物
のPostgreSQLデータベースユーザデータベースはPostgresデータベースとしてデフォルトで作成されます管理者は、デフォルトのパスワードは空白になって、私たちはPostgresのためにここに集合、指定したパスワードを変更する必要があります。
SU - Postgresの
PSQL
#PostgresのALTER PASSWORD 'Postgresの' WITH USER;
SELECTから#1 pg_shadowシステム*;
データベースの作成HXL;
#\ HXL C

プロジェクト#は表人=(整数ID、名前のテキスト)を作成します。
INSERT INTO人=#プロジェクト値(1、「HXL」);
プロジェクト位=人からSELECT *;

1.3.11 PostgreSQLは、リモートアクセスを許可するように配置された
唯一のディレクトリデータと二つのファイルの下にあるpostgresql.confにpg_hba.confを変更する必要があります。
pg_hba.confの:データベースへのアクセスを設定します。
postgresql.confの:設定するには、適切なパラメータPostgreSQLデータベースサーバ

のvim /opt/postgresql-11.6/data/pg_hba.conf

#IPv4のローカル接続:
ホストすべてすべて127.0.0.1/32トラスト
ホストすべてすべて0.0.0.0/0 md5の


リロード設定ファイル
SU - Postgresの
にpg_ctl -D /opt/postgresql-11.6/dataリロード


postgresql.confの変更
/opt/postgresql-11.6/data/postgresql.conf Vimを

のlisten_addresses = '*' #IPアドレス(複数可)をするためにどのように耳を傾け、
変更パラメータの変更は、再起動が必要

-l /opt/postgresql-11.6/log/postgres.log STOPにpg_ctl -D /opt/postgresql-11.6/data
にpg_ctl -D /opt/postgresql-11.6/data -l /opt/postgresql-11.6/log/postgres。起動ログ

の設定以下となっている主なライブラリーに基づいて、ここで良い、メインライブラリを



1.3.12マスター・スレーブ構成
同期アカウント1.3.12.1を作成するための
[データのPostgres @ localhostの] $ psqlの
PSQL(11.6)
ヘルプのための「ヘルプ」と入力

Postgresの=レプリケーションログインROLE#暗号化されたパスワード「REPL」を作成REPL;
ROLE CREATE

1.3.12.2は、設定ファイル(pg_hba.confの)を変更し


、以下の文献に最後の2行が追加されました:
ホスト192.168.1.0/24 MD5 REPLレプリケーション
ホストすべてのREPLを192.168.1.0/トラスト24


1.3.12.3設定ファイル(postgresql.confの)を変更し
、次のように変形対応するパラメータを検索することが
wal_level =レプリカ##は、ホストWALのメインセットであります
= ONにarchive_mode

にarchive_command = '192.168.1.131テストSSH!-f /opt/postgresql-11.6/data/pg_archive/%f && P 192.168.1.131:/opt/postgresql-11.6/data/pg_archive/%f% SCP'

max_wal_sendersを6 = ##複数のストリームの複製接続、ほとんど複数のスレーブまで提供することができ、設定されているいくつかの
wal_keep_segments = 10240 ##設定された予約コピーxlogのストリームの最大数
wal_sender_timeout = 60秒を##に配置された複製ホスト伝送ストリームタイムアウト


1.3.12.4アーカイブログディレクトリを作成

ます。mkdir -p /opt/postgresql-11.6/data/pg_archive


1.3.12.5メインライブラリの再起動
にpg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6を再起動が/log/postgres.log

アーカイブ・ライブラリを用意するかどうかを送信する1.3.12.6チェックを
一次ライブラリー上でコマンドを実行
にpsql -c「を選択pg_switch_wal()」
または実行

のpostgres =#選択pg_switch_walを();

参照が生成されています2新しいログファイル
のバックアップリポジトリに対応する送信ログファイル
 

 



1.4  从库安装

1.4.1 安装
从库的安装跟主库安装步骤一致,需要启动数据库



1.4.2 停掉从库
若从库的数据库已经在运行的话,事先将其停掉
[postgres@localhost data]$ pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log stop
waiting for server to shut down.... done
server stopped




1.4.3 准备data目录
从库安装完成后,不初始化,若已经初始化,删除其data目录
若之前安装的pg有data目录的话需要将其删除掉,并创建一个空的相同的目录
su - postgres
[postgres@localhost postgresql-11.6]$ cd /opt/postgresql-11.6
[postgres@localhost postgresql-11.6]$ mv data bakdata
[postgres@localhost postgresql-11.6]$ mkdir data


root用户下修改权限
chown -R postgres:postgres /opt/postgresql-11.6
chmod 0700 /opt/postgresql-11.6/data

1.4.4 基础同步主库的数据文件
[postgres@localhost postgresql-11.6]$ pg_basebackup -RF p --progress -D /opt/postgresql-11.6/data -h 192.168.1.130 -p 5432 -U repl --password
Password:
113625/113625 kB (100%), 1/1 tablespace

可以看到data目录下的所有文件都同步过来了,使用了R参数会生成一个recovery.conf文件,下面我们直接修改该文件即可

[postgres@localhost data]$ pwd
/opt/postgresql-11.6/data
[postgres@localhost data]$ ls -al
[postgres@localhost data]$ ls -1
backup_label
base
global
pg_archive
pg_commit_ts
pg_dynshmem
pg_hba.conf
pg_ident.conf
pg_logical
pg_multixact
pg_notify
pg_replslot
pg_serial
pg_snapshots
pg_stat
pg_stat_tmp
pg_subtrans
pg_tblspc
pg_twophase
PG_VERSION
pg_wal
pg_xact
postgresql.auto.conf
postgresql.conf
recovery.conf

1.4.4 修改recovery.conf文件
前面的步骤已经生成了recovery.conf文件,
该文件的内容是这样的
[postgres@localhost data]$ more recovery.conf
standby_mode = 'on'
primary_conninfo = 'user=repl password=repl host=192.168.1.130 port=5432 sslmode=disable sslcompression=0 target_session_attrs=any'
我们这里不做流复制, primary_conninfo先注释掉,修改该文件,内容如下:
restore_command = 'cp /opt/postgresql-11.6/data/pg_archive/%f %p'
standby_mode = on


1.4.5 修改从库postgresql.conf文件
修改如下内容项:
max_connections = 1000 #一般查多于写的应用从库的最大连接数要比较大
hot_standby = on       #说明这台机器不仅仅是用于数据归档,也用于数据查询
max_standby_streaming_delay = 30s  #数据流备份的最大延迟时间
wal_receiver_status_interval = 1s  #多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on          #如果有错误的数据复制,是否向主进行反馈



1.4.5 启动从库

pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6/log/postgres.log start



1.4.6    验证
1.4.6.1 查看同步情况
[postgres@localhost pg_wal]$ psql
postgres=# \x
Expanded display is on.
postgres=#  select * from pg_stat_replication;
(0 rows)

这里不是流复制,所有没有显示
1.4.6.2 主库尝试创建对象看是否同步到从库
psql -h localhost -U uhxl -d hxl
create table tb_test
(
 id bigserial primary key not null,
 name varchar(64)
);

insert into tb_test(name) values('name1');
insert into tb_test(name) values('name2');
insert into tb_test(name) values('name3');
insert into tb_test(name) values('name4');
insert into tb_test(name) values('name5');


查看从库是否同步


这种归档模式的同步需要主库上执行检查点切换后,主库的数据才会同步到从库
或是手工进行检查点的切换
select pg_switch_wal();

为啥要切换生成日志,因为架设的是基于文件的备库,只有归档传输到备库后才会应用,否则记录还在主库的xlog中.

おすすめ

転載: www.cnblogs.com/hxlasky/p/12208561.html