PostgreSQL14.6主从模式配置

news/2024/7/9 22:17:54 标签: postgresql, 数据库, 服务器

一、系统架构

roleip
master11.0.1.11
slave11.0.1.12

二、编译安装PostgreSQL
不多描述,参考:https://zhaoll.blog.csdn.net/article/details/128416411
注意:两台服务器的PG,master正常安装和初始化,slave无需初始化,编译安装完就行;

三、master配置:
修改ppostgresql.conf,下面只列出了需要修改的参数,其他未列出的参数使用默认值。

data_directory = '/data/pgsql'
hba_file = '/app/postgresql/conf/pg_hba.conf'
log_directory = '/app/postgresql/logs'
listen_addresses = '11.0.1.11'
port = 15432
max_connections = 10000 
unix_socket_directories = '/app/postgresql'
unix_socket_group = 'postgres'
unix_socket_permissions = 0777
wal_level = replica 
archive_mode = on
archive_command = 'test ! -f /app/postgresql/archive/%f && cp %p /app/postgresql/archive/%f'
max_wal_senders = 10
wal_keep_size = 256 
wal_sender_timeout = 60s 

注意上面“archive_command”参数中涉及到的目录,如果不存在,需要你手动创建,不然PG无法启动:

mkdir /app/postgresql/archive

修改pg_hba.conf,增加如下内容,允许slave进行数据同步:

host    replication     repuser         11.0.1.12/24         md5

然后启动PG,进行以下操作:
1、登录PG库:

psql -h 127.0.0.1 -p 15432

2、创建用户,用于进行主从同步操作

create role repuser login encrypted password 'repuser123' replication;

3、查看用户权限

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 itpux     |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 repuser   | Replication                                                | {}

四、slave配置
1、同步数据
在slave服务器上执行:

[postgres@data3 postgresql]$ bin/pg_basebackup -h 11.0.1.11 -p 15432 -U repuser -F p -P -D /data/pgsql/
Password:                       <<<<此处输入我们在master上创建的repuser的密码
34772/34772 kB (100%), 1/1 tablespace

输入密码后就开始从master上同步数据到slave上,同步完成后可以看到同步后的文件:

[postgres@data3 postgresql]$ ll /data/pgsql/
总用量 240
-rw-------. 1 postgres postgres    225 5月   2 15:30 backup_label
-rw-------. 1 postgres postgres 180267 5月   2 15:30 backup_manifest
drwx------. 6 postgres postgres     54 5月   2 15:30 base
drwx------. 2 postgres postgres   4096 5月   2 15:30 global
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_commit_ts
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_dynshmem
-rw-------. 1 postgres postgres   4928 5月   2 15:30 pg_hba.conf
-rw-------. 1 postgres postgres   1636 5月   2 15:30 pg_ident.conf
drwx------. 4 postgres postgres     68 5月   2 15:30 pg_logical
drwx------. 4 postgres postgres     36 5月   2 15:30 pg_multixact
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_notify
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_replslot
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_serial
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_snapshots
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_stat
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_stat_tmp
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_subtrans
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_tblspc
drwx------. 2 postgres postgres      6 5月   2 15:30 pg_twophase
-rw-------. 1 postgres postgres      3 5月   2 15:30 PG_VERSION
drwx------. 3 postgres postgres     60 5月   2 15:30 pg_wal
drwx------. 2 postgres postgres     18 5月   2 15:30 pg_xact
-rw-------. 1 postgres postgres     88 5月   2 15:30 postgresql.auto.conf
-rw-------. 1 postgres postgres  28868 5月   2 15:30 postgresql.conf

2、修改配置

su - postgres
cd /app/postgresql
mkdir conf && cd conf
ln -s /data/pgsql/postgresql.conf .
ln -s /data/pgsql/pg_hba.conf .

修改postgresql.conf,只需修改两处:

listen_addresses = '11.0.1.12'
primary_conninfo = 'host=11.0.1.11 port=15432 user=repuser password=Elk@95598'

然后启动slave上的PG库,查看日志有没有报错。

五、测试
在master上登录PG并创建testdb库,在slave上查看是否也存在testdb库。


http://www.niftyadmin.cn/n/282403.html

相关文章

Ubuntu使用gzip与bzip2与rar和tar压缩解压

目录 1、gzip压缩解压 2、bzip2压缩解压 3、rar压缩解压 tar归档压缩与解压包 1、gzip压缩解压 因为都是系统自带的我们不需要安装&#xff1b; ls 看一下目录当前的文件&#xff0c;创建一个 touch 123.txt 文件&#xff1b; gzip 进行压缩&#xff0c;&#xff08;“ …

【华为OD机试真题-老题库】超级玛丽通过吊桥的走法(C++Javapython)100%通过率 超详细代码注释 代码解读

超级玛丽通过吊桥的走法 题目描述 超级玛丽好不容易来到新的一关,有一个长长的吊桥,吊桥的尽头是下水管道,其中随机的木板存在缺失,旦踩到就会死亡,死亡后如果还有剩余的生命将在原地复活且不受木板缺失影响,会消耗一次生命,如果跨过了管道,将跌入悬崖,通关失败。 …

Latex 定理和证明类环境(amsthm)和(ntheorm)的区别

最近在写毕业论文&#xff0c;出现了一些定理和证明的环境的问题&#xff0c;问题出现在对两个包的理解程度不够的问题上&#xff1a; \RequirePackage{ntheorem} 1、\newtheorem*{proof}{\hspace{2em}证:} 这个是让证明失去计数原则&#xff0c;该命令不能用于 amsthm 2…

sql注入(三)绕过方法及防御手段

一、sql注入的绕过方法 1.注释符过滤绕过 常用的注释符有&#xff1a; 1&#xff09;-- 注释内容 2&#xff09;# 注释内容 3&#xff09;/* 注释内容 */ 绕过方法 构造闭合&#xff1a; ?id1 and sql语句 and 11 这样接收源码前面的引号被 id1 中的后引号所闭合&…

redis和memcache区别

1redis做存储&#xff0c;可以持久化&#xff0c;memcache做缓存&#xff0c;数据易丢失。 2 redis支持多数据类型&#xff0c;memcache存放字符串。 3 redis服务端仅支持单进程、单线程访问&#xff0c;也就是先来后到的串行模式&#xff0c;避免线程上下文切换&#xff0c;…

留守儿童爱心网站

摘要 随着留守儿童爱心管理的不断发展&#xff0c;留守儿童爱心网站在现实生活中的使用和普及&#xff0c;留守儿童爱心管理成为近年内出现的一个热门话题&#xff0c;并且能够成为大众广为认可和接受的行为和选择。设计留守儿童爱心网站的目的就是借助计算机让复杂的管理操作…

Markdown编辑器快捷方式

这里写自定义目录标题 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个…

4 异常机制--数组、切片、算法【Go语言教程】

4 异常机制–数组、切片、算法【Go语言教程】 1 异常机制 1.1 处理错误 Go 语言追求简洁优雅&#xff0c;所以&#xff0c;Go 语言不支持传统的 try…catch…finally 这种处理。Go 中引入的处理方式为&#xff1a;defer, panic, recover这几个异常的使用场景可以这么简单描述…