PostgreSQL的一主一从集群搭建部署 (同步)
一、实验环境
| 虚拟机名 | IP | 身份 | 简称 |
|---|---|---|---|
| keep-postgres12-node1 | 192.168.122.87 | 主节点 | node1 |
| keep-postgres12-node2 | 192.168.122.89 | 备节点 | node2 |
二、安装数据库
源码包方式(主)
1、创建用户
[root@keep-postgres12-node1 ~]# groupadd postgres
[root@keep-postgres12-node1 ~]# useradd -g postgres postgres -m -s /bin/bash
[root@keep-postgres12-node1 ~]# echo "Database@123" | passwd --stdin postgres
2、修改服务器内核信息
[root@keep-postgres12-node1 ~]# echo '# 最大共享内存段大小 (默认值68719476736)
kernel.shmmax = 68719476736
# 可以使用的共享内存的总量 (默认值4294967296)
kernel.shmall = 4294967296
# 整个系统共享内存段的最大数目
kernel.shmmni = 4096
# 每个信号对象集的最大信号对象数
kernel.sem = 50100 64128000 50100 1280
# 文件句柄的最大数量
fs.file-max = 7672460
# 应用程序可使用的IPv4端口范围
net.ipv4.ip_local_port_range = 9000 65000
# 套接字接收缓冲区大小的缺省值
net.core.rmem_default = 1048576
# 套接字发送缓冲区大小的缺省值
net.core.wmem_default = 262144
# 套接字发送缓冲区大小的最大值
net.core.wmem_max = 1048576' >> /etc/sysctl.conf# 使参数生效
[root@keep-postgres12-node1 ~]# sysctl -p
3、安装依赖包
[root@keep-postgres12-node1 ~]# yum install gcc gcc-c++ zlib-devel readline-devel perl-ExtUtils-Embed pam-devel openssl openssl-devel cmake libxslt-devel libxml2-devel openldap-devel python-devel tcl tcl-devel bison flex xmlto -y
4、创建数据库目录
[root@keep-postgres12-node1 ~]# mkdir /data
[root@keep-postgres12-node1 ~]# mkdir /data/postgres12.2
[root@keep-postgres12-node1 ~]# chown -R postgres: /data
5、创建环境变量
[root@keep-postgres12-node1 ~]# su - postgres
[postgres@keep-postgres12-node1 ~]$ echo 'export PGPORT=5432
export PG_HOME=/data/postgres12.2
export PATH=$PG_HOME/bin:$PATH
export PGDATA=$PG_HOME/data
export LD_LIBRARY_PATH=$PG_HOME/lib
export LANG=en_US.utf8' >> ~/.bash_profile && source ~/.bash_profile
6、下载源码包,并解压
链接1:https://ftp.postgresql.org/pub/source
链接2:https://www.postgresql.org/ftp/source/
# 安装postgresql-12.2版本
# 下载postgresql-12.2源码包
[postgres@keep-postgres12-node1 ~]$ cd /data
[postgres@keep-postgres12-node1 data]$ wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2
# 解压源码包
[postgres@keep-postgres12-node1 data]$ tar -xf postgresql-12.2.tar.bz2
7、编译源码包
[postgres@keep-postgres12-node1 postgresql-12.2]$ ./configure --prefix=/data/postgres12.2 --with-pgport=5432 --with-openssl --with-perl \
--with-tcl --with-python --with-pam --without-ldap --with-libxml --with-libxslt \
--enable-thread-safety --with-wal-blocksize=16 --with-blocksize=8
# 包括第三方插件全部编译,包含文档和所有的contirb
[postgres@keep-postgres12-node1 postgresql-12.2]$ gmake world
8、安装数据库
[postgres@keep-postgres12-node1 postgresql-12.2]$ make && make install
9、配置wal日志目录
[postgres@keep-postgres12-node1 postgresql-12.2]$ # 用于保存wal日志
[postgres@keep-postgres12-node1 postgresql-12.2]$ mkdir $PG_HOME/arch
10、初始化数据库
[postgres@keep-postgres12-node1 postgresql-12.2]$ # 创建目录
[postgres@keep-postgres12-node1 postgresql-12.2]$ mkdir $PG_HOME/data
[postgres@keep-postgres12-node1 postgresql-12.2]$ # 初始化数据库集簇
[postgres@keep-postgres12-node1 postgresql-12.2]$ # --data-checksums 主从复制时需要
[postgres@keep-postgres12-node1 postgresql-12.2]$ initdb -D $PGDATA -W --data-checksums
11、启动数据库
[postgres@keep-postgres12-node1 postgresql-12.2]$ pg_ctl -D $PGDATA start
12、创建数据库用户和数据库
[postgres@keep-postgres12-node1 data]$ psql -d postgres -p 5432
postgres=# create database testdb;
postgres=# create user keep with Superuser password 'keep';
13、配置远程连接
# 配置连接
[postgres@keep-postgres12-node1 data]$ cd $PGDATA
[postgres@keep-postgres12-node1 data]$ sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" postgresql.conf
[postgres@keep-postgres12-node1 data]$ echo 'host all all 0.0.0.0/0 md5' >> pg_hba.conf
# 重启数据库
[postgres@keep-postgres12-node1 data]$ pg_ctl stop
[postgres@keep-postgres12-node1 data]$ pg_ctl -D $PGDATA start
# 验证远程连接
[postgres@keep-postgres12-node1 data]$ psql -d testdb -U keep -p 5432 -h 192.168.140.96
14、配置数据库日志
[postgres@keep-postgres12-node1 data]$ echo "# Add settings for extensions here
log_destination='csvlog'
logging_collector=on
log_directory='pg_log'
log_filename='postgresql-%Y-%m-%d.log'
log_truncate_on_rotation=off
log_rotation_age=1d
log_rotation_size=0
log_error_verbosity=verbose" >> $PGDATA/postgresql.conf
源码包方式(备)
1、创建用户
[root@keep-postgres12-node2 ~]# groupadd postgres
[root@keep-postgres12-node2 ~]# useradd -g postgres postgres -m -s /bin/bash
[root@keep-postgres12-node2 ~]# echo "Database@123" | passwd --stdin postgres
2、修改服务器内核信息
[root@keep-postgres12-node2 ~]# echo '# 最大共享内存段大小 (默认值68719476736)
kernel.shmmax = 68719476736
# 可以使用的共享内存的总量 (默认值4294967296)
kernel.shmall = 4294967296
# 整个系统共享内存段的最大数目
kernel.shmmni = 4096
# 每个信号对象集的最大信号对象数
kernel.sem = 50100 64128000 50100 1280
# 文件句柄的最大数量
fs.file-max = 7672460
# 应用程序可使用的IPv4端口范围
net.ipv4.ip_local_port_range = 9000 65000
# 套接字接收缓冲区大小的缺省值
net.core.rmem_default = 1048576
# 套接字发送缓冲区大小的缺省值
net.core.wmem_default = 262144
# 套接字发送缓冲区大小的最大值
net.core.wmem_max = 1048576' >> /etc/sysctl.conf# 使参数生效
[root@keep-postgres12-node2 ~]# sysctl -p
3、安装依赖包
[root@keep-postgres12-node2 ~]# yum install gcc gcc-c++ zlib-devel readline-devel perl-ExtUtils-Embed pam-devel openssl openssl-devel cmake libxslt-devel libxml2-devel openldap-devel python-devel tcl tcl-devel bison flex xmlto -y
4、创建数据库目录
[root@keep-postgres12-node2 ~]# mkdir /data
[root@keep-postgres12-node2 ~]# mkdir /data/postgres12.2
[root@keep-postgres12-node2 ~]# chown -R postgres: /data
5、创建环境变量
[root@keep-postgres12-node2 ~]# su - postgres
[postgres@keep-postgres12-node2 ~]$ echo 'export PGPORT=5432
export PG_HOME=/data/postgres12.2
export PATH=$PG_HOME/bin:$PATH
export PGDATA=$PG_HOME/data
export LD_LIBRARY_PATH=$PG_HOME/lib
export LANG=en_US.utf8' >> ~/.bash_profile && source ~/.bash_profile
6、下载源码包,并解压
链接1:https://ftp.postgresql.org/pub/source
链接2:https://www.postgresql.org/ftp/source/
# 安装postgresql-12.2版本
# 下载postgresql-12.2源码包
[postgres@keep-postgres12-node2 ~]$ cd /data
[postgres@keep-postgres12-node2 data]$ wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2
# 解压源码包
[postgres@keep-postgres12-node2 data]$ tar -xf postgresql-12.2.tar.bz2
7、编译源码包
[postgres@keep-postgres12-node2 postgresql-12.2]$ ./configure --prefix=/data/postgres12.2 --with-pgport=5432 --with-openssl --with-perl \
--with-tcl --with-python --with-pam --without-ldap --with-libxml --with-libxslt \
--enable-thread-safety --with-wal-blocksize=16 --with-blocksize=8
# 包括第三方插件全部编译,包含文档和所有的contirb
[postgres@keep-postgres12-node2 postgresql-12.2]$ gmake world
8、安装数据库
[postgres@keep-postgres12-node2 postgresql-12.2]$ make && make install
9、配置wal日志目录
[postgres@keep-postgres12-node1 postgresql-12.2]$ # 用于保存wal日志
[postgres@keep-postgres12-node1 postgresql-12.2]$ mkdir $PG_HOME/arch
配置互信
1、配置主机名
# keep-postgres12-node1
[root@keep-postgres12-node1 ~]# echo '192.168.122.87 keep-postgres12-node1
192.168.122.88 keep-postgres12-node2' >> /etc/hosts# keep-postgres12-node2
[root@keep-postgres12-node2 ~]# echo '192.168.122.87 keep-postgres12-node1
192.168.122.88 keep-postgres12-node2' >> /etc/hosts
2、配置postgres用户互信
# keep-postgres12-node1
[postgres@keep-postgres12-node1 ~]$ ssh-keygen
[postgres@keep-postgres12-node1 ~]$ ssh-copy-id keep-postgres12-node1
[postgres@keep-postgres12-node1 ~]$ ssh-copy-id keep-postgres12-node2# 验证
[postgres@keep-postgres12-node1 ~]$ ssh keep-postgres12-node1
[postgres@keep-postgres12-node1 ~]$ ssh keep-postgres12-node2
# keep-postgres12-node2
[postgres@keep-postgres12-node2 ~]$ ssh-keygen
[postgres@keep-postgres12-node2 ~]$ ssh-copy-id keep-postgres12-node1
[postgres@keep-postgres12-node2 ~]$ ssh-copy-id keep-postgres12-node2# 验证
[postgres@keep-postgres12-node2 ~]$ ssh keep-postgres12-node1
[postgres@keep-postgres12-node2 ~]$ ssh keep-postgres12-node2
三、修改主节点配置
1、创建同步用户
# 数据库需要在线状态
[postgres@keep-postgres12-node1 ~]$ psql -c "create role replrole login replication encrypted password 'ReplRole@123';"
2、配置$PGDATA/pg_hba.conf
[postgres@keep-postgres12-node1 ~]$ echo 'host replication replrole keep-postgres12-node1 trust
host replication replrole keep-postgres12-node2 trust
' >> $PGDATA/pg_hba.conf
3、修改数据库参数
[postgres@keep-postgres12-node1 ~]$ echo "wal_level = replica
max_wal_senders=20
wal_keep_segments =64
# 开启归档
archive_mode = on
archive_command = 'cp %p /data/postgres12.2/arch/%f'
restore_command = 'cp /data/postgres12.2/arch/%f %p'
recovery_target_timeline = 'latest'
# full_page_writes是控制是否开启全页写入
full_page_writes = on
# 将每个磁盘页的全部内容写入到WAL
wal_log_hints = on
synchronous_standby_names = 'standby_pg2'
# 默认值,可以设置为remote_write,对主库性能有利
# 开启同步模式,此刻需要强同步,数据无法入库
synchronous_commit = on
" >> $PGDATA/postgresql.conf
4、重启数据库
[postgres@keep-postgres12-node1 ~]$ pg_ctl restart
四、修改备节点配置
1、进行数据恢复
[postgres@keep-postgres12-node2 ~]$ pg_basebackup -h keep-postgres12-node1 -p 5432 -U replrole -R -F p -P -D $PG_HOME/data
2、修改standby.signal配置
[postgres@keep-postgres12-node2 data]$ cd $PGDATA
[postgres@keep-postgres12-node2 data]$ cat standby.signal
primary_conninfo = 'host=keep-postgres12-node1 port=5432 user=replrole password=ReplRole@123 application_name=standby_pg2 options=''-c wal_sender_timeout=5000'''
restore_command = 'cp /data/postgres12.2/arch/%f %p'
archive_cleanup_command = 'pg_archivecleanup /data/postgres12.2/arch %r'
standby_mode = on
3、修改postgresql.auto.conf配置
# 注意:强同步需要注意在postgresql.auto.conf中加入application_name=standby_pg2
[postgres@keep-postgres12-node2 data]$ cat postgresql.auto.conf
primary_conninfo = 'user=replrole passfile=''/home/postgres/.pgpass'' host=''keep-postgres12-node1'' application_name=standby_pg2 port=5432 sslmode=prefer sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
五、启动数据同步
# 注意:主库修改synchronous_standby_names = ‘standby_pg2’ 后,需等待备库接收主库发送的WAL日志流并写入WAL文件,之后才向客户端返回成功。
1、主库检查点
[postgres@keep-postgres12-node1 ~]$ pg_controldata
pg_control version number: 1201
Catalog version number: 201909212
Database system identifier: 7435168474920575893
Database cluster state: in production
pg_control last modified: Mon 02 Dec 2024 11:14:28 AM CST
Latest checkpoint location: 0/1F000028
Latest checkpoint's REDO location: 0/1F000028
Latest checkpoint's REDO WAL file: 00000003000000000000001F
Latest checkpoint's TimeLineID: 3
Latest checkpoint's PrevTimeLineID: 3
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0:609
Latest checkpoint's NextOID: 16643
Latest checkpoint's NextMultiXactId: 1
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 480
Latest checkpoint's oldestXID's DB: 1
Latest checkpoint's oldestActiveXID: 0
Latest checkpoint's oldestMultiXid: 1
Latest checkpoint's oldestMulti's DB: 1
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Time of latest checkpoint: Mon 02 Dec 2024 11:14:27 AM CST
Fake LSN counter for unlogged rels: 0/3E8
Minimum recovery ending location: 0/0
Min recovery ending loc's timeline: 0
Backup start location: 0/0
Backup end location: 0/0
End-of-backup record required: no
wal_level setting: replica
wal_log_hints setting: on
max_connections setting: 100
max_worker_processes setting: 8
max_wal_senders setting: 20
max_prepared_xacts setting: 0
max_locks_per_xact setting: 64
track_commit_timestamp setting: off
Maximum data alignment: 8
Database block size: 8192
Blocks per segment of large relation: 131072
WAL block size: 16384
Bytes per WAL segment: 16777216
Maximum length of identifiers: 64
Maximum columns in an index: 32
Maximum size of a TOAST chunk: 1996
Size of a large-object chunk: 2048
Date/time type storage: 64-bit integers
Float4 argument passing: by value
Float8 argument passing: by value
Data page checksum version: 1
Mock authentication nonce: 2fd610c9c82cf604c47448c0e7c842aa9ac4944e8fea828fcc0d2425b6f3a6e4
2、启动备库
[postgres@keep-postgres12-node2 data]$ pg_ctl start
3、查看备库日志
[postgres@keep-postgres12-node2 pg_log]$ tail -f postgresql-2024-12-02.csv2024-12-02 11:19:12.451 CST,,,25290,,674d2730.62ca,2,,2024-12-02 11:19:12 CST,,0,LOG,00000,"entering standby mode",,,,,,,,"StartupXLOG, xlog.c:6324",""
2024-12-02 11:19:12.462 CST,,,25290,,674d2730.62ca,3,,2024-12-02 11:19:12 CST,1/0,0,LOG,00000,"redo starts at 0/1F0000A0",,,,,,,,"StartupXLOG, xlog.c:7037",""
2024-12-02 11:19:12.462 CST,,,25290,,674d2730.62ca,4,,2024-12-02 11:19:12 CST,1/0,0,LOG,00000,`"consistent recovery state reached at 0/1F000350",,,,,,,,"CheckRecoveryConsistency, xlog.c:7880",""
2024-12-02 11:19:12.462 CST,,,25290,,674d2730.62ca,5,,2024-12-02 11:19:12 CST,1/0,0,LOG,00000,"invalid record length at 0/1F000350: wanted 24, got 0",,,,,,,,"ReadRecord, xlog.c:4284",""
2024-12-02 11:19:12.462 CST,,,25288,,674d2730.62c8,2,,2024-12-02 11:19:12 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,"sigusr1_handler, postmaster.c:5153",""
2024-12-02 11:19:12.473 CST,,,25297,,674d2730.62d1,1,,2024-12-02 11:19:12 CST,,0,LOG,00000,"started streaming WAL from primary at 0/1F000000 on timeline 3",,,,,,,,"WalReceiverMain, walreceiver.c:371",""
4、检查数据库进程
# keep-postgres12-node1
[postgres@keep-postgres12-node1 ~]$ ps -ef | grep postgres:
postgres 28608 28601 0 11:14 ? 00:00:00 postgres: logger
postgres 28610 28601 0 11:14 ? 00:00:00 postgres: checkpointer
postgres 28611 28601 0 11:14 ? 00:00:00 postgres: background writer
postgres 28612 28601 0 11:14 ? 00:00:00 postgres: walwriter
postgres 28613 28601 0 11:14 ? 00:00:00 postgres: autovacuum launcher
postgres 28614 28601 0 11:14 ? 00:00:00 postgres: archiver
postgres 28615 28601 0 11:14 ? 00:00:00 postgres: stats collector
postgres 28616 28601 0 11:14 ? 00:00:00 postgres: logical replication launcher
postgres 29485 28601 0 11:18 ? 00:00:00 postgres: postgres testdb [local] idle
postgres 29845 28601 0 11:19 ? 00:00:00 postgres: `walsender replrole 192.168.122.89(54086) streaming 0/1F000438`# keep-postgres12-node2
[postgres@keep-postgres12-node2 data]$ ps -ef | grep postgres:
postgres 25289 25288 0 11:19 ? 00:00:00 postgres: logger
postgres 25290 25288 0 11:19 ? 00:00:00 postgres: startup recovering 00000003000000000000001F
postgres 25294 25288 0 11:19 ? 00:00:00 postgres: checkpointer
postgres 25295 25288 0 11:19 ? 00:00:00 postgres: background writer
postgres 25296 25288 0 11:19 ? 00:00:00 postgres: stats collector
postgres 25297 25288 0 11:19 ? 00:00:00 postgres: `walreceiver streaming 0/1F000438`
六、验证数据同步
1、主库检查同步状态sync_state 为sync
[postgres@keep-postgres12-node1 data]$ psql
postgres=# \x
Expanded display is on.
postgres=# SELECT * FROM pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 29845
usesysid | 16642
usename | replrole
application_name | standby_pg2
client_addr | 192.168.122.89
client_hostname | keep-postgres12-node2
client_port | 54086
backend_start | 2024-12-02 11:19:12.468565+08
backend_xmin |
state | streaming
sent_lsn | 0/1F000438
write_lsn | 0/1F000438
flush_lsn | 0/1F000438
replay_lsn | 0/1F000438
write_lag |
flush_lag |
replay_lag |
sync_priority | 1
sync_state | sync
reply_time | 2024-12-02 11:23:49.82386+08
2、测试数据同步
-- 主库执行
drop table if exists employees_sync CASCADE;
CREATE TABLE employees (id SERIAL PRIMARY KEY,name VARCHAR(100),position VARCHAR(100),department VARCHAR(100),hire_date DATE
);INSERT INTO employees_sync (name, position, department, hire_date)
SELECT 'Employee ' || (generate_series(1, 200))::text,'Position ' || (generate_series(1, 200))::text,'Department ' || (random() *(200-1)+1)::text,'2010-01-01'::date + (generate_series(1, 200) * interval '1 day');-- 备库查询数据
select count(1) from employees_sync ;
-- 计算两边的md5值
SELECT md5(string_agg(id::text || '-' ||name || '-' ||position || '-' ||department || '-' ||TO_CHAR(hire_date, 'YYYY-MM-DD'),',')
) AS row_md5
FROM employees_sync;
七、主备切换
1、停主库
[postgres@keep-postgres12-node1 ~]$ pg_ctl stop -m fast
2、主从切换
备库升主
# 切换之后,$PGDATA下原有的 standby.signal 文件不存在了
[postgres@keep-postgres12-node2 ~]$ pg_ctl promote
查看状态
[postgres@keep-postgres12-node2 ~]$ pg_controldata | grep -i cluster
Database cluster state: `in production`
3、检查原备库的配置(现主)
检查配置文件postgresql.auto.conf
[postgres@keep-postgres12-node2 data]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
# 检查是否进行注释,配置还是当作备库,自相矛盾
# primary_conninfo = 'user=replrole passfile=''/home/postgres/.pgpass'' host=''keep-postgres12-node1'' application_name=standby_pg2 port=5432 sslmode=prefer sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
检查状态
# 此处进程应为 walwriter,表示还是备机状态。若修改了postgresql.auto.conf,需重启数据库
[postgres@keep-postgres12-node2 data]$ ps -ef | grep postgres
postgres 25288 1 0 Dec02 ? 00:00:00 /data/postgres12.2/bin/postgres
postgres 25289 25288 0 Dec02 ? 00:00:00 postgres: logger
postgres 25294 25288 0 Dec02 ? 00:00:02 postgres: checkpointer
postgres 25295 25288 0 Dec02 ? 00:00:02 postgres: background writer
postgres 25296 25288 0 Dec02 ? 00:00:00 postgres: stats collector
postgres 29058 25288 0 16:11 ? 00:00:00 postgres: `walwriter`
postgres 29059 25288 0 16:11 ? 00:00:00 postgres: autovacuum launcher
postgres 29060 25288 0 16:11 ? 00:00:00 postgres: archiver last was 000000030000000000000020.partial
postgres 29061 25288 0 16:11 ? 00:00:00 postgres: logical replication launcher[postgres@keep-postgres12-node2 data]$ pg_ctl restart# 重启数据库后,备机状态为未连接,不会启动walsender
[postgres@keep-postgres12-node2 data]$ ps -ef | grep postgres
postgres 32261 1 0 16:20 ? 00:00:00 /data/postgres12.2/bin/postgres
postgres 32262 32261 0 16:20 ? 00:00:00 postgres: logger
postgres 32264 32261 0 16:20 ? 00:00:00 postgres: checkpointer
postgres 32265 32261 0 16:20 ? 00:00:00 postgres: background writer
postgres 32266 32261 0 16:20 ? 00:00:00 postgres: walwriter
postgres 32267 32261 0 16:20 ? 00:00:00 postgres: autovacuum launcher
postgres 32268 32261 0 16:20 ? 00:00:00 postgres: archiver
postgres 32269 32261 0 16:20 ? 00:00:00 postgres: stats collector
postgres 32270 32261 0 16:20 ? 00:00:00 postgres: logical replication launcher
4、备库配置standby.signal文件
# standby.signal文件需要新建
[postgres@keep-postgres12-node1 data]$ cat standby.signal
# 添加以下内容
primary_conninfo = 'host=keep-postgres12-node2 port=5432 user=replrole password=ReplRole@123 application_name=standby_pg2 options=''-c wal_sender_timeout=5000'''
restore_command = 'cp /data/postgres12.2/arch/%f %p'
archive_cleanup_command = 'pg_archivecleanup /data/postgres12.2/arch %r'
standby_mode = on
5、备库修改配置文件postgresql.auto.conf
[postgres@keep-postgres12-node1 data]$ cat postgresql.auto.conf
# 添加以下内容
primary_conninfo = 'user=replrole passfile=''/home/postgres/.pgpass'' host=''keep-postgres12-node2'' application_name=standby_pg2 port=5432 sslmode=prefer sslcompression=0 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
6、启动备库
[postgres@keep-postgres12-node1 data]$ pg_ctl start
7、检查数据库进程
# 备库
[postgres@keep-postgres12-node1 ~]$ ps -ef | grep postgres
postgres 32638 1 0 16:38 ? 00:00:00 /data/postgres12.2/bin/postgres
postgres 32639 32638 0 16:38 ? 00:00:00 postgres: logger
postgres 32640 32638 0 16:38 ? 00:00:00 postgres: startup recovering 000000040000000000000021
postgres 32644 32638 0 16:38 ? 00:00:00 postgres: checkpointer
postgres 32645 32638 0 16:38 ? 00:00:00 postgres: background writer
postgres 32646 32638 0 16:38 ? 00:00:00 postgres: stats collector
postgres 32647 32638 0 16:38 ? 00:00:00 postgres: `walreceiver` streaming 0/21002210# 主库
[postgres@keep-postgres12-node2 ~]$ ps -ef | grep postgres
postgres 4764 32261 0 16:38 ? 00:00:00 postgres: `walsender` replrole 192.168.122.87(18913) streaming 0/21002210
postgres 32261 1 0 16:20 ? 00:00:00 /data/postgres12.2/bin/postgres
postgres 32262 32261 0 16:20 ? 00:00:00 postgres: logger
postgres 32264 32261 0 16:20 ? 00:00:00 postgres: checkpointer
postgres 32265 32261 0 16:20 ? 00:00:00 postgres: background writer
postgres 32266 32261 0 16:20 ? 00:00:00 postgres: walwriter
postgres 32267 32261 0 16:20 ? 00:00:00 postgres: autovacuum launcher
postgres 32268 32261 0 16:20 ? 00:00:00 postgres: archiver
postgres 32269 32261 0 16:20 ? 00:00:00 postgres: stats collector
postgres 32270 32261 0 16:20 ? 00:00:00 postgres: logical replication launcher
8、验证是否同步
-- 主库执行
drop table if exists employees1_sync CASCADE;
CREATE TABLE employees1_sync (id SERIAL PRIMARY KEY,name VARCHAR(100),position VARCHAR(100),department VARCHAR(100),hire_date DATE
);INSERT INTO employees1_sync (name, position, department, hire_date)
SELECT 'Employee ' || (generate_series(1, 200))::text,'Position ' || (generate_series(1, 200))::text,'Department ' || (random() *(200-1)+1)::text,'2010-01-01'::date + (generate_series(1, 200) * interval '1 day');-- 备库查询数据
select count(1) from employees1_sync ;
-- 计算两边的md5值
SELECT md5(string_agg(id::text || '-' ||name || '-' ||position || '-' ||department || '-' ||TO_CHAR(hire_date, 'YYYY-MM-DD'),',')
) AS row_md5
FROM employees1_sync;
八、pg_rewind 工具
当主备集群中的备库意外崩溃,且经过长时间,归档日志又被删除了,需要把这段时间的增量数据同步回来,那么就可以用 到pg_rewind 工具进行同步。pg_rewind 使一个PostgreSQL 数据目录与另一个数据目录一致。
[postgres@keep-postgres12-node1 ~]$ pg_rewind --target-pgdata $PGDATA --source-server='host=keep-postgres12-node2 port=5432 user=keep password=keep dbname=postgres' -P
pg_rewind帮助命令
[postgres@keep-postgres12-node1 ~]$ pg_rewind --help
pg_rewind resynchronizes a PostgreSQL cluster with another copy of the cluster.Usage:pg_rewind [OPTION]...Options:-D, --target-pgdata=DIRECTORY existing data directory to modify--source-pgdata=DIRECTORY source data directory to synchronize with--source-server=CONNSTR source server to synchronize with-n, --dry-run stop before modifying anything-N, --no-sync do not wait for changes to be writtensafely to disk-P, --progress write progress messages--debug write a lot of debug messages-V, --version output version information, then exit-?, --help show this help, then exit相关文章:
PostgreSQL的一主一从集群搭建部署 (同步)
一、实验环境 虚拟机名IP身份简称keep-postgres12-node1192.168.122.87主节点node1keep-postgres12-node2192.168.122.89备节点node2 二、安装数据库 源码包方式(主) 1、创建用户 [rootkeep-postgres12-node1 ~]# groupadd postgres [rootkeep-post…...
ios逆向某新闻 md5+aes
本期的案例比较简单,也许是ios逆向算法本来就比较简单的原因,所以前面我就多扯一些爬虫和逆向的东西。之前写的文章都是js逆向和android逆向的案例,这也是首篇ios的案例,所以会从入门开始讲起。 3大逆向对比 首先爬虫工程师大部…...
grpc的负载均衡
grpc的负载均衡分为client-side load balance和server-side load balance。 所谓的“客户端负载均衡”是指主调方调用被调方的时候,在grpc.DialContext里需要指定grpc.WithDefaultServiceConfig,这个DefaultServiceConfig默认是用pick-first策略。也支持…...
提升搜索体验!—— 推出 Elastic Rerank 模型(技术预览版)
作者:来自 Elastic Shubha Anjur Tupil 几分钟内即可开始使用 Elastic Rerank 模型:强大的语义搜索功能,无需重新索引,提供灵活性和成本控制;高相关性、顶级性能和文本搜索效率。 使用我们全新的先进跨编码器 Elastic …...
【51单片机】程序实验1112.外部中断-定时器中断
主要参考学习资料:B站【普中官方】51单片机手把手教学视频 前置知识:C语言 单片机套装:普中STC51单片机开发板A4标准版套餐7 码字不易,求点赞收藏加关注(•ω•̥) 有问题欢迎评论区讨论~ 目录 程序实验11&12.外部中断-定时器…...
webrtc-java:引领Java进入实时通信新时代
webrtc-java:引领Java进入实时通信新时代 项目地址:https://gitcode.com/gh_mirrors/we/webrtc-java 在现代互联网应用中,实时通信(Real-Time Communication, RTC)已成为连接人们的桥梁。而说起RTC技术的先锋,不得不…...
TongWeb7-东方通快速使用手册
TongWeb7-东方通 快速使用手册 文章目录 第1章 TongWeb7 产品介绍 1.1 概述1.2 规范支持 第2章 TongWeb7 安装 2.1 TongWeb7 安装要求 2.1.1 TongWeb7 支持的操作系统2.1.2 系统要求2.1.3 其他 2.2 安装TongWeb72.3TongWeb7 目录结构说明2.4 TongWeb7 的启动和停止 第3章 应用…...
JVM内存区块
大家好,经过前两篇文章的介绍,大家对数组也有了一定了解,其实所有的数组都是对象,我们在方法中引用数组的变量叫做引用变量(简称引用),那么数组到底是存放在哪里的呢,为什么引用再出…...
C语言单元总结
黑色加粗表示刷题刷到这样的题 红色加粗表示可能重要 单元一 程序设计宏观认识 C语言程序框架 C语言程序最基本的程序框架由两部分构成,分别是 1) 编译预处理 2) 函数组 C语言程序构成 C程序最大的特点就是所有的程序都是用函数来装配的,函数是构成…...
通过PS和Unity制作2D动画之一:创建形象
1、通过路径画出轮廓 使用路径的过程中,需要注意: 1)如果使用形状工具作图,比如使用椭圆工具画正圆形,需要设置其属性为“路径”。 2)使用路径选择工具,再按住Alt键点击某个路径,可…...
Notable是一款优秀开源免费的Markdown编辑器
一、Notable简介 Notable是一款开源的跨平台Markdown编辑器,支持Linux、MacOS、Windows以及国产操作系统等多种主流操作系统。它以其高颜值和强大的功能,成为了许多用户的首选工具。 主要特性 实时预览: Notable提供了实时预览功能&…...
基于MFC绘制门电路
MFC绘制门电路 1. 设计内容、方法与难点 本课题设计的内容包括了基本门电路中与门和非门的绘制、选中以及它们之间的连接。具体采用的方法是在OnDraw函数里面进行绘制,并设计元器件基类,派生出与门和非门,并组合了一个引脚类,在…...
C—指针初阶(2)
如果看完阁下满意的话,能否一键三连呢,我的动力就是大家的支持与肯定,冲! 二级指针 我们先看概念以及作用:用来存放一级指针的地址的指针 先看例子,我们逐一分析 我们先分析上面那个“1” 标注那里&#x…...
Linux 基础环境的开发工具以及使用(下)
1. make / Makefile 自动化构建的工具 1)引入 在我们进行一些大型的工程的时候,代码量是极其大,当我们代码在进行一系列的编译的时候,难免会出现一些错误,当我们对错误进行一系列的更改之后,难道我们需要…...
constexpr、const和 #define 的比较
constexpr、const 和 #define 的比较 一、定义常量 constexpr 定义:constexpr用于定义在编译期可求值的常量表达式。示例:constexpr int x 5;这里,x的值在编译期就确定为5。 const 定义:const表示变量在运行期间不能被修改&…...
期末复习-Hadoop综合复习
说明 以下内容仅供参考,提到不代表考到,请结合实际情况自己复习 目录 说明 一、题型及分值 二、综合案例题-部署Hadoop集群 或 部署Hadoop HA集群 案例 1:Hadoop 基础集群部署 案例 2:Hadoop HA 集群部署 案例 3ÿ…...
禁用SAP Hana错误密码锁定用户功能
背景 公司项目适配多种数据库其中包含SAP Hana,由于有同事的数据库连接工具保存了某个在用的数据库的旧密码,导致时不时会被锁用户。通过查询官方文档已解决,这里统一记录一下。 禁用密码锁定方法 以下按系统管理员和普通用户的解法分别列…...
Ubuntu 22.04加Windows AD域
说明: Ubuntu 22.04系统通过realmd,sssd加入到 Active Directory 域,并为域用户配置sudo权限。同时为方便用户使用为Ubuntu系统安装wps与sogou中文输入法。 1. Ubuntu 22.04加入Windows AD域 1.1 首先配置网络,Ubuntu系统能…...
qt实现窗口的动态切换
先说一下整体思路。页面布局两个widget然后再将定时器和按钮关联起来。 定时器发出信号的时候,随着信号,不断地重新设置widget的宽度,实现窗口的动态切换。 具体操作如下: class QtWidgetsApplication4 : public QMainWindow {…...
第十七届山东省职业院校技能大赛 中职组“网络安全”赛项资源任务书样题②
第十七届山东省职业院校技能大赛 中职组“网络安全”赛项资源任务书样题② 模块A 基础设施设置与安全加固(200分)A-1 登录安全加固(Windows, Linux)A-2 Nginx安全策略(Linux)A-3日志监控(Windows)A-4中间件…...
大话软工笔记—需求分析概述
需求分析,就是要对需求调研收集到的资料信息逐个地进行拆分、研究,从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要,后续设计的依据主要来自于需求分析的成果,包括: 项目的目的…...
从WWDC看苹果产品发展的规律
WWDC 是苹果公司一年一度面向全球开发者的盛会,其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具,对过去十年 WWDC 主题演讲内容进行了系统化分析,形成了这份…...
STM32+rt-thread判断是否联网
一、根据NETDEV_FLAG_INTERNET_UP位判断 static bool is_conncected(void) {struct netdev *dev RT_NULL;dev netdev_get_first_by_flags(NETDEV_FLAG_INTERNET_UP);if (dev RT_NULL){printf("wait netdev internet up...");return false;}else{printf("loc…...
AtCoder 第409场初级竞赛 A~E题解
A Conflict 【题目链接】 原题链接:A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串,只有在同时为 o 时输出 Yes 并结束程序,否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...
DAY 47
三、通道注意力 3.1 通道注意力的定义 # 新增:通道注意力模块(SE模块) class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...
YSYX学习记录(八)
C语言,练习0: 先创建一个文件夹,我用的是物理机: 安装build-essential 练习1: 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件,随机修改或删除一部分,之后…...
Web 架构之 CDN 加速原理与落地实践
文章目录 一、思维导图二、正文内容(一)CDN 基础概念1. 定义2. 组成部分 (二)CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 (三)CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 …...
初学 pytest 记录
安装 pip install pytest用例可以是函数也可以是类中的方法 def test_func():print()class TestAdd: # def __init__(self): 在 pytest 中不可以使用__init__方法 # self.cc 12345 pytest.mark.api def test_str(self):res add(1, 2)assert res 12def test_int(self):r…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...
USB Over IP专用硬件的5个特点
USB over IP技术通过将USB协议数据封装在标准TCP/IP网络数据包中,从根本上改变了USB连接。这允许客户端通过局域网或广域网远程访问和控制物理连接到服务器的USB设备(如专用硬件设备),从而消除了直接物理连接的需要。USB over IP的…...
