您好,登錄后才能下訂單哦!
主從服務(wù)器IP:
192.168.11.131 postgreSQL master
192.168.11.132 postgreSQL slave
服務(wù)器系統(tǒng)版本:
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)
PG版本:
系統(tǒng)默認(rèn)9.2
1、安裝軟件、配置環(huán)境變量
主從服務(wù)器:
# yum install postgresql -y
# yum install postgresql-server -y
# mkdir /data/pg_data
# chown postgres:postgres /data/pg_data
# vi /etc/profile
export PGDATA=/data/pg_data
# source /etc/profile
主服務(wù)器:
2、初始化數(shù)據(jù)庫(kù)、啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)并設(shè)置為開機(jī)啟動(dòng)
# initdb -D /data/pg_data
注意:
-D是指定數(shù)據(jù)存放目錄,默認(rèn)情況下是存放在/var/lib/pgsql/data目錄下,但是生產(chǎn)環(huán)境通常有單獨(dú)的數(shù)據(jù)存放分區(qū)。
# su postgres
bash-4.2$ pg_ctl -D /data/pg_data start
bash-4.2$ exit
# vi /etc/rc.d/rc.local
/usr/bin/postgres -D /data/pg_data
3、創(chuàng)建同步用戶
# su postgres
bash-4.2$ psql
postgres=# create role repuser login replication encrypted password 'password123';
postgres=# \q
注意:
這里創(chuàng)建的用戶是repuser,密碼是password123,你可以根據(jù)需要隨意配置。
4、修改配置文件pg_hba.conf、postgresql.conf
bash-4.2$ vi /data/pg_data/pg_hba.conf
host replication repuser 192.168.11.0/8 md5
host all all 192.168.11.0/8 trust
bash-4.2$ vi /data/pg_data/postgresql.conf
添加下面配置,配置文件有下面配置的要?jiǎng)h除(包括前邊有警號(hào)'#'的)
listen_addresses = '192.168.11.131'
wal_level = hot_standby
max_wal_senders= 6
wal_keep_segments = 10240
max_connections = 512
archive_mode = on
archive_command = 'cp %p /data/pg_data/pg_archive/%f'
bash-4.2$ mkdir /data/pg_data/pg_archive
注意:
這里的配置archive_command需要根據(jù)實(shí)際配置做出改變。
重新加載使配置生效
bash-4.2$ pg_ctl -D /data/pg_data reload
從服務(wù)器:
5、同步數(shù)據(jù)
bash-4.2$ pg_basebackup -h 192.168.11.131 -U repuser -D /data/pg_data -X stream -P
Password:
36413/36413 kB (100%), 1/1 tablespace
6、修改配置文件recovery.conf、postgresql.conf
bash-4.2$ cp /usr/share/pgsql/recovery.conf.sample /data/pg_data/recovery.conf
bash-4.2$ vi /data/pg_data/recovery.conf
添加下面配置,配置文件有下面配置的要?jiǎng)h除(包括前邊有警號(hào)'#'的)
standby_mode = on
primary_conninfo = 'host=192.168.11.131 port=5432 user=repuser password=password123 keepalives_idle=60'
recovery_target_timeline = 'latest
注意:
這里的user就是之前創(chuàng)建的同步數(shù)據(jù)的用戶,密碼也是響應(yīng)的密碼。
bash-4.2$ vi /data/pg_data/postgresql.conf
添加下面配置,配置文件有下面配置的要?jiǎng)h除(包括前邊有警號(hào)'#'的)
listen_addresses = '192.168.11.132'
wal_level = hot_standby
max_connections = 1000
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
bash-4.2$ exit
7、啟動(dòng)服務(wù)并設(shè)置開機(jī)啟動(dòng)
# chmod 700 /data/pg_data
# vi /etc/rc.d/rc.local
su postgres -c '/usr/bin/postgres -D /data/pg_data'
# su postgres
bash-4.2$ pg_ctl -D /data/pg_data start
8、驗(yàn)證
主服務(wù)器:
bash-4.2$ psql
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
--------------+------------
192.168.11.132 | async
postgres=# create database test;
CREATE DATABASE
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
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
從服務(wù)器:
# su postgres
bash-4.2$ psql
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
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
9、創(chuàng)建數(shù)據(jù)庫(kù)、訪問用戶并給數(shù)據(jù)庫(kù)賦權(quán)
主服務(wù)器
postgres=# create user pgone with password 'password321';
CREATE ROLE
postgres=# create database pgdata owner pgone;
CREATE DATABASE
postgres=# grant all privileges on database pgdata to pgone;
GRANT
10、設(shè)置本地登錄數(shù)據(jù)庫(kù)需要輸入密碼,設(shè)置客戶端工具登錄數(shù)據(jù)庫(kù)需要輸入密碼
10.1、如果有需求的話,可以設(shè)置本地登錄數(shù)據(jù)庫(kù)需要輸入密碼
主服務(wù)器
修改賬戶登錄數(shù)據(jù)庫(kù)密碼
# su postgres
bash-4.2$ psql
postgres=# alter user postgres with password 'password321';
ALTER ROLE
修改配置文件pg_hba.conf
postgres=# vi /data/pg_data/pg_hba.conf
local all all trust
host all all 127.0.0.1/32 trust
>>
local all all md5
host all all 127.0.0.1/32 md5
重新加載配置文件
bash-4.2$ pg_ctl -D /data/pg_data reload
從服務(wù)器只需要修改配置文件pg_hba.conf 并使其生效就可以了
10.2、設(shè)置遠(yuǎn)程登錄數(shù)據(jù)庫(kù)需要輸入密碼,用于客戶端工具連接
主服務(wù)器
修改配置文件pg_hba.conf
postgres=# vi /data/pg_data/pg_hba.con
host all all 192.168.11.0/24 md5
對(duì)于192.168.11.0網(wǎng)段的服務(wù)器登錄數(shù)據(jù)庫(kù)需要輸入密碼登錄
從服務(wù)器只需要修改配置文件pg_hba.conf 并使其生效就可以了
參考:
https://www.cnblogs.com/sunshine-long/p/9059695.html
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。