提升副本 / 故障转移(promote)
Oracle
-- Data Guard broker (DGMGRL):
SWITCHOVER TO 'standby_db'; -- planned, no data loss
-- FAILOVER TO 'standby_db'; -- on loss of the primary, emergencySWITCHOVER 是计划内的角色切换(主备切换);FAILOVER(故障转移)在主库不可达时执行,存在数据丢失风险。
PostgreSQL
-- Promote the standby to primary:
SELECT pg_promote();
-- or from the shell: pg_ctl promote -D /var/lib/pgsql/data提升之前,务必确认旧主库确实已停止(脑裂风险)。提升之后,要将旧主库作为新主库的副本重新加入需要 pg_rewind;为此必须启用 wal_log_hints = on 或数据校验和。
MySQL
-- On the replica (manual):
-- 1) Stop receiving new events, wait until the received relay log is fully applied:
STOP REPLICA IO_THREAD;
SELECT RECEIVED_TRANSACTION_SET FROM performance_schema.replication_connection_status;
SELECT @@GLOBAL.gtid_executed; -- the received set must be fully contained here
-- 2) Remove replication and enable writes:
STOP REPLICA;
RESET REPLICA ALL;
SET GLOBAL read_only = OFF; -- also turns off super_read_only
-- 3) Fence the old source against writes, then redirect the application.如果在 relay log 完全应用之前执行 RESET REPLICA ALL,已接收但未应用的事务将丢失。副本通常启用 read_only/super_read_only 运行;关闭之前不接受写入。如果旧源库仍可写,存在脑裂风险。该语法适用于 8.0.22+(之前为 STOP SLAVE / RESET SLAVE ALL;旧形式已在 8.4 中移除)。生产环境中要实现自动且安全的故障转移,优先选择 InnoDB Cluster/Group Replication、Orchestrator 或 MHA。
SQL Server
-- On the target secondary replica (synchronous):
ALTER AVAILABILITY GROUP [AG_Name] FAILOVER;
-- Force it, with risk of data loss:
-- ALTER AVAILABILITY GROUP [AG_Name] FORCE_FAILOVER_ALLOW_DATA_LOSS;向同步副本进行计划内故障转移不会丢失数据;FORCE 仅用于灾难场景。
MongoDB
// Trigger an election on the current primary:
rs.stepDown(60) // step down from primary for 60 sstepDown 会发起新的选举;若要偏向特定节点,调整成员的 priority 值。
Redis
# Make the replica an independent primary:
REPLICAOF NO ONE
# With Sentinel (recommended): SENTINEL FAILOVER <master-name>如果使用 Sentinel/Cluster,请使用 SENTINEL FAILOVER 而不是手动 REPLICAOF;否则存在脑裂风险。