首页 文章 精选 留言 我的

精选列表

搜索[灾备],共4380篇文章
优秀的个人博客,低调大师

PostgreSQL-11.3-主从流复制+手动主切换

1 摘要 使用PostgreSQL 11.3 创建两个节点:node1 和 node2; 配置主从流复制,然后做手动切换(failover)。为了配置过程简单,两个节点在同一台物理机器上。 首先建立主从同步流复制,起初,node1作为主节点(Primary),node2作为从节点(Standby)。 接下来,模拟主节点(node1)无法工作,把从节点(node2)手动切换成主节点。然后把原来的主节点(node1)作为从节点加入系统。 此时,node2是主 (Primary), node1是从(Standby)。 最后,模拟主节点(node2)无法工作,把从节点(node1)手动切换成主节点。然后把node2作为从节点加入系统。 此时,node1是主 (Primary), node2是从(Standby)。 2 创建node1和node2,配置主从流复制 上级目录是: /Users/tom 两个子目录,testdb113,testdb113b, 分别属于两个节点, node1, node2: testdb113 --> node1 testdb113b --> node2 2.1 创建主节点(Primary) node1 mkdir testdb113 initdb -D ./testdb113 pg_ctl -D ./testdb113 start 2.1.1 node1: 创建数据库账号,用于流复制 创建账号 'replicauser',该账号存在于node1和node2 。 CREATE ROLE replicauser WITH REPLICATION LOGIN ENCRYPTED PASSWORD 'abcdtest'; 2.1.2 node1: 创建归档目录 mkdir ./testdb113/archive 2.1.3 node1: 在配置文件中(postgresql.conf)设定参数 #for the primary wal_level = replica synchronous_commit = remote_apply archive_mode = on archive_command = 'cp %p /Users/tom/testdb113/archive/%f' max_wal_senders = 10 wal_keep_segments = 10 synchronous_standby_names = 'pgslave001' #for the standby hot_standby = on 2.1.4 node1: 编辑文件pg_hba.conf,设置合法地址: add following to then end of pg_hba.conf host replication replicauser 127.0.0.1/32 md5 host replication replicauser ::1/128 md5 2.1.5 node1: 预先编辑文件:recovery.done 注意: 该文件后来才会用到。当node1成为从节点时候,需要把recovery.done重命名为recovery.conf standby_mode = 'on' recovery_target_timeline = 'latest' primary_conninfo = 'host=localhost port=6432 user=replicauser password=abcdtest application_name=pgslave001' restore_command = 'cp /Users/tom/testdb113b/archive/%f %p' trigger_file = '/tmp/postgresql.trigger.5432' 2.2 创建和配置从节点(node2) mkdir testdb113b pg_basebackup -D ./testdb113b chmod -R 700 ./testdb113b 2.2.1 node2: 编辑文件postgresql.conf,设定参数 这个文件来自node1(由于使用了pg_basebackup),只需要更改其中的个别参数。 #for the primary port = 6432 wal_level = replica synchronous_commit = remote_apply archive_mode = on archive_command = 'cp %p /Users/tom/testdb113b/archive/%f' max_wal_senders = 2 wal_keep_segments = 10 synchronous_standby_names = 'pgslave001' #for the standby hot_standby = on 2.2.2 node2: 建立归档目录 确保归档目录存在 mkdir ./testdb113b/archive 2.2.3 node2: 检查/编辑文件pg_hba.conf 这个文件来自node1,不需要编辑 2.2.4 node2: 创建/编辑文件recovery.conf PG启动时,如果文件recovery.conf存在,则工作在recovery模式,当作从节点。如果当前节点升级成主节点,文件recovery.conf将会被重命名为recovery.done。 standby_mode = 'on' recovery_target_timeline = 'latest' primary_conninfo = 'host=localhost port=5432 user=replicauser password=abcdtest application_name=pgslave001' restore_command = 'cp /Users/tom/testdb113/archive/%f %p' trigger_file = '/tmp/postgresql.trigger.6432' 2.3 简单测试 2.3.1 重新启动主节点(node1): pg_ctl -D ./testdb113 restart 2.3.2 启动从节点(node2) pg_ctl -D ./testdb113b start 2.3.3 在node1上做数据更改 node1支持数据的写和读 psql -d postgres postgres=# create table test ( id int, name varchar(100)); CREATE TABLE postgres=# insert into test values(1,'1'); INSERT 0 1 2.3.4 节点node2读数据 node2是从节点(Standby),只能读,不能写。 psql -d postgres -p 6432 postgres=# select * from test; id | name ----+------ 1 | 1 (1 row) postgres=# insert into test values(1,'1'); ERROR: cannot execute INSERT in a read-only transaction postgres=# 3 模拟主节点node1不能工作时,把从节点node2提升到主节点 3.1 停止主节node1 node1停止后,只有node2工作,仅仅支持数据读。 pg_ctl -D ./testdb113 stop 3.1.1 尝试连接到节点node1时失败 因为node1停止了。 Ruis-MacBook-Air:tom$ psql -d postgres psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? 3.1.2 尝试连接到node2,成功;插入数据到node2,失败 node2在工作,但是只读 Ruis-MacBook-Air:~ tom$ psql -d postgres -p 6432 psql (11.3) Type "help" for help. postgres=# insert into test values(1,'1'); ERROR: cannot execute INSERT in a read-only transaction postgres=# 3.2 node2: 把node2提升到主 提升后,node2成为了主(Primary),既能读,也能写。 touch /tmp/postgresql.trigger.6432 3.3 node2: 插入数据到node2,阻塞等待 node2是主节点(Primary),能够成功插入数据。但是由于设置了同步流复制,node2必须等待某个从节点把数据更改应用到从节点,然后返回相应的LSN到主节点。 postgres=# insert into test values(2,'2'); 3.4 node1: 创建文件recovery.conf 使用之前建立的文件recovery.done作为模版,快速创建recovery.conf。 mv recovery.done recovery.conf 3.5 node1: 启动node1,作为从节点(Standby) 启动后,系统中又有两个节点:node2是主,node1是从。 pg_ctl -D ./testdb113 start 3.6 插入数据到node2(见3.3)成功返回 由于节点node1作为从节点加入系统,应用主节点的数据更改,然后返回相应WAL的LSN到主节点,使主节点等到了回应,从而事务处理得以继续。 postgres=# insert into test values(2,'2'); INSERT 0 1 postgres=# 3.7 分别在node1和node2读取数据 由于两个节点都只是数据读,而且流复制正常工作,所有读取到相同的结果 postgres=# select * from test; id | name ----+------ 1 | 1 2 | 2 (2 rows) 4 模拟node2无法工作时,提升node1成为主(Primary) 4.1 停止node2 pg_ctl -D ./testdb113b stop 4.1.1 尝试访问node2,失败 因为node2已经停止了. Ruis-MacBook-Air:~ tom$ psql -d postgres -p 6432 psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? 4.1.2 node1: 连接到node1,成功;插入数据到node1,失败 node1只读 Ruis-MacBook-Air:~ tom$ psql -d postgres psql (11.3) Type "help" for help. postgres=# insert into test values(3,'3'); ERROR: cannot execute INSERT in a read-only transaction postgres=# 4.2 node1: 提升node1,成为主(Primary) touch /tmp/postgresql.trigger.5432 4.3 node1: 插入数据到node1,阻塞等待 node1成功插入数据,但是要等待从节点的 remote-apply. postgres=# insert into test values(3,'3'); 4.4 node2: 创建文件recovery.conf mv recovery.done recovery.conf 4.5 node2: 启动node2,作为从节点(Standby) pg_ctl -D ./testdb113b start 4.6 node1: 插入数据(见4.3)成功返回。 postgres=# insert into test values(3,'3'); INSERT 0 1 4.7 分别在node1和node2读取数据 由于两个节点都只是数据读,而且流复制正常工作,所有读取到相同的结果 postgres=# select * from test; id | name ----+------ 1 | 1 2 | 2 3 | 3 (3 rows) 5 自动化解决方案 当主节点不能工作时,需要把一个从节点提升成为主节点。 如果是手工方式提升,可以使用trigger文件,也可以使用命令'pg_ctl promote' 。 目前也有很多种自动化监控和切换的方案,在网上都能搜索到。或者,可以自己动手写一个自动化监控和切换的方案。

优秀的个人博客,低调大师

记录一次邮件容恢复过程

背景介绍 客户目前使用的是Exchange Server 2013,两前两后,数据盘是存储挂载过来的,邮件备份使用的是NBU,由于机房漏水,导致存储服务器宕机。导致绝大部分数据丢失。 Exchange恢复过程 使用新存储重新划分磁盘,并使用NBU逐个恢复数据库,按照原路径恢复。 检查已恢复的数据库所在的服务器(以数据库L14DB3为例) Get–MailboxDatabase L14DB2 | Get-MailboxDatabaseCopyStatus 3.检查数据库和log路径 Get-MailboxDatabase L14DB3 | fl Name,edb,Log 4.检查是否有日志缺失 进入到HQEXMB01服务器上,以管理员运行命令提示符,进入到数据库目录 输入Eseutil /mh L14db3.edb 检查数据状态及是否有日志缺失:如图:Log Required项显示有日志缺失 5.修复缺失Log文件 eseutil /r E0C /L G:\Mailboxlog\L14DB3 再次检查,状态如下: 6.检查多余的Log文件 7.删除此数据库的DAG副本 Remove-MailboxDatabaseCopy –Identity L14DB3\ HQEXMB01 –Confirm:$False 8.装入数据库 mount-database L14DB3 -Force 检查数据库是否已经装入 9.其他情况 如果日志未丢失,执行以下步骤恢复: eseutil /mh O:\Mailbox\Line04C006\line04c006.edb (PS:检查数据库日志缺失状态,发现未丢失) eseutil /p O:\Mailbox\Line04C006\line04c006.edb (PS:使用命令强制修复数据库) mount-database 数据库名称 -Force 强制修复的数据库用户可能无法正常收发邮件,报错如下: 解决办法:将此邮箱移动到其他数据库; Exchange DAG副本添加 说明:此文档以数据库public001-t为例: 1.卸载和装入数据库: 打开Exchange ECP服务器数据库选择要添加副本的数据库…卸载 弹出警告-点击“是” 检查数据库是否已经卸载 打开Exchange Powershell命令 挂载数据库:(指定参数-force参数) Mount-Database Public001-t –force 通过ECP检查数据库是否已经装入 2.添加数据库副本 Exchange Powershell中运行: Add-MailboxDatabaseCopy –Identity Public001-t –MailboxServer hqexmb01 (说明:–MailboxServer 后面跟副本服务器的名称;先检查此数据库在哪台服务器上装入,则另外一台服务器为添加副本的服务器) 添加完成 重启Exchange信息存储服务 3.检查DAG状态

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册