# 安装 repository 源:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 安装 PostgreSQL13:
sudo yum install -y postgresql13-server
# 初始化数据库,并设置启动命令和开机启动:
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
# 另外,查看状态,关闭命名和重启命令
sudo systemctl status postgresql-13
sudo systemctl stop postgresql-13
sudo systemctl restart postgresql-13
sudo -u postgres /usr/pgsql-13/bin/psql -c "select pg_is_in_recovery()"
主从节点可以自己规划好,准备好不同机器就行,也可以使用最小集群一主一从
主要是修改2个配置文件,都在data目录下,一个是数据库基础配置,一个是访问权限控制
默认初始化的数据库,data目录在/var/lib/pgsql/13/data
pg_hba.conf
,设置 Replication 访问策略,允许流复制# replication privilege.
# 追加一条“允许postgres用户,通过全部网络地址使用 Replication ”的策略
host replication postgres 0.0.0.0/0 trust
postgres.conf
,进行一些参数设置,允许流复制# -----------------------------
# PostgreSQL configuration file
# -----------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
#port = 5432 # (change requires restart)
max_connections = 200 # (change requires restart)
# - Authentication -
#authentication_timeout = 1min # 1s-600s
password_encryption = scram-sha-256 # md5 or scram-sha-256
#------------------------------------------------------------------------------
# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------
# - Memory -
shared_buffers = 1GB # min 128kB 官方推荐内存设置带大小为系统内存的1/4
temp_buffers = 64MB # min 800kB
work_mem = 64MB # min 64kB
max_stack_depth = 4MB # min 100kB
dynamic_shared_memory_type = posix # the default is the first option
#------------------------------------------------------------------------------
# WRITE-AHEAD LOG
#------------------------------------------------------------------------------
wal_level = replica # minimal, replica, or logical
fsync = on # flush data to disk for crash safety
synchronous_commit = on # synchronization level;
wal_log_hints = on # also do full page writes of non-critical updates
# - Checkpoints -
checkpoint_timeout = 10min # range 30s-1d
max_wal_size = 1GB
min_wal_size = 80MB
# - Archiving -
archive_mode = on # enables archiving; off, on, or always
# (change requires restart)
archive_command = 'cp %p /data/postgresql/archive/%f' # command to use to archive a logfile segment
#-----------------------------------------------------------------------------
# REPLICATION
#------------------------------------------------------------------------------
max_wal_senders = 10 # max number of walsender processes 设置了可以最多有几个流复制的链接
# (change requires restart)
wal_keep_size = 1024 # in megabytes; 0 disables
max_slot_wal_keep_size = 10 # in megabytes; -1 disables
wal_sender_timeout = 120s # in milliseconds; 0 disables
# - Standby Servers -
hot_standby = on # "off" disallows queries during recovery
#------------------------------------------------------------------------------
# REPORTING AND LOGGING
#------------------------------------------------------------------------------
# - Where to Log -
log_destination = 'stderr' # Valid values are combinations of
logging_collector = on # Enable capturing of stderr and csvlog
log_directory = 'log' # directory where log files are written,
# can be absolute or relative to PGDATA
log_filename = 'postgresql-%a.log' # log file name pattern,
log_truncate_on_rotation = on # If on, an existing log file with the
log_rotation_age = 1d # Automatic rotation of logfiles will
log_rotation_size = 50MB # Automatic rotation of logfiles will
log_min_duration_statement = 2000 # 配置数据库慢查询时间,如果超过配置的时间,就会将查询的SQL语句记录到日志
# - What to Log -
log_connections = on
log_disconnections = on
log_line_prefix = '%m [%p] ' # special values:
log_timezone = 'Asia/Shanghai'
datestyle = 'iso, mdy'
#intervalstyle = 'postgres'
timezone = 'Asia/Shanghai'
# These settings are initialized by initdb, but they can be changed.
lc_messages = 'en_US.UTF-8' # locale for system error message
# strings
lc_monetary = 'en_US.UTF-8' # locale for monetary formatting
lc_numeric = 'en_US.UTF-8' # locale for number formatting
lc_time = 'en_US.UTF-8' # locale for time formatting
# default configuration for text search
default_text_search_config = 'pg_catalog.english'
shared_preload_libraries = '' # citus (change requires restart)
sudo systemctl start postgresql-13
# 或
sudo systemctl restart postgresql-13
主从要保持一致,主要是 data 保持一致,我们可以直接将主节点data同步到从节点
sudo systemctl stop postgresql-13
cd /var/lib/pgsql/13/data
rm -rf *
pg_basebackup
命令将主节点的data同步过来sudo -u postgres /usr/pgsql-13/bin/pg_basebackup -h 192.168.1.76 -U postgres -p 5432 -F p -X s -v -P -R -D /var/lib/pgsql/13/data
其中:-Fp
表示以plain
格式数据,-Xs
表示以stream
方式包含所需的WAL
文件,-P
表示显示进度,-R
表示为replication
写配置信息。
备份完成,在数据库实例目录下自动生成standby.signal
“信号”文件,并在postgresql.auto.conf
文件写入了主库的连接信息
4. 以上就配置完了,启动服务,验证是否为从节点,会得到t
,即true,为从节点
sudo systemctl start postgresql-13
sudo -u postgres /usr/pgsql-13/bin/psql -c "select pg_is_in_recovery()"
sudo -u postgres /usr/pgsql-13/bin/psql -x -c "select * from pg_stat_replication" -d postgres
ddl
操作和增删改查,从节点只允许数据查询,结构和数据已经实时同步postgres.auto.conf
,将之前生成的 主从信息primary_conninfo
注释掉或者删掉,重启服务使之生效pg_ctl promote
,在某个从节点执行,将该节点升格为主节点sudo -u postgres /usr/pgsql-13/bin/pg_ctl promote -D /var/lib/pgsql/13/data
此节点执行前为从节点,执行后,变为主节点
3. 注意:此时已经没有主从集群了,只是这个从节点可以读写了,并不会将数据同步到另一个从节点
5. 按照之前主从设置步骤,将坏掉的主节点和剩余的从节点服务停止,将data目录清空,使用pg_basebackup
命令将主节点的data同步过来,之后启动服务
6. 验证,现在去查看是否为从节点和集群状态,会发现新集群已生效。去验证增删改查,会发现结构和数据都能同步了,切换完成
如果一开始只有一个主节点或一主一从,查询较多,存在慢查询,数据库服务压力过大,可以考虑扩容。
新节点扩容很简单,按照之前步骤,安装数据库,使用pg_basebackup
即可。