ITPub博客

首页 > 数据库 > 国内数据库 > GBase 8a安全重建表的一个方案

GBase 8a安全重建表的一个方案

原创 国内数据库 作者:静静1068 时间:2021-11-02 18:19:11 0 删除 编辑

当由于某些原因需要重建表时,需要锁定表,避免该表在重建期间又被更改,导致新表和老表数据不一致。本文提供一个应用层的安全重建方案。

方案内容

该方案通过如下几个步骤来实现

  • 锁住表
  • 建一个新表,结构和原有的一样
  • 将数据从原有表迁移到新表
  • 将原有表改名
  • 将新表改名成原有表
  • 释放锁
  • 将改名的老表删除【可选】


实现例子

如下的一个shell脚本,传入2个参数,库名和表名,连接数据库的用户名和密码,需要根据实际情况做修改。其中的sign是方便查找哪些表做了处理的一个临时表名标志。

如下例子没有包含删除老表的部分,建议进行人工确认后,再删除老表,避免操作失误导致数据丢失。

该脚本开始清理了残留表,为了安全,默认是注释掉的。

[gbase@rh6-1 ~]$ cat gbase_rebuild_table.sh
dbname=$1
tablename=$2
sign='ABCDEFG'
echo --${tablename}--
gccli -ugbase -pXXXXX -vvv -D${dbname} <<EOF
-- drop table if exists ${tablename}_${sign}_OLD;
-- drop table if exists ${tablename}_${sign}_NEW;
lock table  ${tablename} write;
create table ${tablename}_${sign}_NEW like ${tablename};
insert into ${tablename}_${sign}_NEW select * from  ${tablename};
rename table  ${tablename} to ${tablename}_${sign}_OLD;
rename table ${tablename}_${sign}_NEW to  ${tablename};
unlock tables;
EOF
[gbase@rh6-1 ~]$

执行效果

[gbase@rh6-1 ~]$ sh gbase_rebuild_table.sh testdb t2
--t2--
--------------
drop table if exists t2_ABCDEFG_OLD
--------------
Query OK, 0 rows affected, 3 warnings (Elapsed: 00:00:00.11)
--------------
drop table if exists t2_ABCDEFG_NEW
--------------
Query OK, 0 rows affected, 3 warnings (Elapsed: 00:00:00.23)
--------------
lock table  t2 write
--------------
Query OK, 0 rows affected (Elapsed: 00:00:00.00)
--------------
create table t2_ABCDEFG_NEW like t2
--------------
Query OK, 0 rows affected (Elapsed: 00:00:00.39)
--------------
insert into t2_ABCDEFG_NEW select * from  t2
--------------
Query OK, 0 rows affected (Elapsed: 00:00:00.10)
Records: 0  Duplicates: 0  Warnings: 0
--------------
rename table  t2 to t2_ABCDEFG_OLD
--------------
Query OK, 0 rows affected (Elapsed: 00:00:01.24)
--------------
rename table t2_ABCDEFG_NEW to  t2
--------------
Query OK, 0 rows affected (Elapsed: 00:00:00.93)
--------------
unlock tables
--------------
Query OK, 0 rows affected (Elapsed: 00:00:00.00)
Bye

对其它业务的影响

经测试,在lock期间,该表可以继续查询,但所有DML,DDL将阻塞,直到unlock tables. 之后阻塞的SQL将继续运行,或者阻塞超时而报错退出。

连接断开,锁会自动释放。

持有表锁的session,该表被rename, 甚至drop,并不会导致锁被释放。其它session如果对该表做操作,依然会等待表锁。

gbase> create table t2 like t1;
^CQuery aborted by Ctrl+C
ERROR 1727 (HY000): try to lock in gcluster failed:  (GBA-02LO-0002) Failed to lock: [testdb.t2580D5F90-B287-4199-B057-E6FBD44B5BFA] GC_AIS_ERR_EXIST


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69979525/viewspace-2840304/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2021-02-25

  • 博文量
    7
  • 访问量
    3114