磨礪技術珠磯,踐行數據之道,追求卓越價值
回到上一級頁面: PostgreSQL集群方案相關索引頁 回到頂級頁面:PostgreSQL索引頁
[作者 高健@博客園 luckyjackgao@gmail.com]
主要參考的是如下url:
http://www.rassoc.com/gregr/weblog/2013/02/16/zero-to-postgresql-streaming-replication-in-10-mins/
准備兩台機器,
master: 10.10.10.2
slave: 10.10.10.1
首先在 master機器上,建立一個名為replicator的用戶:
psql -c "CREATE USER replicator REPLICATION LOGIN ENCRYPTED PASSWORD 'thepassword';"
master機器上的 postgresql.conf,配置成這樣:
listen_address = # make sure we're listening as appropriate wal_level = hot_standby max_wal_senders = 3 checkpoint_segments = 8 wal_keep_segments = 8
然后在master的 pg_hba.conf文件,中,進行如下配置,添加一行,允許replicator用戶從遠端訪問:
host replication replicator 10.10.10.1/32 md5
然后,啟動master端的postgresql
然后,在slave端:
在slave端的postgresql停止的前提下,以postgres用戶身份,刪除data目錄:
rm -rf /usr/local/pgsql/data
然后,在slave端,執行pg_basebackup程序:
pg_basebackup -h 10.10.10.2 -D /usr/local/pgsql/data -U replicator -v -P
在執行完畢 pg_basebackup后,會得到一個從 master端拷貝到的/usr/local/pgsql/data目錄,
編輯其中的 postgresql.conf,把其standby_mode設置為on。
在slave端,編輯一個/usr/local/pgsql/data/recovery.conf文件,
內容如下:
standby_mode = 'on' primary_conninfo = 'host=10.10.10.2 port=5432 user=replicator password=thepassword sslmode=require' trigger_file = '/tmp/postgresql.trigger'
然后,在slave端,啟動postgresql:
[postgres@pg200 pgsql]$ ./bin/pg_ctl -D ./data start pg_ctl: another server might be running; trying to start server anyway server starting [postgres@pg200 pgsql]$ LOG: database system was interrupted while in recovery at log time 2013-09-27 17:28:27 CST HINT: If this has occurred more than once some data might be corrupted and you might need to choose an earlier recovery target. LOG: entering standby mode LOG: consistent recovery state reached at 0/5012F78 LOG: redo starts at 0/5012EE0 LOG: record with zero length at 0/5012F78 LOG: database system is ready to accept read only connections LOG: streaming replication successfully connected to primary
從log中,可以看到,postgresql的 streaming repliation開始工作了。
下面進行簡單的驗證:
master端,新增數據:
[postgres@pg200 ~]$ cd /usr/local/pgsql/
[postgres@pg200 pgsql]$ ./bin/psql psql (9.2.4) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres (3 rows) postgres=# \d List of relations Schema | Name | Type | Owner --------+------+-------+----------
public | test | table | postgres (1 row) postgres=# select * from test; id ----
1
2
3 (3 rows) postgres=# insert into test values(4); INSERT 0 1 postgres=#
slave端,可以看到數據:
[postgres@pg200 ~]$ cd /usr/local/pgsql/bin [postgres@pg200 bin]$ ./psql psql (9.2.4) Type "help" for help. postgres=# select * from test; id ----
1
2
3
4 (4 rows) postgres=#
關於pg_basebackup,其官方文檔說明如下:
http://www.postgresql.org/docs/current/static/app-pgbasebackup.html
pg_basebackup is used to take base backups of a running PostgreSQL database cluster. These are taken without affecting other clients to the database, and can be used both for point-in-time recovery (see Section 24.3) and as the starting point for a log shipping or streaming replication standby servers (see Section 25.2). pg_basebackup makes a binary copy of the database cluster files, while making sure the system is automatically put in and out of backup mode automatically. Backups are always taken of the entire database cluster, it is not possible to back up individual databases or database objects. For individual database backups, a tool such as pg_dump must be used. The backup is made over a regular PostgreSQL connection, and uses the replication protocol. The connection must be made with a superuser or a user having REPLICATION permissions (see Section 20.2), and pg_hba.conf must explicitly permit the replication connection. The server must also be configured with max_wal_senders set high enough to leave at least one session available for the backup.
但是,實際上有一個問題是需要引起注意的,上述的streaming replication,並沒有使用到archive_log模式,這個也不是必須的。
可是如果master很繁忙,比如像這樣:
create table test01(id integer, val char(1024)); insert into test01 values(generate_series(1,1228800),repeat( chr(int4(random()*26)+65),1024));
此時,master端的online wal log,不斷地快速產生,有的會隨着新的wal log的生成而被刪除掉。
此時,就會出現如下錯誤:
[postgres@pg200 ~]$ cd /usr/local/pgsql [postgres@pg200 pgsql]$ ./bin/pg_ctl -D ./data start server starting [postgres@pg200 pgsql]$ LOG: database system was shut down in recovery at 2013-09-30 14:51:27 CST LOG: entering standby mode LOG: consistent recovery state reached at 0/5013A48 LOG: redo starts at 0/50139B0 LOG: record with zero length at 0/5013A48 LOG: database system is ready to accept read only connections LOG: streaming replication successfully connected to primary FATAL: could not receive data from WAL stream: FATAL: requested WAL segment 000000010000000000000011 has already been removed LOG: invalid magic number 0000 in log file 0, segment 17, offset 14467072
LOG: streaming replication successfully connected to primary FATAL: could not receive data from WAL stream: FATAL: requested WAL segment 000000010000000000000011 has already been removed
從這個意義上說,使用 archive log是必須的。
[作者 高健@博客園 luckyjackgao@gmail.com]
回到上一級頁面: PostgreSQL集群方案相關索引頁 回到頂級頁面:PostgreSQL索引頁
磨礪技術珠磯,踐行數據之道,追求卓越價值