软件环境
CentOS 5.1
P****ostgresql 9.4.10
一、 安装postgreSQL
1.YUM自动获取版本
SHELL>wget --no-check-certificate https://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/pgdg-redhat94-9.4-1.noarch.rpm
SHELL>rpm -ivh pgdg-redhat94-9.4-1.noarch.rpm
SHELL>yum install postgresql94-server postgresql94-contrib
2.初始化数据库
SHELL>/etc/init.d/postgresql-9.4 initdb
3.启动数据库
SHELL>/etc/init.d/postgresql-9.4 start
4.进入数据库
SHELL>su postgres
SHELL>psql
5.修改密码
SQL>alter user postgres with password 'postgres';
6.启动服务
SHELL>service postgresql-9.4 restart
二、****配置过程
主机IP:server.example.com
从机IP:client.example.com
数据库:postgres
用户名:postgres
密 码:postgres
1. 主机pg_hba.conf添加备份主从数据库
# IPv4 local connections
host replication postgres client.example.com/32 trust
host replication postgres server.example.com/32 trust
2. 主机postgresql.conf配置(‘#’非必须)
wal_level = hot_standby #开启主从热备模式
max_wal_senders = 10 #从机可连接数
wal_keep_segments = 32
#check_function_bodies = on
#checkpoint_segments = 16
#hot_standby = on
#hot_standby_feedback = on
#autovacuum = on
#fsync = on #异步数据同步
#full_page_writes = on
#archive_mode = on #打开归档模式
#archive_command = 'cp %p /var/lib/pgsql/9.4/data/pg_archive/%f'
#将归档文件保存在data/archive目录下
#log_destination = 'csvlog'
#logging_collector = on
#log_directory = '/var/log/pgsql-log/'
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
#log_truncate_on_rotation = on
#log_rotation_age = 1d
#log_rotation_size = 10MB
#log_min_duration_statement = 1000
#log_lock_waits = on
#log_statement = ' ddl '
#log_timezone = 'PRC'
#log_autovacuum_min_duration = 0
3. 建立归档文件和日志文件
SHELL>mkdir /var/lib/pgsql/9.4/data/pg_archive
SHELL>chown -R postgres.postgres /var/lib/pgsql/9.4/data/pg_archive
SHELL>mkdir /var/log/pgsql-log
SHELL>chown -R postgres.postgres /var/log/pgsql-log
4. 重启动服务
SHELL>service postgresql-9.4 restart
5. 基础备份
SQL> select pg_start_backup('');
6. 从数据库删除 data目录
SHELL>rm -rf /var/lib/pgsql/9.4/data
7. 拷贝主数据库至从服务器
SHELL>scp -rp /var/lib/pgsql/9.4/data root@client.example.com:/var/lib/pgsql/9.4/
SHELL>chown -R postgres.postgres /var/lib/pgsql/9.4/data
8. 结束主数据库的备份状态
SQL> select pg_stop_backup();
9. 查看是否成功归档
SHELL>cd /var/lib/pgsql/9.4/data/pg_archive
SHELL>ls
10. 移动归档文件到从机
SHELL>scp -rp /var/lib/pgsql/9.4/data/pg_archive root@client.example.com:/var/lib/pgsql/9.4/data/
SHELL>chown -R postgres.postgres /var/lib/pgsql/9.4/data/pg_archive
11. 从机数据库配置
SHELL>mkdir /var/log/pgsql-log
SHELL>chown -R postgres.postgres /var/log/pgsql-log
postgresql.conf 设置hot_standby = on
12. 添加recovery.conf文件(在$PG_HOME/share里,用户组postgresql),并录入以下内容
restore_command = 'cp /var/lib/pgsql/9.4/data/pg_archive/%f %p'
standby_mode = on
primary_conninfo = 'host=server.example.com port=5432 user=postgres password=postgres'
trigger_file = '/trigger.postgres'(任意值)
13. 删除从数据库postmaster.pid文件以及pg_xlog下的文件,并重新启动服务
SHELL>rm /var/lib/pgsql/9.4/data/postmaster.pid
SHELL>rm -rf /var/lib/pgsql/9.4/data/pg_xlog/*
SHELL>service postgresql-9.4 restart
- 验证是否配置成功并确定主从机
①.主机表中能查到记录,从机表中没有
SQL>select * from pg_stat_replication;
√②.根据psql自带函数判断,主机f 从机t
SQL>select pg_is_in_recovery();
√③. 查看postgresql线程,wal sender是主机,wal receiver是从机
SHELL>ps -ef|grep postgres
如果都验证失败,可以setup命令关闭防火墙再次验证
14. 主从切换
①.关闭主机服务
SHELL>service postgresql-9.4 stop
有trigger_file触发文件则删除
SHELL>rm trigger_file文件
从机进行验证(结果应该无wal receiver项)
SHELL>ps -ef|grep postgres
②.触发从机为主机
SHELL>touch trigger_file文件
验证(应包含autovacuum项且recovery文件后缀变为done)
SHELL>ps –ef|grep postgres
③.建立新从机,以下选一种方式
(一)按主从热备配置新建
(二)复制新主机的recovery.conf文件,并修改配置
Ⅰprimary_conninfo信息
Ⅱ添加recovery_target_timeline = 'latest'(不加会有timeline时间线问题)
Ⅲ重命名唯一trigger_file 然后同步归档文件
SHELL>scp -rp /var/lib/pgsql/9.4/data/pg_archive root@server.example.com:/var/lib/pgsql/9.4/data
SHELL>chown postgres:postgres /var/lib/pgsql/9.4/data
④.启动新从机
SHELL>service postgresql-9.4 start
三、****部分问题记录
could not connect to the primary server 关闭防火墙
server closed the connection unexpectedly启动postgreSQL服务/var/lib/pgsql权限需为700
参考博客
http://fshuanglan.blog.51cto.com/133806/1400235/
http://www.cnblogs.com/liuyuanyuanGOGO/archive/2012/10/09/pg_streamingrepulication.html