CentOS7.6下PostgreSQL主从流部署手册

目录

1、环境规划... 1

2、安装pg专用yum源(双节点)... 1

3、安装pg客户端(双节点)... 2

4、安装pgserver(双节点)... 2

5、初始化数据库(双节点)... 2

6、启动服务(双节点)... 3

7、配置postgres用户环境变量(双节点)... 3

8、改配置文件postgresql.conf(主库)... 3

9、pg_ctl启动pg服务(主库)... 4

10、改用户密码(主库)... 4

11、配置PostgreSQL-MD5认证(主库)... 4

12、配置PostgreSQL-Configure TCP/IP(主库)... 5

13、重启pg服务(主库)... 5

14、配置流复制(主库)... 5

15、创建流复制账号(主库)... 6

16、修改配置文件pg_hba.conf. 6

17、停止备库的PostgreSQL服务(备库)... 6

18、清空备库之前的数据文件(备库)... 6

19、恢复备库(备库)... 6

20、从库上修改postgresql.conf(备库)... 7

21、从库增加recovery.conf配置文件(从库)... 7

22、启动PostgreSQL服务... 7

23、验证配置... 8

23.1、主库检查复制进程... 8

23.2、备库检查复制进程... 8

23.3、主库上建表和插入数据... 9

23.4、从库上验证是否有对应表和数据... 9

 

1、环境规划

ip

os

role

10.45.11.177

CentOS7.6

master

10.45.11.178

CentOS7.6

slave

 

2、安装pg专用yum源(双节点)

Install the repository RPM:

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

 

3、安装pg客户端(双节点)

Install the client packages:

yum install postgresql11

 

4、安装pgserver(双节点)

Optionally install the server packages:

yum install postgresql11-server

 

集群版额外安装(双节点)

yum -y install postgresql11-contrib

 

5、初始化数据库(双节点)

Optionally initialize the database and enable automatic start:

/usr/pgsql-11/bin/postgresql-11-setup initdb

/usr/pgsql-11/bin/initdb -D $PGDATA

 

postgres@ossec01[/postgres]$/usr/pgsql-11/bin/initdb -D $PGDATA

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.

 

The database cluster will be initialized with locale "en_US.UTF-8".

The default database encoding has accordingly been set to "UTF8".

The default text search configuration will be set to "english".

 

Data page checksums are disabled.

 

fixing permissions on existing directory /postgres/data ... ok

creating subdirectories ... ok

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting default timezone ... Asia/Shanghai

selecting dynamic shared memory implementation ... posix

creating configuration files ... ok

running bootstrap script ... ok

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

 

WARNING: enabling "trust" authentication for local connections

You can change this by editing pg_hba.conf or using the option -A, or

--auth-local and --auth-host, the next time you run initdb.

 

Success. You can now start the database server using:

 

    /usr/pgsql-11/bin/pg_ctl -D /postgres/data -l logfile start

 

postgres@ossec01[/postgres]$

 

6、启动服务(双节点)

systemctl enable postgresql-11

systemctl start postgresql-11

 

7、配置postgres用户环境变量(双节点)

export PGHOME=/usr/pgsql-11

export PGDATA=/postgres/data

export PATH=$PATH:$HOME/bin:$PGHOME/bin

使用rpm包安装,会自动创建postgres用户。如果用源码包安装,需要自行创建postgres用户。

 

8、改配置文件postgresql.conf(主库)

配置文件就在data目录

postgres@ossec01[/postgres/data]$ls

base          pg_dynshmem    pg_logical    pg_replslot   pg_stat      pg_tblspc    pg_wal                postgresql.conf

global        pg_hba.conf    pg_multixact  pg_serial     pg_stat_tmp  pg_twophase  pg_xact

pg_commit_ts  pg_ident.conf  pg_notify     pg_snapshots  pg_subtrans  PG_VERSION   postgresql.auto.conf

postgres@ossec01[/postgres/data]$pwd

/postgres/data

 

postgres@ossec01[/postgres/data]$vi postgresql.conf

data_directory = '/postgres/data'

unix_socket_directories = '/postgres/data, /tmp'

log_directory = '/postgres/logs'

 

9、pg_ctl启动pg服务(主库)

postgres@ossec01[/postgres/data]$pg_ctl start

waiting for server to start....2019-09-12 19:54:50.744 CST [8809] LOG:  listening on IPv6 address "::1", port 5432

2019-09-12 19:54:50.744 CST [8809] LOG:  listening on IPv4 address "127.0.0.1", port 5432

2019-09-12 19:54:50.770 CST [8809] LOG:  listening on Unix socket "/postgres/data/.s.PGSQL.5432"

2019-09-12 19:54:50.772 CST [8809] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"

2019-09-12 19:54:50.782 CST [8809] LOG:  redirecting log output to logging collector process

2019-09-12 19:54:50.782 CST [8809] HINT:  Future log output will appear in directory "/postgres/logs".

 done

server started

 

10、改用户密码(主库)

postgres@ossec01[/postgres/data]$psql

psql (11.5)

Type "help" for help.

 

postgres=# alter user postgres with encrypted password 'postgres';

ALTER ROLE

postgres=#

 

 

11、配置PostgreSQL-MD5认证(主库)

vi /postgres/data/pg_hba.conf

最后加入如下配置:

host    all     all     0.0.0.0/0       md5

 

12、配置PostgreSQL-Configure TCP/IP(主库)

默认情况下,TCP/IP连接是不可行的,所以其他计算机用户不能连接到postgresql。

修改文件:vi /postgres/data/postgresql.conf 增加如下配置:

vi /postgres/data/postgresql.conf

listen_addresses = '*'

port = 5432

max_connections = 500

 

13、重启pg服务(主库)

postgres@ossec02[/postgres]$pg_ctl restart

waiting for server to shut down.... done

server stopped

waiting for server to start....2019-09-16 19:06:24.896 CST [7931] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2019-09-16 19:06:24.896 CST [7931] LOG:  listening on IPv6 address "::", port 5432

2019-09-16 19:06:24.898 CST [7931] LOG:  listening on Unix socket "/postgres/data/.s.PGSQL.5432"

2019-09-16 19:06:24.901 CST [7931] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"

2019-09-16 19:06:25.008 CST [7931] LOG:  redirecting log output to logging collector process

2019-09-16 19:06:25.008 CST [7931] HINT:  Future log output will appear in directory "/postgres/logs".

 done

server started

 

14、配置流复制(主库)

至此前期准备工作已经完成。

下面就是本文的重点了,主备流复制的配置。

vi /postgres/data/postgresql.conf

在最后添加如下属性:

wal_level = hot_standby

#checkpoint_segments = 16      #该配置项在11.5版本中不存在,否则报错

checkpoint_timeout = 5min

archive_mode = on

archive_command ='cp -i %p /postgres/data/arch/%f

max_wal_senders = 3

wal_keep_segments = 16

 

15、创建流复制账号(主库)

创建具有replication权限的用户

postgres@ossec02[/postgres/data]$psql

psql (11.5)

Type "help" for help.

 

postgres=# create user rep replication login encrypted password 'postgres';

CREATE ROLE

postgres=#

 

16、修改配置文件pg_hba.conf

增加复制属性

vi /postgres/data/pg_hba.conf

host    replication     rep     0.0.0.0/0       md5

 

17、停止备库的PostgreSQL服务(备库)

postgres@ossec02[/postgres/data]$pg_ctl stop

waiting for server to shut down..... done

server stopped

 

18、清空备库之前的数据文件(备库)

postgres@ossec02[/postgres/data]$rm -rf /postgres/data/*

 

19、恢复备库(备库)

从主库上恢复数据,输入下面命令,密码是之前配置的rep复制用户的密码

在从库上执行的,-h后面跟的ip是主库的ip

pg_basebackup -D /postgres/data/ -F p -X stream -v -P -h 10.45.11.177 -U rep

postgres@ossec02[/postgres/data]$pg_basebackup -D /postgres/data/ -F p -X stream -v -P -h 10.45.11.177 -U rep

Password:

pg_basebackup: initiating base backup, waiting for checkpoint to complete

WARNING:  skipping special file "./.s.PGSQL.5432"

pg_basebackup: checkpoint completed

pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1

pg_basebackup: starting background WAL receiver

pg_basebackup: created temporary replication slot "pg_basebackup_8551"

WARNING:  skipping special file "./.s.PGSQL.5432"000000010000000000000001)

40934/40934 kB (100%), 1/1 tablespace                                        

pg_basebackup: write-ahead log end point: 0/2000130

pg_basebackup: waiting for background process to finish streaming ...

pg_basebackup: base backup completed

 

20、从库上修改postgresql.conf(备库)

vi /postgres/data/postgresql.conf

增加如下配置项:

hot_standby = on

 

21、从库增加recovery.conf配置文件(从库)

从库增加recovery.conf配置文件:

vi /postgres/data/recovery.conf

standby_mode = on

primary_conninfo = 'host=10.45.11.177 port=5432 user=rep password=postgres'

trigger_file = '/postgres/data/postgresql.trigger.5432'

 

chmod 777 /postgres/data/recovery.conf

 

22、启动PostgreSQL服务

postgres@ossec02[/postgres/data]$pg_ctl start

waiting for server to start....2019-09-16 23:44:18.715 CST [8746] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2019-09-16 23:44:18.715 CST [8746] LOG:  listening on IPv6 address "::", port 5432

2019-09-16 23:44:18.717 CST [8746] LOG:  listening on Unix socket "/postgres/data/.s.PGSQL.5432"

2019-09-16 23:44:18.719 CST [8746] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"

2019-09-16 23:44:19.034 CST [8746] LOG:  redirecting log output to logging collector process

2019-09-16 23:44:19.034 CST [8746] HINT:  Future log output will appear in directory "/postgres/logs".

 done

server started

 

23、验证配置

23.1、主库检查复制进程

在主库上检查wal进程,会看到多出一个wal sender process的进程

postgres@ossec01[/postgres/data]$ps -aux | grep postgres

root       7749  0.0  0.0 191772  2348 pts/0    S    18:41   0:00 su - postgres

postgres   7750  0.0  0.0 115436  2032 pts/0    S    18:41   0:00 -bash

postgres   8107  0.0  1.0 415068 29448 pts/0    S    19:31   0:00 /usr/pgsql-11/bin/postgres

postgres   8108  0.0  0.0 249356  1924 ?        Ss   19:31   0:00 postgres: logger  

postgres   8110  0.0  0.1 415316  3904 ?        Ss   19:31   0:00 postgres: checkpointer  

postgres   8111  0.0  0.1 415200  3604 ?        Ss   19:31   0:00 postgres: background writer 

postgres   8112  0.0  0.2 415068  6240 ?        Ss   19:31   0:05 postgres: walwriter  

postgres   8113  0.0  0.1 415836  3204 ?        Ss   19:31   0:00 postgres: autovacuum launcher 

postgres   8114  0.0  0.0 251476  2024 ?        Ss   19:31   0:00 postgres: archiver   last was 000000010000000000000002.00000028.backup

postgres   8115  0.0  0.0 251608  2192 ?        Ss   19:31   0:00 postgres: stats collector 

postgres   8116  0.0  0.0 415680  2808 ?        Ss   19:31   0:00 postgres: logical replication launcher 

postgres   8582  0.0  0.1 415844  3656 ?        Ss   23:44   0:00 postgres: walsender rep 10.45.11.178(26317) streaming 0/3000140

postgres   8586  0.0  0.0 155360  1880 pts/0    R+   23:45   0:00 ps -aux

postgres   8587  0.0  0.0 112708   972 pts/0    S+   23:45   0:00 grep --color=auto postgres

 

23.2、备库检查复制进程

在从库上有个walreceiver streaming的进程

postgres@ossec02[/postgres/data]$ps -aux | grep postgres

root       7824  0.0  0.0 191772  2348 pts/0    S    18:41   0:00 su - postgres

postgres   7825  0.0  0.0 115436  2016 pts/0    S    18:41   0:00 -bash

postgres   8746  0.0  1.0 415068 29336 pts/0    S    23:44   0:00 /usr/pgsql-11/bin/postgres

postgres   8747  0.0  0.0 249356  1920 ?        Ss   23:44   0:00 postgres: logger  

postgres   8748  0.0  0.0 415232  2788 ?        Ss   23:44   0:00 postgres: startup   recovering 000000010000000000000003

postgres   8749  0.0  0.1 415068  3360 ?        Ss   23:44   0:00 postgres: checkpointer  

postgres   8750  0.0  0.1 415068  3112 ?        Ss   23:44   0:00 postgres: background writer 

postgres   8751  0.0  0.0 251476  1788 ?        Ss   23:44   0:00 postgres: stats collector 

postgres   8752  0.1  0.1 421908  3668 ?        Ss   23:44   0:01 postgres: walreceiver   streaming 0/3000140

postgres   8757  0.0  0.0 155360  1880 pts/0    R+   23:54   0:00 ps -aux

postgres   8758  0.0  0.0 112708   972 pts/0    S+   23:54   0:00 grep --color=auto postgres

 

23.3、主库上建表和插入数据

postgres@ossec01[/postgres/data]$psql

psql (11.5)

Type "help" for help.

 

postgres=# \c

You are now connected to database "postgres" as user "postgres".

postgres=# \d

Did not find any relations.                     ^

postgres=# create table tmp_list_001(id int ,name varchar(10));

CREATE TABLE

postgres=# insert into tmp_list_001 values(1,'haha');

INSERT 0 1

postgres=#

 

23.4、从库上验证是否有对应表和数据

postgres@ossec02[/postgres/logs]$psql

psql (11.5)

Type "help" for help.

 

postgres=# \d

            List of relations

 Schema |     Name     | Type  |  Owner  

--------+--------------+-------+----------

 public | tmp_list_001 | table | postgres

(1 row)

 

postgres=# select * from tmp_list_001 ;

 id | name

----+------

  1 | haha

(1 row)

 

postgres=#

测试完成。

你可能感兴趣的:(centos7,postgresql,11)