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 </dev/null'

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=#

測試完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章