閱讀685 返回首頁    go 阿裏雲 go 技術社區[雲棲]


雲服務器 ECS 建站教程:PostgreSQL 本地Slave搭建步驟




PostgreSQL被業界譽為“最先進的開源數據庫”,目前阿裏雲數據庫PostgreSQL版具有NoSQL兼容,高效查詢,插件化管理,安全穩定的特性。本文檔介紹使用阿裏雲ECS搭建PostgreSQL主從架構的操作步驟。

適用對象

適用於熟悉ECS,熟悉Linux係統,熟悉PostgreSQL的阿裏雲用戶。

基本流程

使用阿裏雲ECS搭建PostgreSQL主從架構的操作步驟如下:

  • 選購ECS 實例
  • 主節點安裝配置
  • 從節點安裝配置
  • 檢測驗證

步驟 1:選購ECS實例

搭建主從複製架構,需要選購2台專有網絡類型的雲服務器ECS實例,建議不分配公網IP,可按需購買彈性公網IP綁定至對應ECS實例,進行配置操作。後續使用您可以根據實際情況考慮配置升級或者架構調優變更。

步驟2:安裝PostgreSQL

在阿裏雲服務器上安裝PostgreSQL有2種方式

  • 鏡像部署
  • 手動部署(源碼編譯安裝/YUM安裝)

本文檔基於yum部署的方式,安裝postgresql;您也可以在雲市場基礎環境中搜索篩選,使用鏡像部署,更加快捷方便。

本文環境軟件明細:CentOS 7.2 |PostgreSQL (9.5.6)

步驟3:PostgreSQL主節點配置

1、主節點上執行以下命令安裝PostgreSQL。

# yum update -y 
# yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm -y
# yum install postgresql95-server postgresql95-contrib -y
# /usr/pgsql-9.5/bin/postgresql95-setup initdb
# systemctl enable postgresql-9.5.service
# systemctl start postgresql-9.5.service

2、主節點上創建進行主從複製的數據庫賬號,並設置密碼及登錄和備份權限。

# su - postgres
# psql
postgres=# CREATE ROLE replica login replication encrypted password 'replica';
CREATE ROLE
postgres=# SELECT usename from pg_user ;
usename  
----------
postgres
replica
(2 rows)
postgres=# SELECT rolname from pg_roles ;
rolname  
----------
postgres
replica
(2 rows)

3、修改pg_hba.conf,設置replica用戶白名單。

# vim /var/lib/pgsql/9.5/data/pg_hba.conf

在IPv4 local connections段添加下麵兩行內容

host    all             all             192.168.1.0/24         md5
允許VPC網段中md5密碼認證連接
host    replication     replica         192.168.1.0/24         md5
允許用戶從replication數據庫進行數據同步

4、修改postgresql.conf

# vim /var/lib/pgsql/9.5/data/postgresql.conf

設置以下參數

wal_level = hot_standby  啟用熱備模式
synchronous_commit = on  開啟同步複製
max_wal_senders = 32     同步最大的進程數量
wal_sender_timeout = 60s 流複製主機發送數據的超時時間
max_connections = 100    最大連接數,從庫的max_connections必須要大於主庫的

5、重啟服務

# systemctl restart postgresql-9.5.service

步驟4:PostgreSQL從節點配置

1、安裝postgres。

# yum update -y
# yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm -y
# yum install postgresql95-server postgresql95-contrib -y

2、使用pg_basebackup基礎備份的工具製定備份目錄。

# pg_basebackup -D /var/lib/pgsql/9.5/data -h 主節點IP -p 5432 -U replica -X stream -P
Password: 
30075/30075 kB (100%), 1/1 tablespace

3、添加並修改recovery.conf。

# cp /usr/pgsql-9.5/share/recovery.conf.sample /var/lib/pgsql/9.5/data/recovery.conf
# vim /var/lib/pgsql/9.5/data/recovery.conf

設置以下參數。

standby_mode = on  
# 聲明此節點為從庫
primary_conninfo = 'host=主節點IP port=5432 user=replica password=replica'  
# 對應主庫的連接信息
recovery_target_timeline = 'latest' 
# 流複製同步到最新的數據

4、修改postgresql.conf。

# vim /var/lib/pgsql/9.5/data/postgresql.conf

設置以下參數。

max_connections = 1000             # 最大連接數,從節點需設置比主節點大
hot_standby = on                   # 開啟熱備
max_standby_streaming_delay = 30s  # 數據流備份的最大延遲時間
wal_receiver_status_interval = 1s  # 從節點向主節點報告自身狀態的最長間隔時間
hot_standby_feedback = on          # 如果有錯誤的數據複製向主進行反饋

5、修改數據目錄屬組屬主。

# chown -R postgres.postgres /var/lib/pgsql/9.5/data

6、啟動服務,設置開機自啟。

# systemctl start postgresql-9.5.service
# systemctl enable postgresql-9.5.service

步驟5:檢測驗證

1、主節點中可查看到sender進程。

# ps aux |grep sender
postgres  2916  0.0  0.3 340388  3220 ?        Ss   15:38   0:00 postgres: wal sender process replica 192.168.1.222(49640) streaming 0/F01C1A8

2、從節點中可查看到receiver進程。

# ps aux |grep receiver
postgres 23284  0.0  0.3 387100  3444 ?        Ss   16:04   0:00 postgres: wal receiver process   streaming 0/F01C1A8

3、主庫中可查看到從庫狀態。

replication=# select * from pg_stat_replication;
pid  | usesysid | usename | application_name |  client_addr  | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_location | write_locati
on | flush_location | replay_location | sync_priority | sync_state 
------+----------+---------+------------------+---------------+-----------------+-------------     +-------------------------------+--------------+-----------+---------------+-------------
---+----------------+-----------------+---------------+------------
2916 |    16393 | replica | walreceiver      | 192.168.1.222 |                 |       49640 |    2017-05-02 15:38:06.188988+08 |         1836 | streaming | 0/F01C0C8     | 0/F01C0C8   
| 0/F01C0C8      | 0/F01C0C8       |             0 | async
(1 rows)

原文鏈接

最後更新:2017-08-13 22:28:55

  上一篇:go  HiStore:阿裏巴巴海量數據場景下的OLAP解決方案
  下一篇:go  無人自助便利店采用射頻識別技術 30秒鍾就能完成付款