首页 > 数据库 > 国内数据库 > GBase 8a安全重建表的一个方案
当由于某些原因需要重建表时,需要锁定表,避免该表在重建期间又被更改,导致新表和老表数据不一致。本文提供一个应用层的安全重建方案。
方案内容
该方案通过如下几个步骤来实现
如下的一个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/ ,如需转载,请注明出处,否则将追究法律责任。