Postgresq9.6主从部署
实验环境
名称 |
IP |
系统 |
Master |
172.17.10.190 |
Centos 6.5 |
Slave |
172.17.10.189 |
Centos 6.5 |
1.yun安装
rpm -ivh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm yum install postgresql96.x86_64 postgresql96-server.x86_64 -y
2.主从配置
2.1 主数据库配置
启动master
/etc/init.d/postgresql-9.6 initdb /etc/init.d/postgresql-9.6 start su - postgres psql
授权
create role repl login replication encrypted password '51idc.com';
编辑hba文件
/var/lib/pgsql/9.6/data/pg_hba.conf
新增
host replication repl 172.17.10.0/24 md5 host all repl 172.17.10.0/24 trust
编辑配置文件
/var/lib/pgsql/9.6/data/postgresql.conf
listen_addresses = 172.17.10.190 wal_level = hot_standby #热备模式 max_wal_senders= 6 #可以设置最多几个流复制链接,差不多有几个从,就设置多少 wal_keep_segments = 10240 #重要配置 wal_send_timeout = 60s max_connections = 512 #从库的 max_connections要大于主库 archive_mode = on #允许归档 archive_command = 'cp %p /url/path%f' #根据实际情况设置
2.2 从数据库配置
su - postgres
如果开始为启动数据库可忽略下一步
rm -rf /var/lib/pgsql/9.6/data/* #开始没有启动从数据库,这一步可以省略 pg_basebackup -h 172.17.10.190 -U repl -D /var/lib/pgsql/9.6/data -X stream -P cp /usr/pgsql-9.6/share/recovery.conf.sample /var/lib/pgsql/9.6/data/recovery.conf
修改配置文件recovery.conf
standby_mode = on primary_conninfo = 'host=172.17.10.190 port=5432 user=repl password=51idc.com' trigger_file = '/var/lib/pgsql/9.6/data/trigger.kenyon' #主从切换时后的触发文件 recovery_target_timeline = 'latest'
配置postgresql.conf文件
listen_addresses = 172.17.10.189 wal_level = hot_standby max_connections = 1000 #一般从的最大链接要大于主的。 hot_standby = on #说明这台机器不仅仅用于数据归档,也用于查询 max_standby_streaming_delay = 30s wal_receiver_status_interval = 10s #多久向主报告一次从的状态。 hot_standby_feedback = on #如果有错误的数据复制,是否向主进行范例
检测
select client_addr,sync_state from pg_stat_replication;
查看主从状态
select * from pg_stat_replication;
脚本监控主从
#!/bin/bash # mail xuel@51idc.com data=`date +%Y-%M-%d" "%H:%m` netstat -lntup|grep 5432 && ps -ef|grep postmaster if [ $? -eq 0 ];then for IP in 172.17.10.188 172.17.10.189 do /usr/bin/psql -h 172.17.10.190 -p 5432 -U repl -d postgres --command "select * from pg_stat_replication"|grep $IP if [ "$?" != "0" ];then echo "postgresql master-slave status is error! please login check!"|mail -r "xuel@51idc.com" -s "postgresql master-slave status is error" xuel@51idc.com \ && echo "$data postgresql postgresql master-slave status is error!">>/var/log/postgresql-error.log fi done else echo "postgresql master-slave status is error! please login check!"|mail -r "xuel@51idc.com" -s "postgresql master-slave status is error" xuel@51idc.com \ && echo "$data postgresql postgresql master-slave status is error!">>/var/log/postgresql-error.log fi
2.3主从切换
主库查看进程为sender
备库
停止主库
查看slave的日志
创建触发文件,切换主
touch trigger.kenyon
查看slave的日志,面前已经切换为主
使用pg_controldata
备机状态为: in archive recovery
主库状态为:in production