使用pg_basebackup搭建PostgreSQL流複製環境
接觸了PostgreSQL的高可用,在這裡先進行流複製實驗。
環境:
OS:
[ha@node0 ~]$ uname -a
Linux node0 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
記憶體:1G
CPU:1核
資料庫:
postgres=# select version();
version
--------------------------------------------------------------------------------
-------------------------------
PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120
313 (Red Hat 4.4.7-16), 64-bit
(1 row)
postgres=#
.bash_profile
[ha@node0 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PGHOME=/home/ha/pgdb/
export PGDATA=/home/ha/pgdb/data/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib/
export PATH=$PGHOME/bin:$PATH:$HOME/bin
搭建過程:
1.主庫配置
a.postgresql.conf:
listen_addresses = '*'
wal_level = hot_standby
archive_mode = off
max_wal_senders = 3
wal_keep_segments = 16
這裡要說明的是參數archive_mode,我在實驗過程中有一次發現參數archive_mode和archive_command在不設定的情況下也可以完成流複製,所以我對此進行了調研,發現francs已經對此進行瞭解釋,這裡引用francs的結論:
“ 在搭建流複製環境時,並不必須設定 archive_mode 參數為 on ,很多資料在介紹搭建流複製環境時設定這個參數為 on ,可能是出於開啟 WAL 歸檔更安全的原因,因為在主庫宕機並且較長時間不能恢複時,從庫依然可以讀取歸檔目錄的 WAL,從而保證不丟資料; 另一方面,如果主庫設定了較大的 wal_keep_segments ,也可以不用開啟archive_mode,因為主庫保留了足夠的 WAL,從而大大降低了因從庫所需要的 WAL 被主庫覆蓋而需要從歸檔去取 WAL 的情況。所以從這方面說,archive_mode 參數的設定與搭建流複製並沒有直接關係。 提示: 對於比較繁忙的庫,在搭建流複製從庫時,建議主庫設定較大的 wal_keep_segments 參數。 ”
b.pg_hba.conf
host all all 192.168.238.0/24 md5
host replication rep 192.168.238.0/24 trust
這裡說明的是,網上有其他對replication設定為md5,並建立.pgpass檔案,這種方法也是可行的。
c.建立使用者rep
create user rep replication encrypted password 'rep';
2.使用pg_basebackup進行從庫備份
[ha@localhost pgdb]$ pg_basebackup -D $PGDATA -F p -X stream -v -P -h 192.168.238.130 -U rep
transaction log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
20945/20945 kB (100%), 1/1 tablespace
transaction log end point: 0/20000F0
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
[ha@localhost pgdb]$ ll
total 20
drwxrwxr-x. 2 ha ha 4096 Dec 26 00:54 bin
drwx------. 18 ha ha 4096 Dec 26 12:02 data
drwxrwxr-x. 4 ha ha 4096 Dec 26 00:54 include
drwxrwxr-x. 4 ha ha 4096 Dec 26 00:54 lib
drwxrwxr-x. 4 ha ha 4096 Dec 26 00:54 share
[ha@localhost pgdb]$ cd data/
[ha@localhost data]$ ls
backup_label pg_hba.conf pg_replslot pg_subtrans postgresql.auto.conf
base pg_ident.conf pg_serial pg_tblspc postgresql.conf
global pg_logical pg_snapshots pg_twophase
pg_clog pg_multixact pg_stat PG_VERSION
pg_dynshmem pg_notify pg_stat_tmp pg_xlog
這裡需要對目錄進行一下說明:data目錄可以通過pg_basebackup自動建立,但是其他資料表空間,需要手動建立並賦權,這裡需要注意的是,許可權為700。
3.從庫配置
a.postgresql.conf
hot_standby = on
b.recovery.conf
standby_mode = on
primary_conninfo = 'host=192.168.238.130 port=5432 user=rep'
trigger_file = '/home/ha/pgdb/pg.trigger.file'
4.備庫啟動前,主庫進程
[ha@localhost pgdb]$ ps -ef | grep post
root 2124 1 0 04:47 ? 00:00:00 /usr/libexec/postfix/master
postfix 2147 2124 0 04:47 ? 00:00:00 qmgr -l -t fifo -u
postfix 10385 2124 0 11:27 ? 00:00:00 pickup -l -t fifo -u
ha 10691 1 0 12:33 pts/3 00:00:00 /home/ha/pgdb/bin/postgres_ora
ha 10693 10691 0 12:33 ? 00:00:00 postgres: checkpointer process
ha 10694 10691 0 12:33 ? 00:00:00 postgres: writer process
ha 10695 10691 0 12:33 ? 00:00:00 postgres: wal writer process
ha 10696 10691 0 12:33 ? 00:00:00 postgres: autovacuum launcher process
ha 10697 10691 0 12:33 ? 00:00:00 postgres: stats collector process
ha 10717 4087 0 12:37 pts/3 00:00:00 grep post
5.備庫啟動後
a.主庫進程
[ha@localhost pgdb]$ ps -ef | grep post
root 2124 1 0 04:47 ? 00:00:00 /usr/libexec/postfix/master
postfix 2147 2124 0 04:47 ? 00:00:00 qmgr -l -t fifo -u
postfix 10385 2124 0 11:27 ? 00:00:00 pickup -l -t fifo -u
ha 10691 1 0 12:33 pts/3 00:00:00 /home/ha/pgdb/bin/postgres_ora
ha 10693 10691 0 12:33 ? 00:00:00 postgres: checkpointer process
ha 10694 10691 0 12:33 ? 00:00:00 postgres: writer process
ha 10695 10691 0 12:33 ? 00:00:00 postgres: wal writer process
ha 10696 10691 0 12:33 ? 00:00:00 postgres: autovacuum launcher process
ha 10697 10691 0 12:33 ? 00:00:00 postgres: stats collector process
ha 10718 10691 0 12:37 ? 00:00:00 postgres: wal sender process rep 192.168.238.131(59195) streaming 0/3000060
ha 10720 4087 0 12:37 pts/3 00:00:00 grep post
b.備庫進程
[ha@localhost data]$ ps -ef | grep post
root 2086 1 0 04:48 ? 00:00:00 /usr/libexec/postfix/master
postfix 2108 2086 0 04:48 ? 00:00:00 qmgr -l -t fifo -u
postfix 9657 2086 0 11:27 ? 00:00:00 pickup -l -t fifo -u
ha 9782 1 0 12:36 pts/2 00:00:00 /home/ha/pgdb/bin/postgres
ha 9783 9782 0 12:36 ? 00:00:00 postgres: startup process recovering 000000010000000000000003
ha 9784 9782 0 12:36 ? 00:00:00 postgres: checkpointer process
ha 9785 9782 0 12:36 ? 00:00:00 postgres: writer process
ha 9786 9782 0 12:36 ? 00:00:00 postgres: stats collector process
ha 9787 9782 0 12:36 ? 00:00:00 postgres: wal receiver process streaming 0/3000060
ha 9792 3744 0 12:37 pts/2 00:00:00 grep post
6.實驗效果
a.主庫
[ha@localhost pgdb]$ psql postgres
psql (9.4.5)
Type "help" for help.
postgres=# create table test(id int);
CREATE TABLE
postgres=# insert into test values (1),(2);
INSERT 0 2
postgres=# select * from test ;
id
----
1
2
(2 rows)
postgres=#
b.備庫
[ha@localhost data]$ psql postgres
psql (9.4.5)
Type "help" for help.
postgres=# select * from test ;
id
----
1
2
(2 rows)
------------------------------------華麗麗的分割線------------------------------------
Ubuntu Server 14.04 下安裝 PostgreSQL 9.3.5 資料庫
CentOS 6.3環境下yum安裝PostgreSQL 9.3
PostgreSQL緩衝詳述
Windows平台編譯 PostgreSQL
Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)環境的配置與安裝
Ubuntu上的phppgAdmin安裝及配置
CentOS平台下安裝PostgreSQL9.3
PostgreSQL配置Streaming Replication叢集
如何在CentOS 7/6.5/6.4 下安裝PostgreSQL 9.3 與 phpPgAdmin
------------------------------------華麗麗的分割線------------------------------------
PostgreSQL 的詳細介紹:請點這裡
PostgreSQL 的:請點這裡
本文永久更新連結地址: