postgresql 主备配置

主备服务器信息

主:192.168.9.113  centos7.9

备:192.168.9.114  centos7.9

一、主节点配置

1.安装postgresql-14服务

# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
sudo yum install -y postgresql14-server

# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14

2.创建用户复制的专用用户和修改postgres用户密码

su - postgres
psql
ALTER USER postgres with encrypted password 'postgres'; #修改postgres密码
create user repl login replication encrypted password 'repl'; #创建主从复制的专用用户repl

3.修改主配置文件postgresql.conf

vim $PGDATA/postgresql.conf

listen_addresses = '*'
wal_level = replica
max_wal_senders = 10     # 最大发送进程,默认 10,读写分离一写多服务读,请设置为读数据库的数量
synchronous_commit = on  # 将事务提交方式改为本地提交,默认为 on,在 on 模式下事务需要等备份数据库一起提交,这里改为 local 让备份数据库不影响主库,如若是先配置读写分离,请设置为默认 on
修改主配置文件pg_hba.conf
vim $PGDATA/pg_hba.conf

host all all 0.0.0.0/0 scram-sha-256 #允许其他主机连接该数据库,记住防火墙也要开放5432端口才能连接,或者直接关闭防火墙
host all all 192.168.9.0/24 trust
host replication repl 192.168.9.0/24 md5  #其中repl就是创建的用户

4.重启主节点

systemctl restart postgresql-14

主节点就配置完成了

二、从节点配置

 1.安装postgresql-14服务

# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
sudo yum install -y postgresql14-server

# Optionally initialize the database and enab
首先,您需要在两台服务器上安装Docker和Docker Compose。然后,您可以按照以下步骤在两台服务器上安装postgresql: 1. 在两台服务器上创建一个文件夹,用于存放docker-compose.yml文件和postgresql数据。 2. 创建docker-compose.yml文件,配置两个postgresql容器,并将它们连接成一个模式。下面是一个示例docker-compose.yml文件: ``` version: '3' services: master: image: postgres:latest restart: always environment: POSTGRES_PASSWORD: example volumes: - ./master_data:/var/lib/postgresql/data networks: - postgresql_network slave: image: postgres:latest restart: always environment: POSTGRES_PASSWORD: example POSTGRES_SLAVE: "true" POSTGRES_MASTER_HOST: master POSTGRES_MASTER_PORT: 5432 volumes: - ./slave_data:/var/lib/postgresql/data depends_on: - master networks: - postgresql_network networks: postgresql_network: ``` 在这个文件中,我们定义了两个postgresql容器,一个是容器,另一个是容器。容器被命名为master,容器被命名为slave。我们还定义了POSTGRES_SLAVE环境变量来指示容器是从容器,并且必须连接到容器。我们还定义了POSTGRES_MASTER_HOST和POSTGRES_MASTER_PORT环境变量来指定容器的地址和端口。 3. 在两台服务器上运行以下命令,以启动postgresql容器: ``` docker-compose up -d ``` 这将启动两个容器,一个是容器,另一个是容器。 4. 配置容器以允许从容器复制数据。在容器中的postgresql.conf文件中添加以下配置: ``` listen_addresses = '*' wal_level = replica max_wal_senders = 10 ``` 5. 配置容器以连接到容器并复制数据。在容器中的recovery.conf文件中添加以下配置: ``` standby_mode = on primary_conninfo = 'host=master port=5432 user=postgres password=example' trigger_file = '/var/lib/postgresql/data/failover.trigger' ``` 这将使容器连接到容器并复制数据。如果容器无法正常工作,则会在failover.trigger文件中创建一个标志文件,并且容器将自动接管为容器。 6. 在两台服务器上启动postgresql服务: ``` docker exec -it <container-name> /bin/bash service postgresql start ``` 在这里,<container-name>是您的postgresql容器名称。 现在您已经成功地安装了postgresql模式。您可以测试它是否正常工作,例如在节点创建一个表,然后在节点上查询该表是否存在。如果节点无法正常工作,则节点将自动接管并成为新的节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值