postgresql-11主从复制(流复制)部署
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了postgresql-11主从复制(流复制)部署,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5595字,纯文字阅读大概需要8分钟。
内容图文
-
主从介绍
- PostgreSQL流复制默认是异步的。如果主服务器崩溃,则某些已被提交的事务可能还没有被复制到后备服务器,这会导致数据丢失。数据的丢失量与故障转移时的复制延迟成比例。
- 同步复制能够保证一个事务的所有修改都能被传送到一台或者多台同步后备服务器。这扩大了由一次事务提交所提供的标准持久化级别。在计算机科学理论中这种保护级别被称为 2-safe 复制。而当
synchronous_commit
被设置为remote_write
时,则是 group-1-safe (group-safe 和 1-safe)。 - 在请求同步复制时,一个写事务的每次提交将一直等待,直到收到一个确认表明该提交在主服务器和后备服务器上都已经被写入到磁盘上的预写式日志中。数据会被丢失的唯一可能性是主服务器和后备服务器在同一时间都崩溃。这可以提供更高级别的持久性,尽管只有系统管理员要关系两台服务器的放置和管理。等待确认提高了用户对于修改不会丢失的信心,但是同时也不必要地增加了对请求事务的响应时间。最小等待时间是在主服务器和后备服务器之间的来回时间。
- 只读事务和事务回滚不需要等待后备服务器的回复。子事务提交也不需要等待后备服务器的响应,只有顶层提交才需要等待。长时间运行的动作(如数据载入或索引构建)不会等待最后的提交消息。所有两阶段提交动作要求提交等待,包括预备和提交。
- 同步后备可以是物理复制后备或者是逻辑复制订阅者。它还可以是任何其他物理或者逻辑WAL复制流的消费者,它懂得如何发送恰当的反馈消息。除内建的物理和逻辑复制系统之外,还包括
pg_receivewal
和pg_recvlogical
之类的特殊程序,以及一些第三方复制系统和定制程序。同步复制支持的细节请查看相应的文档。
-
主从机器分配
-
IP地址 DB版本 主从关系 192.168.63.134 11.6 主 192.168.63.141 11.6 从
-
-
安装postgresql
-
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y yum install postgresql11 -y yum install postgresql11-server -y
-
-
主库配置
-
主库初始化
-
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl start postgresql-11
-
- 创建复制用户,进行主从同步使用
-
[root@localhost data]# sudo -i -u postgres -bash-4.2$ psql psql (11.6) 输入 "help" 来获取帮助信息. postgres=# create role repl login replication encrypted password '123456'; CREATE ROLE
-
- 主库上配置从库采用repl账号
-
vim /var/lib/pgsql/11/data/pg_hba.conf #只需要台添加下面两行,repl是用来做备份的用户,后面的192.168.63.0/24是该网段内的IP地址 host replication repl 192.168.63.0/24 md5 host all repl 192.168.63.0/24 trust vim /var/lib/pgsql/11/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart) max_connections = 512 # (change requires restart) shared_buffers = 128MB # min 128kB dynamic_shared_memory_type = posix # the default is the first option wal_level = hot_standby # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = 'cp %p /var/lib/pgsql/11/data/pg_archive/%f' # command to use to archive a logfile segment max_wal_senders = 6 # max number of walsender processes wal_keep_segments = 10240 # in logfile segments, 16MB each; 0 disables wal_sender_timeout = 60s # in milliseconds; 0 disables log_directory = 'log' # directory where log files are written
- 修改完,要创建刚刚配置的一些目录结构:
-
mkdir /var/lib/pgsql/11/data/pg_archive/
chown -R postgres.postgres /var/lib/pgsql/11/data - 重启主库服务
-
systemctl restart postgresql-11
-
-
-
从库配置
- 从库安装完成后,不初始化,若已经初始化,删除其data目录
-
#把主节点所有的数据文件都会拷贝过来 [root@localhost ~]# pg_basebackup -h 192.168.63.134 -U repl -D /var/lib/pgsql/11/data/ -X stream -P 口令: 25312/25312 kB (100%), 1/1 表空间 [root@localhost ~]# ls /var/lib/pgsql/12/data/ backup_label current_logfiles log pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf base global pg_archive pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf
- 从库配置文件,根据下面的配置进行修改。
[root@localhost ~]# vim /var/lib/pgsql/11/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on; port = 5432 # (change requires restart) max_connections = 1000 # (change requires restart) shared_buffers = 128MB # min 128kB dynamic_shared_memory_type = posix # the default is the first option wal_level = replica # minimal, replica, or logical archive_mode = on # enables archiving; off, on, or always archive_command = 'cp %p /var/lib/pgsql/12/data/pg_archive/%f' # command to use to archive a logfile segment wal_sender_timeout = 60s # in milliseconds; 0 disables hot_standby = on # "on" allows queries during recovery max_standby_streaming_delay = 30s # max delay before canceling queries wal_receiver_status_interval = 10s # send replies at least this often hot_standby_feedback = on # send info from standby to prevent log_directory = 'log' # directory where log files are written,
- 创建恢复文件recovery.conf。
-
[root@localhost 11]# cp /usr/pgsql-11/share/recovery.conf.sample /var/lib/pgsql/11/data/recovery.conf [root@localhost 11]# vim /var/lib/pgsql/11/data/recovery.conf # 调整参数: recovery_target_timeline = 'latest' #同步到最新数据 standby_mode = on #指明从库身份
trigger_file = 'failover.now' primary_conninfo = 'host=192.168.63.134 port=5432 user=repl password=123456' #连接到主库信息 - 启动从库
systemctl start postgresql-11
-
- 验证主从配置
- 在主库上运行以下命令
postgres=# select client_addr,sync_state from pg_stat_replication; client_addr | sync_state ----------------+------------ 192.168.63.141 | async (1 行记录)
- 在主库上运行以下命令
- 可以创建一个数据库自行验证
内容总结
以上是互联网集市为您收集整理的postgresql-11主从复制(流复制)部署全部内容,希望文章能够帮你解决postgresql-11主从复制(流复制)部署所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。