一、背景
数据库偶尔会出现主库节点宕机或是网卡故障等事件发生,从而会触发CM管控软件自动 切换主备操作,来确保业务能正常访问提供有效服务。但有时候主备库之间延时太大问题会影响原主库无法正常回切成备库,因此导致Polardb数据库集群关系脱离成了单机状态,这个时候就需要进行备库修复,重新建立集群同步复制关系。
二、环境信息
2.1 主机信息
Polardb-O 数据库 |
实际地址IP |
数据库架构 |
备注 |
Polardb-O 集群 |
192.168.58.130 |
旧主库 |
原主库宕机节点后,用于模拟新备库 |
192.168.58.131 |
旧备库 |
新主库 | |
|
192.168.58.129 |
VIP |
/ |
集群管理软件 |
192.168.58.132 |
/ |
/ |
2.2 架构图
三、备库恢复过程
将旧主库节点修复好后,利旧用于新备库。为了快速与主库建立集群关系,新备库不做太多变量操作只要将PFS文件系统中的数据清理掉,并保留polardb安装软件,就可以了。使用polar_basebackup物理备份工具远程从主库拉文件到备库,最后在CM上面将备库加入节点建立集群复制关系。下面详细介绍一下备库的恢复过程。
3.1、新备库恢复过程
1、 清除新备库文件系统
# 删除本地配置目录 sudo rm -rf $PGDATA # 清除 PFS 文件系统数据 sudo pfs -C disk rm -r /sdb/pgdata #重 建数据目录 pfs -C disk mkdir /sdb/polar_data |
2、 从 主库同步数据到新备库
在新备库上面使用polardb自带物理备份工具远程同步主库的文件到新备库。 (在新备库使用polardb 登录)
[polardb@polardb01 local] $polar_basebackup -h 192.168.58.131 -p5432 -U replicator -D $PGDATA --polardata=$PFSDIR --polar_storage_cluster_name=disk --polar_disk_name=$PFSDISK --polar_host_id=2 -X stream --progress --write-recovery-conf -v
[polardb@polardb01 local] $ ls -lrt $PGDATA -rw------- 1 polardb polardb 216 Oct 11 10:16 backup_label.old drwx------ 8 polardb polardb 74 Oct 11 10:16 base drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_snapshots drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_serial drwx------ 4 polardb polardb 34 Oct 11 10:16 pg_multixact -rw------- 1 polardb polardb 1636 Oct 11 10:16 pg_ident.conf -rw------- 1 polardb polardb 4513 Oct 11 10:16 pg_hba.conf drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_dynshmem -rw------- 1 polardb polardb 88 Oct 11 10:16 postgresql.auto.conf drwx------ 2 polardb polardb 6 Oct 11 10:16 polar_rel_size_cache drwx------ 2 polardb polardb 27 Oct 11 10:16 polar_fullpage -rw------- 1 polardb polardb 3 Oct 11 10:16 PG_VERSION drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_tblspc drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_stat -rw------- 1 polardb polardb 7478 Oct 11 10:16 postgresql.conf drwx------ 2 polardb polardb 6 Oct 11 10:16 dbms_pipe -rw------- 1 polardb polardb 168 Oct 11 10:16 polar_node_static.conf drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_commit_ts drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_csnlog drwx------ 2 polardb polardb 17 Oct 11 10:16 pg_notify -rw------- 1 polardb polardb 49 Oct 11 10:16 postmaster.opts drwx------ 2 polardb polardb 142 Oct 11 10:16 polardb_log -rw------- 1 polardb polardb 178 Oct 11 10:16 current_logfiles drwx------ 2 polardb polardb 17 Oct 11 10:16 pg_xact drwx------ 2 polardb polardb 6 Oct 11 10:16 polar_cache_trash -rw------- 1 polardb polardb 82 Oct 11 10:16 postmaster.pid drwx------ 2 polardb polardb 4096 Oct 11 10:16 global drwx------ 2 polardb polardb 17 Oct 11 10:16 pg_subtrans drwx------ 2 polardb polardb 6 Oct 11 10:16 pg_replslot drwx------ 4 polardb polardb 65 Oct 11 10:16 pg_logical drwx------ 2 polardb polardb 135 Oct 11 10:16 pg_stat_tmp [polardb@polardb01 local] $ pfs -C disk ls /sdb/polar_data Dir 1 256 Sun Oct 11 10:16:13 2020 pg_wal Dir 1 768 Sun Oct 11 10:16:32 2020 base Dir 1 9344 Sun Oct 11 10:16:36 2020 global Dir 1 0 Sun Oct 11 10:16:36 2020 pg_tblspc Dir 1 0 Sun Oct 11 10:16:36 2020 pg_logindex Dir 1 0 Sun Oct 11 10:16:36 2020 pg_twophase Dir 1 128 Sun Oct 11 10:16:36 2020 pg_xact Dir 1 0 Sun Oct 11 10:16:36 2020 pg_commit_ts Dir 1 256 Sun Oct 11 10:16:36 2020 pg_multixact Dir 1 0 Sun Oct 11 10:16:36 2020 pg_csnlog Dir 1 0 Sun Oct 11 10:16:36 2020 pg_replslot File 1 226 Sun Oct 11 10:16:36 2020 polar_non_exclusive_backup_labe
可以看到备库上面的文件已经从主库同步过来,同时PFS文件系统的数据也正常同步过来。
|
注意:主备库的host_id 不能一致,否则会影响备库数据同步。
3、 启动和检查备库节点
启动与检查备节点可验证数据库是否连接成功。具体操作如下:
[polardb@polardb01 local] $pg_ctl -c start -D $PGDATA waiting for server to start.... 38650 2020-10-11 02:26:59 UTC 00000 LOG: polar_vfs loaded in postmaster 38650
38650 2020-10-11 02:26:59 UTC 00000 LOG: listening on IPv4 address "0.0.0.0", port 5432
38650 2020-10-11 02:26:59 UTC 00000 LOG: listening on IPv6 address "::", port 5432 ......................... >>>>>>>>>>>>>>>>>>>>>>>中间省略部分日志信息<<<<<<<<<<<<<<<<<<<<<<<<< ......................... 38650 2020-10-11 02:27:02 UTC 00000 LOG: removing file "pg_notify/0000" 38650 2020-10-11 02:27:02 UTC 00000 LOG: vfs_unlink pg_notify/0000 38650 2020-10-11 02:27:02 UTC 00000 LOG: forked new process, pid is 38665, true pid is 38665 38650 2020-10-11 02:27:02 UTC 00000 LOG: redirecting log output to logging collector process 38650 2020-10-11 02:27:02 UTC 00000 HINT: Future log output will appear in directory "polardb_log". ... done server started [polardb@polardb01 local] $psql -h$PGDATA -p$PGPORT
polardb=# select sysdate from dual; sysdate -------------------- 11-OCT-20 02:27:57 (1 row) |
注意:如果Polardb数据库能连接上,则证明数据库安装好,并且启动成功。
3.2、 CM 管理软件
为了保障主从能正常切换,需要在CM 中将异常备库清除掉,以免后面加入新备库时出现冲突。
[polardb@polardb04 ~]$curl -H "Content-Type:application/json" -X POST --data "{\"user\":\"polardb\",\"dataPath\":\"/app/polardb_ data\",\"ip\":\" 192.168.58.130\",\"port\":\"5432\",\"type\":\"Standby\",\"storage_type\":\"local_ssd\",\"sync\":\"SYNC\"}" http://127.0.0.1:5000/v1/remove_ins
[polardb@polardb04 ~]$ curl -H "Content-Type:application/json" http://127.0.0.1:5000/v1/status?type=visual { "phase": "RunningPhase", "master": { "endpoint": " 192.168.58.131:5432", "data_path": "/app/polardb_data", "user": "polardb", "phase": "RUNNING", "start_at": "2020-10-11 10:15:16", "sync_status": "SYNC" }, "vip": [ { "vip": "192.168.58.129", "interface": "team0", "mask": "255.255.255.0", "endpoint": "192.168.58.131:5432" } |
将新备库以备库方式 加入polardb集群。
curl -H " Content-Type:application/json" -X POST --data "{\"user\":\"polardb\",\"dataPath\":\"/app/polardb_data\",\"ip\":\" 192.168.58.130 \",\"port\":\"5432\",\"type\":\"Standby\",\"storage_type\":\"local_ssd\",\"sync\":\"SYNC\"}" http://127.0.0.1:5000/v1/add_ins |
3.3、 主备库流复制状态
1 、集群环境检查
[polardb@polardb04 ~]$curl -H ""Content-Type:application/json"" http://127.0.0.1:5000/v1/status?type=visual { "phase": "RunningPhase", "master": { "endpoint": " 192.168.58.131:5432", "data_path": "/app/polardb_data", "user": "polardb", "phase": "RUNNING", "start_at": "2020-10-11 10:15:16", "sync_status": "SYNC" }, "standby": [ { "endpoint": " 192.168.58.130:5432", "data_path": "/app/polardb_data", "user": "polardb", "phase": "RUNNING", "start_at": "2020-10-11 10:27:17", "sync_status": "SYNC" } ], "vip": [ { "vip": " 192.168.58.129", "interface": "team0", "mask": "255.255.255.0", "endpoint": " 192.168.58.131:5432" } 当前 polardb 主备库服务正常, VIP 地址在主节点 192.168.58.131 ,主库同步复制状态正常。 |
2 、检查主库流复制进程
ps -ef |grep wal # 主库 – walwriter ps -ef |grep wal # 备库 – walreceiver |
3 、检查主备库读写模式
[polardb@polardb02 app]$ psql -h$PGDATA -p$PGPORT -c "show transaction_read_only"; transaction_read_only ----------------------- off
[polardb@polardb01 local]$ psql -h$PGDATA -p$PGPORT -c "show transaction_read_only"; transaction_read_only ----------------------- on 注意:主库状态: off, 备库: on |
4
、主库查看复制状态
polardb=# select * from pg_stat_replication ; -[ RECORD 1 ]----+--------------------------------- pid | 41023 usesysid | 356380346 usename | repuser application_name | standby_184492244_5432 client_addr | 192.168.58.130 client_hostname | client_port | 60751 backend_start | 11-OCT-20 10:52:43.643961 +08:00 backend_xmin | state | streaming sent_lsn | 7E/80001518 write_lsn | 7E/80001518 flush_lsn | 7E/80001518 replay_lsn | 7E/80001518 write_lag | 00:00:00.000503 flush_lag | 00:00:00.000515 replay_lag | 00:00:00.000724 sync_priority | 2 sync_state | sync
以上主从同步复制状态正常。 |
5、
测试数据主备同步是否正常
在主库创建一个表,看备库是否存在, 如果存在说明数据同步正常。
# 主库 ztdb=# create table zttab_sync_test01(id int ,name char(20)); CREATE TABLE ztdb=# insert into zttab_sync_test01 values(1,'alan'),(2,'jinxu'); INSERT 0 2 ztdb=# select * from zttab_sync_test01; id | name ----+---------------------- 1 | alan 2 | jinxu
# 备库 ztdb=# ztdb=# select * from zttab_sync_test01; id | name ----+---------------------- 1 | alan 2 | jinxu
测试主库创建一张表并插入几条数据,备库也正常同步过来。 |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28833846/viewspace-2733515/,如需转载,请注明出处,否则将追究法律责任。