PostgreSQL (1) PostgreSQL one-master two-slave cluster deployment

(1) Basic preparation

1.1 Create three virtual machines

Virtual machine name

IP

hostname

Master-slave division

pgpool0

192.168.149.236

node236

Master node

pgpool1

192.168.149.237

node237

Slave node 1

pgpool2

192.168.149.238

node238

Slave node 2

 1.2 Create three virtual machines

The software versions involved in the deployment of this article are shown in the following table.

Operating system release version

CentOS Linux release 7.8.2003 (Core)

Operating system version

Linux version 3.10.0-1127.el7.x86_64

PostgreSQL

9.2.24

pgpool-II

3.4.6

Local virtualization software

VMware® Workstation 15 Pro

(2) Install the software package 

2.1 Check the software version

Before installation, first check whether the software has been installed. input the command

rpm -qa | grep postgresql

Generally speaking, installing a system from a virtual machine will naturally not install PostgreSQL. Therefore the output is empty.

There are many ways to install the software package. For PostgreSQL, the method chosen in this article is to choose yum to install the default version of the software for later maintenance.

2.2 Install and initialize pogstresql

The installation process is the same for the three machines.

(1) Install postgresql postgresql-server

yum install -y postgresql postgresql-server

(2) View the installation results.

rpm -qa | grep postgresql

(3) Initialization

service postgresql initdb

(4) Start

systemctl start postgresql

(5) Open ports (can be omitted if the firewall is closed)

iptables -I INPUT -p tcp --dport 5432 -j ACCEPT

(6) To verify the installation results, first enter su-postgres to switch users, and then enter psql, as shown in the figure below, the installation is successful.

 

2.3 Master node configuration

(1) Create a user for master-slave access

# su - postgres

-bash-4.2$ psql

postgres=# create role repl login replication encrypted password 'postgres';

The result is shown in the figure.

(2) Add configuration content in pg_hba.conf and postgresql.conf

pg_hba.conf is the following configuration for access permissions, which needs to be configured according to the actual situation. In the first two lines, 192.168.149.237 and 192.168.149.238 are the IPs of the slave nodes, which are used for master-slave configuration. repl is the user just created. The last line is used for client login in the subnet.

# vi /var/lib/pgsql/data/pg_hba.conf

host    replication    repl    192.168.149.236/32    trust

host    replication    repl    192.168.149.237/32    trust

host    replication    repl    192.168.149.238/32    trust

host    all    all    192.168.149.0/24    md5

postgresql.conf is the configuration of postgresql itself.

# vi /var/lib/pgsql/data/postgresql.conf

addresses = '*'

wal_level = hot_standby

max_wal_senders= 6

wal_keep_segments = 10240

max_connections = 512

Parameter Description:

listen_addresses = '*'

可以连接服务器使用的IP,一般初始值为 localhost或者local,意味着只有本机可以连接数据库。这里一般设置为 " * " ,允许全部的IP连接数据库。

wal_level = hot_standby

wal_level中有三个主要的参数:minimal、archive和hot_standby

1.minimal是默认的值,它仅写入崩溃或者突发关机时所需要的信息(不建议使用)。

2.archive是增加wal归档所需的日志(最常用)。

3.hot_standby是在备用服务器上增加了运行只读查询所需的信息,一般是在流复制的时候使用到。

max_wal_senders= 6

最多多少个流复制链接

wal_keep_segments = 10240

流复制保留最多的xlog数,xlog这个日志是记录的Postgresql的WAL信息

max_connections = 512

允许的最大并发连接数,简单来说就是同时多少客户端能连接你的数据库。

(3)重启主节点

建议先停止,再启动,而不是重启。之后再验证一下是否启动成功

systemctl stop postgresql

systemctl start postgresql

2.4  从节点配置

从节点的操作建议全部在postgres用户下进行。

(1)切换postgres用户

su - postgres

(2)对主节点的数据进行备份,其中192.168.149.236对应主机IP,repl是上一节主节点创建的用户。

-bash-4.2$ rm -rf /var/lib/pgsql/data/*

-bash-4.2$ pg_basebackup -h 192.168.149.236 -U repl -D /var/lib/pgsql/data -X stream -P

(3)拷贝recovery.conf,编辑recovery.conf内容,其中192.168.149.236对应主机IP,repl是上一节主机创建的用户。

-bash-4.2$ cp /usr/share/pgsql/recovery.conf.sample /var/lib/pgsql/data/recovery.conf

-bash-4.2$ vi /var/lib/pgsql/data/recovery.conf

standby_mode = on

primary_conninfo = 'host=192.168.149.236 port=5432 user=repl password=ct-sfs'

recovery_target_timeline = 'latest'

参数说明。

standby_mode

开启standby模式

primary_conninfo

主节点信息

recovery_target_timeline = 'latest'

指定恢复到一个特定的时间线中。默认值是沿着基础备份建立时的当前时间线恢复。将这个参数设置为latest会恢复到该归档中能找到的最新的时间线。

trigger_file = '/tmp/trigger_file0'

'/tmp/trigger_file0'是一个自定义的文件,在后面主从切换的时候能够用得上。倘若检测到该文件的创建,则PostgreSQL由主节点的状态切换为主。如果下文在failover_stream.sh采用文件触发的办法,则必须配置此项,若采用命令触发方法则无需配置此项,但配置此项对命令方式没有影响。

(4)在postgresql.conf中添加一行,用于开启standby模式。

-bash-4.2$ vi /var/lib/pgsql/data/postgresql.conf

hot_standby = on

(5)退出postgres用户,重启PostgreSQL

(三)验证主从

3.1查看从节点信息

在主节点切换至psql界面,输入命令。

select client_addr,sync_state from pg_stat_replication;

若两个从节点全部显示出来,如下图所示,则说明PostgreSQL集群搭建成功。

3.2 读写测试

(1)在主节点写数据,从节点读数据。

在主节点,切换到psql界面

create database test;

可以看见提示创建成功。

在从节点上查看分别在创建之前和创建之后查看数据库。可以看见,数据库同步了。

(2)在从节点写数据

可以看见,提示,从节点是个只读数据库。

 

 

Guess you like

Origin blog.csdn.net/Kangyucheng/article/details/108543370