1. 程式人生 > >postgres安裝和主從複製

postgres安裝和主從複製

postgres安裝和主從複製

------------------------------------------------------------------------

一、操作環境

 

採用VMware ESXi虛擬出的2臺伺服器:

 

host1:psql_master,172.16.101.220

 

host2:psql_standby,172.16.101.221

 

二.安裝

master和slave 安裝postgres

 

yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

yum install -y postgresql96

yum install -y postgresql96-server

/usr/pgsql-9.6/bin/postgresql96-setup initdb

systemctl enable postgresql-9.6

systemctl start postgresql-9.6

 

三.配置使用

 

1. 修改使用者密碼

su - postgres

-bash-4.2$ psql -U postgres

#進入資料庫後修改密碼;

postgres=# alter user postgres with password '

[email protected]'

 

2.允許遠端訪問

[[email protected]_master ~]# sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" /var/lib/pgsql/9.6/data/postgresql.conf

 

3、主機認證

vim /var/lib/pgsql/9.6/data/pg_hba.conf

 

4. 設定環境變數

[[email protected]_master ~]# vim /etc/profile

export PATH=$PATH:/usr/pgsql-9.6/bin

 

[[email protected]_master ~]# source /etc/profile

 

5. 重啟服務

[[email protected]_master ~]# systemctl restart postgresql-9.6

 

6、驗證

[[email protected]_master ~]# netstat -tunlp

 

四、配置主從(主庫配置)

1. 建立複製使用者

#需要一個賬號進行主從同步

su - postgres

psql -U postgres

postgres=#create role repl login replication encrypted password '[email protected]';

 

2. 認證檔案pg_hba.conf

#配置從庫可以採用repl賬號進行同步

[[email protected]_master ~]# vim /var/lib/pgsql/9.6/data/pg_hba.conf

host replication repl 172.16.101.221/24 md5

3、主庫配置檔案postgresql.conf

#監聽埠

listen_addresses = '*'

wal_level = hot_standby

max_wal_senders = 2

wal_keep_segments = 64

max_connections = 100

4. 重啟服務

#同時注意開啟防火牆埠開啟

systemctl restart postgresql-9.6

 

五、從庫配置

 

從庫安裝postgresql後,暫不初始化,如果從庫已初始化,可以清空其data目錄(預設安裝是/var/lib/pgsql/9.6/data/目錄)。

1. 基礎備份

[[email protected]_standby ~]# pg_basebackup -h 10.11.4.186 -p 5432 -U repl -F p -P -D /var/lib/pgsql/9.6/data/

2. 備份目錄許可權

#基於root賬號做的基礎備份,需要將相關目錄檔案的許可權變更

[[email protected]_standby ~]# chown -R postgres:postgres /var/lib/pgsql/9.6/data/

 

3. 從庫配置檔案postgresql.conf

 

#在基礎備份時,初始化檔案是從主庫複製來的,所以配置檔案一致,可將wal_level,max_wal_senders與wal_keep_segments等引數註釋,以下是新增或修改的引數

[[email protected]_standby ~]# vim /var/lib/pgsql/9.6/data/postgresql.conf

#在備份的同時允許查詢

hot_standby = on

max_standby_streaming_delay = 30s

wal_receiver_status_interval = 10s

hot_standby_feedback = on

#預設引數,非主從配置相關引數,表示到資料庫的連線數,一般從庫做主要的讀服務時,設定值需要高於主庫

max_connections = 1000

 

4. 恢復檔案recovery.conf

 

#在做基礎備份時,也可通過-R引數在備份結束後自動生產一個recovery.conf檔案

[[email protected]_standby ~]# cp /usr/pgsql-9.6/share/recovery.conf.sample /var/lib/pgsql/9.6/data/recovery.conf

[[email protected]_standby ~]# chown postgres:postgres /var/lib/pgsql/9.6/data/recovery.conf

[[email protected]_standby ~]# vim /var/lib/pgsql/9.6/data/recovery.conf

recovery_target_timeline = 'latest'

standby_mode = on

primary_conninfo = 'host=172.16.101.220 port=5432 user=repl [email protected]'

5. 設定環境變數 (很重要)

[[email protected]_master ~]# vim /etc/profile

export PATH=$PATH:/usr/pgsql-9.6/bin

 

[[email protected]_master ~]# source /etc/profile

 

6. 重啟服務

[[email protected]_standby ~]# systemctl restart postgresql-9.6

 

 

五.使用驗證

 

1)主庫sender程序

[[email protected]_master ~]# ps -ef | grep postgres

2)從庫receiver過程

 

[[email protected]_standby ~]# ps -ef | grep postgres

[[email protected]_standby ~]# su - postgres

-bash-4.2$ psql

postgres=# \x

postgres=# select * from pg_stat_replication;

 

 

3. 表複製測試

 

1)建表(主庫)

postgres=# create table postgrestb(id int primary key,name VARCHAR(20),salary real);

postgres=# insert into postgrestb values(10, 'Messi', 10000.00);

postgres=# insert into postgrestb values(6, 'Xavi', 10000.00);

postgres=# select * from postgrestb;

 

2)查詢(從庫)

 

[[email protected]_standby ~]# su - postgres

-bash-4.2$ psql

postgres=# \d

postgres=# select * from postgrestb;

 

3)從庫寫測試

#從庫只讀,不能寫入資料

postgres=# insert into postgrestb values(8, 'Iniesta', 10000.00);

 

4)啟用從庫

#採用”pg_ctl promote“切換從庫為主庫;
#切換後,從庫的recovery.conf檔名字變成了recovery.done
[[email protected]_standby ~]# su - postgres -c "pg_ctl promote"
server promoting

切換後,從庫的recovery.conf檔名字變成了recovery.done

5)檢視從庫日誌,狀態與程序

[[email protected]_standby ~]# tailf /var/lib/pgsql/9.6/data/pg_log/postgresql-Tue.log

 

[[email protected] ~]#  pg_controldata /var/lib/pgsql/9.6/data/

 

6、主庫啟動,由從庫轉換到主庫。

啟動主庫

[[email protected] ~]# su - postgres -c "pg_ctl start -m fast"

server starting

< 2018-08-28 09:42:46.745 CST > LOG:  redirecting log output to logging collector process

< 2018-08-28 09:42:46.745 CST > HINT:  Future log output will appear in directory "pg_log".

主庫的狀態

切換到從庫

首先關閉從庫

[[email protected] data]# su - postgres -c "pg_ctl stop -m fast"

waiting for server to shut down.... done

server stopped

#手動更改從庫的recovery.done檔名字變成了recovery.conf

[[email protected]_standby data]# mv recovery.done recovery.conf

[[email protected]_standby data]# su - postgres -c "pg_ctl start -m fast"

server starting