postgresql 最简主从配置

本文介绍如何在CentOS上配置PostgreSQL10.23的主从环境,包括安装、设置、权限管理和功能验证。
摘要由CSDN通过智能技术生成

实验目的

配置一个最简的主从环境,了解基本的主从配置。

环境参数

操作系统CentOS Linux release 7.9.2009 (Core)
数据库版本PostgreSQL 10.23
主库端口15431
备库端口15432
  • 因为只是做实验,所以主备库放在同一台机器上,仅通过端口区分主备
  • 操作的系统用户为pg,没有特殊说明的情况下操作命令都由pg用户执行

搭个环境

安装数据库

#没有yum源的请去pg官网找一下
[root@localhost /]# yum install postgresql10 postgresql10-server

初始化主库

[root@localhost /]# useradd pg
[root@localhost /]# mkdir /data
[root@localhost /]# chown -R pg.pg /data
[root@localhost /]# su - pg
[pg@localhost ~]$ pg_ctl -D /data/db1 init
The files belonging to this database system will be owned by user "pg".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /data/db1 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/pgsql-10/bin/pg_ctl -D /data/db1 -l logfile start

配置主库参数

[pg@localhost ~]$ vi /data/db1/postgresql.conf
修改如下参数:
---------------------------------------
port = 15431	#主库端口
unix_socket_directories = '/tmp'  #本地连接socket文件目录pg访问/var/run/postgresql会有权限制问题,反正不打算用本地socket方式连接所以直接改成/tmp
[pg@localhost ~]$ vi /data/db1/pg_hba.conf
确认已开启本地复制的权限,本地连接的所有用户都有备机的复制权限
---------------------------------------
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

初始化备库

备库的基准文件必须来自于主库,直接复制主库文件到备库位置即可。

[pg@localhost ~]$ rsync -avz /data/db1/* /data/db2

#修改主备库目录的权限为0700,否则会导致数据库启动异常
[pg@localhost ~]$ chmod 0700 /data/db[1,2]

配置备库参数

备库需求配置一个特定的参数文件recovery.conf,可以从安装目录中复制文件模板过来修改。

[pg@localhost ~]$ cp /usr/pgsql-10/share/recovery.conf.sample /data/db2/recovery.conf

[pg@localhost ~]$ vi /data/db2/recovery.conf
开启备库模式
---------------------------------------
standby_mode = on
primary_conninfo = 'host=localhost port=15431'
[pg@localhost ~]$ vi /data/db2/postgresql.conf
修改如下参数:
---------------------------------------
port = 15432
unix_socket_directories = '/tmp'

验证主备功能

#启动主库
[pg@localhost ~]$ pg_ctl -D /data/db1/ -l /data/db1/server.log start
waiting for server to start.... done
server started

#启动备库
[pg@localhost ~]$ pg_ctl -D /data/db2/ -l /data/db2/server.log start
waiting for server to start.... done
server started

#查看可用账号和库
[pg@localhost ~]$ psql -l -h localhost -p 15431
                              List of databases
   Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
 postgres  | pg    | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | pg    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/pg            +
           |       |          |             |             | pg=CTc/pg
 template1 | pg    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/pg            +
           |       |          |             |             | pg=CTc/pg
(3 rows)

[pg@localhost ~]$ psql -l -h localhost -p 15432
                              List of databases
   Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
 postgres  | pg    | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | pg    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/pg            +
           |       |          |             |             | pg=CTc/pg
 template1 | pg    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/pg            +
           |       |          |             |             | pg=CTc/pg
(3 rows)

#连接主库,建表
[pg@localhost ~]$ psql -h localhost -p 15431 -U pg postgres
psql (10.23)
Type "help" for help.

postgres=# create table t3(a int);
CREATE TABLE
postgres=# insert into t3(a) values (1),(2),(3);
INSERT 0 3

#连接备库查看看同步情况
[pg@localhost ~]$ psql -h localhost -p 15432 -U pg postgres
psql (10.23)
Type "help" for help.

postgres=# \dt
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 public | t3   | table | pg
(1 row)

postgres=# select * from t3;
 a
---
 1
 2
 3
(3 rows)

postgres=#
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PostgreSQL主从复制是一种常见的高可用性和数据备份解决方案。它可以确保在主节点故障时,从节点可以接管并继续提供服务。 要配置PostgreSQL主从复制,需要进行以下步骤: 1. 确保主节点和从节点上都已安装了PostgreSQL。 2. 在主节点上修改postgresql.conf文件,启用WAL(Write-Ahead Logging)并配置适当的参数。找到并修改以下参数: ``` wal_level = replica max_wal_senders = 10 wal_keep_segments = 10 ``` 这些参数将确保主节点生成WAL日志,并允许从节点连接并接收这些日志。 3. 在主节点上修改pg_hba.conf文件,允许从节点连接。找到并添加以下行: ``` host replication <从节点用户名> <从节点IP地址>/32 md5 ``` `<从节点用户名>`是从节点连接时使用的用户名,`<从节点IP地址>`是从节点的IP地址。 4. 在主节点上重启PostgreSQL服务以使更改生效。 5. 在从节点上创建一个空的数据目录。 6. 在从节点上创建一个recovery.conf文件,指定主节点的连接信息和复制模式。将以下内容添加到recovery.conf文件中: ``` standby_mode = 'on' primary_conninfo = 'host=<主节点IP地址> port=5432 user=<从节点用户名> password=<从节点密码>' trigger_file = '/path/to/trigger/file' ``` `<主节点IP地址>`是主节点的IP地址,`<从节点用户名>`和`<从节点密码>`是用于连接主节点的凭据。 7. 启动从节点上的PostgreSQL服务。 完成上述步骤后,主节点和从节点之间的复制将开始运行。你可以通过在主节点上执行一些写操作,然后在从节点上查看数据是否同步来验证复制是否成功。 请注意,以上步骤仅适用于基本的主从复制配置。在实际部署中,你可能还需要考虑故障转移、监控、自动故障恢复等方面的配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

24K老游

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值