postgresql 11.6 from the main deployment deployment (archive mode)

Environment:
the OS: CentOs. 7
Postgres-11.6

1. Installation Step
1.1 deployment environment
database deployment
node ip role
Host01 192.168.1.130 master
Host02 192.168.1.131 from

1.2 equivalent connecting configuration
because we need to transfer secret scp Free primary database archive logs preparation of the library to
an equivalent configuration can be connected to the reference
https://www.cnblogs.com/hxlasky/p/12204180.html

1.3 master repository installation
1.3.1 preparation installation media
Download: https://www.postgresql.org/ftp / source /
me download version 11.6, the following media:
PostgreSQL-11.6.tar.gz

download the source code here, the following steps are source installation
1.3.2 install dependencies
yum the install the readline
yum the install GCC
yum the install -Y - the readline devel-Y
yum the install zlib-devel


1.3.3 compiled and installed
[root @ localhost Soft] # tar -xvf PostgreSQL-11.6.tar.gz
[root @ localhost Soft] #mkdir -p /opt/postgresql-11.6
[root @ localhost Soft] # cd PostgreSQL-11.6
[ Soft @ localhost the root] #. / Configure --prefix = / opt / PostgreSQL-11.6
[the root @ localhost Soft] #make
[the root @ localhost Soft] #make the install


1.3.4 Create a corresponding user
[root @ localhost opt] # Postgres groupadd
[root @ localhost opt] # useradd -g Postgres Postgres

1.3.5 to create data and log directory, and do the appropriate authorization
[root @ localhost Soft] #mkdir -p /opt/postgresql-11.6/{data,log}
[ Soft @ localhost the root] -R & lt #chown Postgres: Postgres /opt/postgresql-11.6

1.3.6 database initialization
#su - Postgres
[Postgres @ localhost bin] $ CD /opt/postgresql-11.6/bin
[Postgres @ localhost bin] $ ./initdb -D /opt/postgresql-11.6/data/


1.3.7 start database
[Postgres @ localhost bin] $ cd /opt/postgresql-11.6/bin
[Postgres @ localhost bin] $. / pg_ctl -D /opt/postgresql-11.6/data/ the -l /opt/postgresql-11.6/log/postgres.log Start


1.3.8 modify environment variables
[Postgres @ localhost ~] $ More .bash_profile
# .bash_profile

# the Get at The Functions and aliases
IF [-f ~ / .bashrc]; the then
        ~ / .bashrc.
Fi

# and the User Environment specific Startup Programs

the PATH the PATH = $: $ the HOME / .local / bin: $ the HOME / bin: / opt / postgresql- 11.6 / bin

Export the PATH

1.3.9 landed use
[Postgres @ localhost bin] $ cd /opt/postgresql-11.6/bin
[Postgres @ localhost bin] $ ./psql
psql (11.6)
. Type "Help" Help for

Postgres = # \ du
                                   List of the Roles
 Role name | the Attributes | Member of
----------- ---------------- + -------------------------------------------- + ----- ------
 postgres | Superuser, the create Role, the create DB, Replication, Bypass RLS | {}

1.3.10 postgres user to modify the access password and building a database test table construction
PostgreSQL database user database is created by default as a postgres database the administrator, the default password is blank, we need to modify the specified password, set here to Postgres.
su - Postgres
psql
# Postgres the ALTER the USER the WITH pASSWORD 'Postgres';
# pg_shadow from the SELECT *;
# the Create Database HXL;
# \ HXL C

Project # Create Table Person = (Integer ID, name text);
INSERT INTO Person = # Project values (. 1, 'HXL');
Project # = SELECT * from Person;

1.3.11 postgresql arranged to allow remote access
only need to modify postgresql.conf pg_hba.conf under directory data and the two files:
pg_hba.conf: configure access to the database;
postgresql.conf: configure the appropriate parameters PostgreSQL database server

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

# IPv4 local Connections:
Host All All 127.0.0.1/32 Trust
host all all 0.0.0.0/0 md5


reload the configuration file
SU - Postgres
pg_ctl -D /opt/postgresql-11.6/data reload


modify the postgresql.conf
Vim /opt/postgresql-11.6/data/postgresql.conf

the listen_addresses = '*' # what IP address (es) to listen on;
modify the parameter change requires restart

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

here in accordance with the main library has been good, a main library following configurations



1.3.12 master-slave configuration
to create a synchronization account 1.3.12.1
[the Data Postgres @ localhost] $ psql
psql (11.6)
Type "Help" for Help.

Postgres = Replication Login the ROLE the repl the CREATE # ENCRYPTED password 'the repl';
the CREATE the ROLE

1.3.12.2 modify the configuration file (the pg_hba.conf)


was added last two lines in the following documents:
Host 192.168.1.0/24 MD5 the repl Replication
Host All the repl 192.168.1.0/ Trust 24


1.3.12.3 modify the configuration file (the postgresql.conf)
to find the corresponding parameters as follows modified
wal_level = replica ## is the main set of the host wal
ON = archive_mode

the archive_command = '192.168.1.131 Test 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 = ## can be provided up to the several streams replication connection, almost several slave, is set a few
wal_keep_segments = 10240 ## set the maximum number of streams xlog copy reserved
wal_sender_timeout = 60s ## disposed replication host transmission stream timeout to


1.3.12.4 create an archive log directory

mkdir -p /opt/postgresql-11.6/data/pg_archive


1.3.12.5 the restart of the main library
pg_ctl -D /opt/postgresql-11.6/data/ -l /opt/postgresql-11.6 the restart /log/postgres.log

1.3.12.6 the check whether to transmit to the archive library prepared
execute commands on the primary library
psql -c "select pg_switch_wal ()"
or run

postgres = # select pg_switch_wal ();

see has been generated 2 new log file
corresponding transmission log file to the backup repository
 

 



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中.

Guess you like

Origin www.cnblogs.com/hxlasky/p/12208561.html