天天看点

Postgresq9.6主从部署Postgresq9.6主从部署

实验环境

名称

IP

系统

Master

172.17.10.190

Centos 6.5

Slave

172.17.10.189

1.yun安装

1

2

<code>rpm -ivh https:</code><code>//download</code><code>.postgresql.org</code><code>/pub/repos/yum/9</code><code>.6</code><code>/redhat/rhel-6-x86_64/pgdg-centos96-9</code><code>.6-3.noarch.rpm</code>

<code>yum </code><code>install</code> <code>postgresql96.x86_64 postgresql96-server.x86_64 -y</code>

2.主从配置

2.1 主数据库配置

启动master

3

4

<code>/etc/init</code><code>.d</code><code>/postgresql-9</code><code>.6 initdb</code>

<code>/etc/init</code><code>.d</code><code>/postgresql-9</code><code>.6 start</code>

<code>su</code> <code>- postgres</code>

<code>psql</code>

授权

<code>create role repl login replication encrypted password </code><code>'51idc.com'</code><code>;</code>

编辑hba文件

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

新增

<code>host    replication     repl            172.17.10.0</code><code>/24</code>         <code>md5</code>

<code>host    all            repl            172.17.10.0</code><code>/24</code>         <code>trust</code>

编辑配置文件

/var/lib/pgsql/9.6/data/postgresql.conf

5

6

7

8

<code>listen_addresses = 172.17.10.190</code>

<code>wal_level = hot_standby  </code><code>#热备模式</code>

<code>max_wal_senders= 6 </code><code>#可以设置最多几个流复制链接,差不多有几个从,就设置多少</code>

<code>wal_keep_segments = 10240  </code><code>#重要配置 </code>

<code>wal_send_timeout = 60s </code>

<code>max_connections = 512 </code><code>#从库的 max_connections要大于主库</code>

<code>archive_mode = on </code><code>#允许归档 </code>

<code>archive_command = </code><code>'cp %p /url/path%f'</code>   <code>#根据实际情况设置</code>

2.2 从数据库配置

如果开始为启动数据库可忽略下一步

<code>rm</code> <code>-rf </code><code>/var/lib/pgsql/9</code><code>.6</code><code>/data/</code><code>*  </code><code>#开始没有启动从数据库,这一步可以省略 </code>

<code>pg_basebackup -h 172.17.10.190 -U repl -D </code><code>/var/lib/pgsql/9</code><code>.6</code><code>/data</code> <code>-X stream -P</code>

<code>cp</code> <code>/usr/pgsql-9</code><code>.6</code><code>/share/recovery</code><code>.conf.sample </code><code>/var/lib/pgsql/9</code><code>.6</code><code>/data/recovery</code><code>.conf</code>

修改配置文件recovery.conf

<code>standby_mode = on</code>

<code>primary_conninfo = </code><code>'host=172.17.10.190 port=5432 user=repl password=51idc.com'</code>

<code>trigger_file = </code><code>'/var/lib/pgsql/9.6/data/trigger.kenyon'</code>    <code>#主从切换时后的触发文件</code>

<code>recovery_target_timeline = </code><code>'latest'</code>

配置postgresql.conf文件

<code>listen_addresses = 172.17.10.189</code>

<code>wal_level = hot_standby </code>

<code>max_connections = 1000 </code><code>#一般从的最大链接要大于主的。 </code>

<code>hot_standby = on </code><code>#说明这台机器不仅仅用于数据归档,也用于查询 </code>

<code>max_standby_streaming_delay = 30s </code>

<code>wal_receiver_status_interval = 10s </code><code>#多久向主报告一次从的状态。 </code>

<code>hot_standby_feedback = on </code><code>#如果有错误的数据复制,是否向主进行范例</code>

检测

<code>select</code> <code>client_addr,sync_state from pg_stat_replication;</code>

<a href="https://s5.51cto.com/wyfs02/M00/8F/1D/wKioL1jT9aOCAZVTAACJLn0Arvk876.png" target="_blank"></a>

查看主从状态

<code>select</code> <code>* from pg_stat_replication;</code>

<a href="https://s3.51cto.com/wyfs02/M02/8F/1F/wKiom1jT9oqDxXLKAADvjuYegcw403.png" target="_blank"></a>

脚本监控主从

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<code>#!/bin/bash</code>

<code># mail  [email protected]</code>

<code>data=`</code><code>date</code> <code>+%Y-%M-%d</code><code>" "</code><code>%H:%m`</code>

<code>netstat</code> <code>-lntup|</code><code>grep</code> <code>5432 &amp;&amp; </code><code>ps</code> <code>-ef|</code><code>grep</code> <code>postmaster</code>

<code>if</code> <code>[ $? -</code><code>eq</code> <code>0 ];</code><code>then</code>

<code>for</code> <code>IP </code><code>in</code> <code>172.17.10.188 172.17.10.189</code>

<code>do</code>

<code>/usr/bin/psql</code> <code>-h 172.17.10.190 -p 5432 -U repl -d postgres --</code><code>command</code> <code>"select * from pg_stat_replication"</code><code>|</code><code>grep</code> <code>$IP</code>

<code>if</code> <code>[ </code><code>"$?"</code> <code>!= </code><code>"0"</code> <code>];</code><code>then</code>

<code>echo</code>

<code> </code><code>"postgresql master-slave status is error! please login check!"</code><code>|mail -r </code>

<code>"[email protected]"</code> <code>-s </code><code>"postgresql master-slave status is error"</code> 

<code>[email protected] \</code>

<code>&amp;&amp; </code><code>echo</code> <code>"$data postgresql postgresql master-slave status is error!"</code><code>&gt;&gt;</code><code>/var/log/postgresql-error</code><code>.log</code>

<code>fi</code>

<code>done</code>

<code>else</code>

2.3主从切换

主库查看进程为sender

<a href="https://s3.51cto.com/wyfs02/M00/8F/22/wKiom1jUiKTwsu0UAALvaWigL5E515.png" target="_blank"></a>

备库

<a href="https://s3.51cto.com/wyfs02/M02/8F/20/wKioL1jUiRjB8XPAAAJNDLZCTIU269.png" target="_blank"></a>

停止主库

<a href="https://s5.51cto.com/wyfs02/M00/8F/20/wKioL1jUiVGSZNWiAAEmy2IYoj8840.png" target="_blank"></a>

查看slave的日志

<a href="https://s1.51cto.com/wyfs02/M01/8F/1D/wKioL1jT-KujDMuiAANHbW6b5yI968.png" target="_blank"></a>

创建触发文件,切换主

<code>touch</code> <code>trigger.kenyon</code>

<a href="https://s3.51cto.com/wyfs02/M00/8F/1F/wKiom1jT-Q3RDgNUAAUdPTrS_YI464.png" target="_blank"></a>

查看slave的日志,面前已经切换为主

<a href="https://s4.51cto.com/wyfs02/M02/8F/1F/wKiom1jT-UnyC2mnAAIvlmww7-I077.png" target="_blank"></a>

使用pg_controldata

<a href="https://s2.51cto.com/wyfs02/M00/8F/1D/wKioL1jT-XXiXGkoAABQpY5XhfM716.png" target="_blank"></a>

<a href="https://s2.51cto.com/wyfs02/M02/8F/1D/wKioL1jT-bDAuyz9AANjzAsM3a4422.png" target="_blank"></a>

备机状态为: in archive recovery

主库状态为:in production

本文转自 KaliArch 51CTO博客,原文链接:http://blog.51cto.com/kaliarch/1909936,如需转载请自行联系原作者