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 ...
随机推荐
- FOBiz组合模糊查询
List list= delegator.findList("Entity",condition , null, null, null, false);其中condition为:组 ...
- 将分布式-队列的实现交给redis
import requestsimport reimport timefrom redis import Redisimport threading REDIS_HOST, REDIS_PORT, P ...
- bzoj3175: [Tjoi2013]攻击装置&&4808: 马
终于知道为啥网络流这么受欢迎了. 其实就是构个图模板一下的事儿,比较好打是吧. 然后这题网络流黑白染色(其实感觉上匈牙利更加直接好想啊,但是实际上黑白染色给人感觉就是二分图) st连白而ed连黑,流量 ...
- Spark2.0 VS Spark 1.* -------SparkSession的区别
Spark .0以前版本: val sparkConf = new SparkConf().setAppName("soyo") val spark = new SparkCont ...
- 【RAID在数据库存储上的应用 】
随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...
- android 中activity 属性说明(转载)
转自:http://liuwuhen.iteye.com/blog/1759796 activity是android中使用非常平凡的一种组件,我们除了需要掌握activity中的生命周期以外,还需要掌 ...
- PCB genesis大孔加小孔(即卸力孔)实现方法
一.为什么 大孔中要加小孔(即卸力孔) 这其实跟钻刀的排屑有关了,当钻刀越大孔,排屑量也越大(当然这也得跟转速,下刀速的参数有关系),通常当钻刀越大,转速越慢,下刀速也越慢(因为要保证它的排屑通畅). ...
- ecshop的一些东西,来看看
\includes\lib_commom.php =>公用函数库 \includes\lib_main.php =>前台公用函数库 \includes\lib_init.php => ...
- printf的字符型
参 数 说 明 %s 按实际宽度输出一个字符串 %ms m指定宽度(不足时左补空格,大于时按实际宽度输出) %-ms 左对齐,不足时右补空格 %m.ns 输出占m个字符位置,其中字符数最多n个,左 ...
- django 菜单权限
一.什么是权限 能做哪些事情,不能做哪些事情,可以做的权限 二.设计权限 思路: web应用中,所谓的权限,其实就是一个用户能够访问的url,通过对用户访问的url进行控制,从而实现对用户权限的控制. ...