postgres主从配置
运维开发技术交流群欢迎大家加入一起学习(QQ:722381733)
开始部署postgres主从(如果没不会安装postgres的请去上一个博文中查看)
这里我使用了两台服务器部署
主:192.168.254.21
从:192.168.254.22
主配置(192.168.254.21)
1、进入部署postgres服务的用户
[root@web1 ~]# su - postgres
Last login: Thu May :: CST on pts/
[postgres@web1 ~]$
2、启动服务
[postgres@web1 ~]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web1 ~]$
3、进入postgres数据库,创建用户并授权
[postgres@web1 ~]$ psql
psql (10.5)
Type "help" for help. postgres=# CREATE ROLE replica login replication encrypted password 'replica';
CREATE ROLE
postgres=#
4、检查所以库用户后退出
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
replica | Replication | {}
test | Superuser | {} postgres=# \q
[postgres@web1 ~]$
5、进入安装目录的库文件配置目录
[postgres@web1 ~]$ cd /usr/local/pgsql-10.5/data/
[postgres@web1 data]$ ls
base pg_hba.conf pg_multixact pg_snapshots pg_tblspc pg_xact postmaster.opts
global pg_hba.conf.-- pg_notify pg_stat pg_twophase postgresql.auto.conf postmaster.pid
pg_commit_ts pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
pg_dynshmem pg_logical pg_serial pg_subtrans pg_wal postgresql.conf.--
[postgres@web1 data]$
6、修改postgresql.conf配置文件,修改如下(修改时记得备份)
wal_level = hot_standby #热备模式
max_wal_senders = #这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
wal_keep_segments = #设置流复制保留的最多的xlog数目(重要配置)
wal_sender_timeout = 60s #设置流复制主机发送数据的超时时间
max_connections = #这个设置要注意下,从库的max_connections必须要大于主库的
7、打开pg_hba.conf配置文件,设置如下(修改时记得备份)
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
#host all all 127.0.0.1/ trust
host all all 0.0.0.0/ md5 #所有ip都可以通过密码连接
# IPv6 local connections:
host all all ::/ trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/ trust
host replication all ::/ trust
host replication replica 192.168.254.22/ md5
8、保存退出后重启主服务器上的postgres库
[postgres@web1 data]$ pg_ctl restart -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
[postgres@web1 data]$
***在此主就配置完了,现在配置从服务器***
从配置(192.168.254.22)
1、进入部署postgres服务的用户
[root@web2 ~]# su - postgres
Last login: Thu May :: CST on pts/
[postgres@web2 ~]$
2、这里为了方便我直接删除data目录用于备份主服务的数据,如果有特殊的要求建议新建个data1,然后用来做从存储目录(线上项目禁用rm,可通过mv移到垃圾箱)
[postgres@web2 ~]$ rm /usr/local/pgsql-10.5/data/* -r
[postgres@web2 ~]$
3、拷贝主数据库中的数据,注意指定的目录,如果新建了目录就指定到新建的目录。
[postgres@web2 ~]$ pg_basebackup -F p --progress -D /usr/local/pgsql-10.5/data/ -h 192.168.254.21 -p -U replica --password
Password:
/ kB (%), / tablespace
[postgres@web2 ~]$
***注这里要求输入密码,就是主数据库刚刚新建用户时建的密码(replica)***
4、复制配置文件并改名,注意文件路径及文件名称
[postgres@web2 ~]$ cp /usr/local/pgsql-10.5/share/recovery.conf.sample /usr/local/pgsql-10.5/data/recovery.conf
[postgres@web2 ~]$ cd /usr/local/pgsql-10.5/data/
[postgres@web2 data]$ ls
backup_label pg_dynshmem pg_logical pg_serial pg_subtrans pg_wal postgresql.conf.--
base pg_hba.conf pg_multixact pg_snapshots pg_tblspc pg_xact recovery.conf
global pg_hba.conf.-- pg_notify pg_stat pg_twophase postgresql.auto.conf
pg_commit_ts pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
[postgres@web2 data]$ ls /usr/local/pgsql-10.5/data/ |grep recovery.conf
recovery.conf
[postgres@web2 data]$
5、编辑recovery.conf文件,配置如下(修改是记得备份)
standby_mode = on #说明这台postgres,为从库
primary_conninfo = 'host=192.168.254.21 port=5432 user=replica password=replica' #配置要同步的ip及库用户还有密码
recovery_target_timeline = 'latest' #流复制同步到最新的数据
6、编辑postgresql.conf文件,配置如下
listen_addresses = '*' #这表示监听的ip,这里我设置监听所有ip,也可以设置成从本机ip
wal_level = hot_standby #注:这个名称要与主的一致
max_connections = #最大连接数要大于主的
hot_standby = on #说明这台机器不仅仅用于数据归档,也用于查询
max_standby_streaming_delay = 30s #数据流备份最大延迟
wal_receiver_status_interval = 1s #多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on #如果有错误的数据复制,是否向主进行反馈
7、配置完成后启动从数据库服务
[postgres@web2 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web2 data]$
***这里主从就配置完成啦,接下来就是测试***
测试
主:1、psql #进入数据器
2、\l #查看所有库
3、create database test; #新建库
4、\l #检查是否创建
[postgres@web1 data]$ psql
psql (10.5)
Type "help" for help. postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
name | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
postgres | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
template0 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres +
| | | | | postgres=CTc/postgres
( rows) postgres=# create database test;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
name | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
postgres | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
template0 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
( rows) postgres=#
从:1、psql #进入数据库
2、\l #查看库是否与主的一致
[postgres@web2 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web2 data]$ psql
psql (10.5)
Type "help" for help. postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
name | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
postgres | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
template0 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
( rows) postgres=#
***测试没问题哈,这个也是意料中的事情!如果上述博文有什么问题,希望大神读客指出来,欢迎在线批评!!!***
postgres主从配置的更多相关文章
- Postgres 主从配置(四)
Postgres 主从切换 数据库主从结构中由从库升级为主库较为容易些,但是主库恢复后重新加入到主从结构中就不那么容易了.以往的做法是当成一个全新的从库加入进来,数据需要重新从现有的主库中使用pg_b ...
- Postgres 主从配置(五)
PostgreSQL 9.4 新增的一个特性, replication slot, 1. 可以被流复制的sender节点用于自动识别它xlog中的数据在下面的standby中是否还需要(例如, st ...
- postgresql pgsql最新版安装指南及数据存储路径更改及主从配置
postgresql pgsql最新版安装指南及数据存储路径更改及主从配置 安装指南 首先在apt的list添加你当前系统版本对应的apt列表 目前官网有16.04,14.04,12.04 分别对应下 ...
- CentOS7 PostgreSQL 主从配置( 二)
同步流复制配置PostgreSql的流复制是异步的,缺点是Standby上的数据落后于主库上的数据,如果使用Hot Standby做读写分离,就会存在数据一致性的问题.PostgreSql9.1版本后 ...
- postgresql主从配置
master:10.0.1.114 slaver:10.0.1.116 一.yum安装https://blog.csdn.net/weixin_41048363/article/details/803 ...
- PostgreSQL9.6主从配置
参考文档: 备机日志传送:https://www.postgresql.org/docs/9.6/static/warm-standby.html 英文文档:https://www.postgresq ...
- mysql主从配置
引言: 双11,阿里云服务器打折,于是我忍不住又买了一台服务器,于是咱也是有两台服务器的爷们了,既然有了两台服务器,那么肯定要好好利用一下吧,那么就来玩玩mysql的主从配置吧. 准备 两台数据库服务 ...
- Redis——学习之路四(初识主从配置)
首先我们配置一台master服务器,两台slave服务器.master服务器配置就是默认配置 端口为6379,添加就一个密码CeshiPassword,然后启动master服务器. 两台slave服务 ...
- redis主从配置及主从切换
环境描述: 主redis:192.168.10.1 6379从redis:192.168.10.2 6380 一.主从配置 1.将主从redis配置文件redis.conf中的aemonize no ...
随机推荐
- 5分钟Serverless实践 | 构建无服务器图片鉴黄Web应用
Serverless是什么 Serverless中文译为“无服务器”,最早可以追溯到2012年Ken Fromm发表的<Why The Future Of Software And Apps I ...
- SOJ.Output the Yanghui triangel
Output the Yanghui triangel 总提交数量: 225 通过数量: 59 时间限制:1秒 内存限制:256兆 题目描写叙述 Writ ...
- hdu 5325 Crazy Bobo dfs
// hdu 5325 Crazy Bobo // // 题目大意: // // 给你一棵树,树上每一个节点都有一个权值w,选择尽可能多的节点, // 这些节点相互联通,而且依照权值升序排序之后得到节 ...
- 5313 [JL]判断邮箱地址 升级版
5313 [JL]判断邮箱地址 升级版 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 正确的邮箱地 ...
- 用R语言完成的交通可视化报告
http://sztocc.sztb.gov.cn/roadcongmore.aspx最终实现这几个图:1. 实时道路交通可视化2. 实时道路拥堵排名3. 历史路况时间序列图4. 每日每小时道况热力图 ...
- Windows的所有风格与扩展风格
SetWindowLonghttp://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspxWindow St ...
- 修改select默认样式
http://www.qkzone.com/code/2015-11-26/1.html
- 洛谷 P3806 点分治模板
题目:https://www.luogu.org/problemnew/show/P3806 就是点分治~ 每次暴力枚举询问即可,复杂度是 nmlogn: 注意 tmp[0]=1 ! 代码如下: #i ...
- MyBatis高级查询 一对一映射
drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...
- E20171011-hm
Rendering engine 渲染引擎; 引擎; 绘图引擎; interactive adj. 互动的; 互相作用的,相互影响的; [计] 交互式的; defer vt. 使推迟; 使延 ...