Postgresql热备安装

一、安装环境
1.CentOS-7.5-x86_64
2.PostgreSQL-9.6

二、数据库安装
1.host文件添加服务器地址

[root@master ~]# vim /etc/hosts
192.168.124.5 master 
192.168.124.6 slave 
192.168.124.11 vip

2.数据库安装
在主备两台服务器安装数据库

[root@master ~]# mv /root/postgresql-9.6.2-1-linux-x64.run /opt
[root@master ~]# cd /opt
[root@master opt]# chmod +x postgresql-9.6.2-1-linux-x64.run
[root@master opt]# ./postgresql-9.6.2-1-linux-x64.run

按默认安装,编码选择zh_CN.utf8

3.修改postgres用户密码

[root@master ]# passwd postgres

4.设置环境变量

[root@master ~]# vim /etc/profile
export PATH=/opt/PostgreSQL/9.6/bin:$PATH
export PGDATA=/opt/PostgreSQL/9.6/data
保存后运行source /etc/profile

三、数据库主机配置
1.创建用户

[root@master ~]# su - postgres
-bash-4.2$ psql
postgres=# create role repl login replication encrypted password 'repl’;

如果成功的则返回CREATE ROLE

postgres=# \q
-bash-4.2$ exit

2.编辑pg_hba.conf文件

[root@master ~]# cd $PGDATA
[root@master ~]# vim pg_hba.conf
local   all             all                                     md5
# IPv4 local connections:
host replication repl   192.168.124.6/24                        md5
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 md5

3.编辑postgresql.conf

[root@master ~]# cd $PGDATA
先备份postgresql.conf:
[root@master data]# cp postgresql.conf postgresql.conf.bak
[root@master data]# vim postgresql.conf
wal_level = replica
max_wal_senders = 2
wal_keep_segments = 256             #10240   16m/个
wal_send_timeout = 60s
max_connections = 100
archive_mode = on
wal_log_hints = on   #上一行要空一行,也就是编辑插入一个回车
log_destination = 'csvlog'
logging_collector = on
log_filename = postgresql-%Y-%m-%d.log
hot_standby = on
保存

4.编辑recovery.done
主库刚安装完编辑的recovery文件为recovery.done

[root@master data]# cp /opt/PostgreSQL/9.6/share/postgresql/recovery.conf.sample recovery.done
[root@master data]# chown postgres:postgres recovery.done
[root@master data]# vi recovery.done
recovery_target_timeline = 'latest'
primary_conninfo='host=slave port=5432 user=repl password=repl' 	#没有空格
trigger_file='/opt/PostgreSQL/9.6/data/standby'		#没有空格
standby_mode = on

重新启动主数据库服务

[root@master data]# systemctl restart postgresql-9.6.service

四、数据库备机配置
1.从主库备份数据库到备库
停止数据库服务

[root@slave ]# systemctl stop postgresql-9.6.service
[root@slave ]# cd /opt/PostgreSQL/9.6
[root@slave 9.6 ]# rm -rf data
[root@slave 9.6 ]# mkdir data
[root@slave 9.6 ]# chown postgres:postgres data
[root@slave 9.6 ]# chmod -R 0700 data
[root@slave 9.6 ]# su - postgres
-bash-4.2$ pg_basebackup -D $PGDATA -Fp -Xs -v -P -h master -p 5432 -U repl
输入密码:repl

2.编辑pg_hba.conf文件

[root@slave ~]# cd $PGDATA
[root@slave ~]# vim pg_hba.conf
local   all             all                                     md5
# IPv4 local connections:
host replication repl   192.168.124.5/24                        md5
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 md5

3.编辑recovery.conf

[root@slave data]# mv recovery.done recovery.conf
[root@slave data]# chown postgres:postgres recovery.conf
[root@slave data]# vim recovery.conf
primary_conninfo = 'host=master port=5432 user=repl password=repl'
host=slave修改为master,修改完需确认hosts文件已经添加了master
[root@slave data]# systemctl start postgresql-9.6.service

五、主备切换
1.验证主备状态

[root@slave ~]# pg_controldata -D $PGDATA | grep cluster
Database cluster state:              in archive recovery

生产: in production 备机: in archive recovery

[root@master ~]# ps -aef|grep postgres

在主机使用sql查询:select * from pg_stat_replication

2.slave切换为主机

[root@slave ~]# touch $PGDATA/standby
[root@slave ~]# pg_controldata -D $PGDATA | grep cluster
Database cluster state:               in production
此时原备机106变为主机,revcovery.conf自动改变为recovery.done

3.master切换为备机

[root@master ~]# systemctl stop postgresql-9.6.service
[root@slave ~]# cd /opt/PostgreSQL/9.6/data/
[root@slave data]# mv recovery.done recovery.conf
[root@master ~]# systemctl start postgresql-9.6.service

4.master恢复为主机

[root@master ~]# touch $PGDATA/standby
此时master恢复为主机

5.slave恢复为备机
在slave服务器,停止postgresql服务

[root@slave ]# systemctl stop postgresql-9.6.service
[root@slave data]# mv recovery.done recovery.conf
[root@slave ]# systemctl start postgresql-9.6.service

验证主备状态,此时slave恢复为备机

发布了2 篇原创文章 · 获赞 2 · 访问量 104

猜你喜欢

转载自blog.csdn.net/weixin_45822470/article/details/105731351