天天看點

centos下postgresql源碼編譯安裝與主備異步配置

機器:10.11.1.197 & 10.11.1.198

配置:CPU:2 * 12核(2.0GHZ),24線程;MEM:120G

pg版本:9.5.1

源碼下載下傳與解壓:

centos下postgresql源碼編譯安裝與主備異步配置

安裝依賴環境:

sudo yum install gcc gcc-c++ automakeautoconf libtool make readline-devel zlib-devel

centos下postgresql源碼編譯安裝與主備異步配置

在解壓後的源碼目錄依次執行下面編譯安裝指令:

./configure --prefix=/home/liyuming/postgresql/postgresql

gmake -j 20

gmake install

一切順利

配置環境變量

centos下postgresql源碼編譯安裝與主備異步配置

建資料目錄

centos下postgresql源碼編譯安裝與主備異步配置

初始化資料庫(pg bin目錄下)

./initdb -D /home/liyuming/postgresql/postgresql/data

啟動資料庫(pg bin目錄下)

./pg_ctl -D /home/liyuming/postgresql/postgresql/data -l logfile start

進入用戶端(pg bin目錄下)

psql 資料庫名

psql postgres

\l檢視所有資料庫

\c test 選擇資料庫

\dt 檢視所有資料表

\d test檢視資料表結構

select * from pg_indexes where tablename='test';檢視資料表的索引

\q 退出資料庫

centos下postgresql源碼編譯安裝與主備異步配置

或者這種方式進入pg:

psql -h 127.0.0.1 -d postgres -U liyuming

centos下postgresql源碼編譯安裝與主備異步配置

給liyuming使用者設定密碼(進入pg後執行):

alter user liyuming with password 'postgres';

以密碼驗證形式進入資料庫

psql -h 127.0.0.1 -d postgres -U liyuming -W

但是指定ip形式無法進入資料庫:

psql -h 10.11.1.197 -p 5432 -d postgres -Uliyuming -W

無法通過ip通路資料庫

下面配置PostgreSQL資料庫遠端連接配接功能:

分别修改下面兩個配置檔案(在data目錄中)

postgresql.conf(update)

listen_addresses= '*'

pg_hba.conf(append)

host all    all    0.0.0.0/0 md5

重新開機

./pg_ctl -D/home/liyuming/postgresql/postgresql/data stop

./pg_ctl -D/home/liyuming/postgresql/postgresql/data -l logfile start

psql -h 10.11.1.197 -p 5432 -d postgres -Uliyuming -W

OK!(passwd:postgres)

JDBC也可正常連接配接

postgresql調優:

max_connections = 300

shared_buffers = 16GB

huge_pages = try

maintenance_work_mem = 1GB

dynamic_shared_memory_type =posix

bgwriter_delay = 10ms

wal_level = minimal

fsync = on

synchronous_commit = off

wal_buffers = 16MB

wal_writer_delay = 10ms

max_wal_size = 16GB

effective_cache_size = 90GB

log_destination = 'csvlog'

logging_collector = on

log_truncate_on_rotation = on

log_timezone = 'PRC'

datestyle = 'iso, mdy'

timezone = 'PRC'

lc_messages = 'EN_US.UTF-8'

lc_monetary = 'EN_US.UTF-8'

lc_numeric = 'EN_US.UTF-8'

lc_time = 'EN_US.UTF-8'

default_text_search_config ='pg_catalog.english'

驗證參數是否生效(登入資料庫)

show all;

發現參數正确

postgresql主備配置:

主庫:

vi pg_hba.conf(append)

host replication     replica    0.0.0.0/0       md5

vi postgresql.conf(append)

wal_level =hot_standby

max_wal_senders = 2

wal_keep_segments= 256

wal_sender_timeout= 60s

在sql控制台中執行

CREATE ROLE replicalogin replication encrypted password 'replica'

重新開機資料庫伺服器

從庫:

bin目錄執行(必須等主庫重新開機後才能執行下面這個指令)

pg_basebackup -F p --progress -D /home/liyuming/postgresql/postgresql/data2 -h 10.11.1.197 -p 5432 -U replica --password

進入到data2目錄,copy share目錄的recovery.conf.sample至data2中,并修改如下:

vi recovery.conf

standby_mode = on

primary_conninfo = 'host=10.11.1.197 port=5432 user=replica password=replica'

recovery_target_timeline= 'latest'

vi postgresql.conf

hot_standby = on

max_standby_streaming_delay= 30s

wal_receiver_status_interval= 1s

hot_standby_feedback= on

起從庫

./pg_ctl -D /home/liyuming/postgresql/postgresql/data2 -l logfile start

./pg_ctl -D /home/liyuming/postgresql/postgresql/data2 -l logfile stop

檢測:

psql -h 10.11.1.197 -p 5432 -d postgres -Uliyuming -W

passwd:postgres

select * from pg_stat_replication;

centos下postgresql源碼編譯安裝與主備異步配置

OK!