1.清空相关表
1.1、先查询相关表,如订单表(order)
select concat('truncate table `', t.table_schema, '`.', t.table_name, '')
from information_schema.tables t
where t.table_schema = '你的数据库名'
and t.table_type = 'base table'
and t.table_name like '%order%';1.2、执行清空truncate语句
truncate table `数据库`.tb_order
2.更改所有表租户id
2.1、动态生成所有表的更新语句,且有租户字段(tenant_id)的才需要更新,没有不更新。
select concat('update `', t.table_name, '` set tenant_id = ''新租户'' where tenant_id = ''旧租户'';')
from information_schema.tables t
join information_schema.columns c
on c.table_schema = t.table_schema
and c.table_name = t.table_name
where t.table_schema = '你的数据库名'
and t.table_type = 'base table'
and c.column_name = 'tenant_id';
2.2、复制结果执行
-- 1. 先禁用外键检查(避免约束报错) set foreign_key_checks = 0; -- 2. 遍历所有表,批量更新 tenant_id update table1 set tenant_id = 200 where tenant_id = 100; -- 动态生成所有表的更新语句 -- 复制结果执行 -- 3. 恢复外键检查 set foreign_key_checks = 1;
3.复制定时任务(xxl-job)
字段job_group为执行器表xxl_job_group的id
insert into `enuo-xxl-job`.`xxl_job_info`(`job_group`, `job_cron`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`, `trigger_status`, `trigger_last_time`, `trigger_next_time`) select 13 job_group, job_cron, job_desc, add_time, update_time, author, alarm_email, executor_route_strategy, executor_handler, executor_param, executor_block_strategy, executor_timeout, executor_fail_retry_count, glue_type, glue_source, glue_remark, glue_updatetime, child_jobid, trigger_status, trigger_last_time, trigger_next_time from `enuo-xxl-job`.xxl_job_info where `job_group` = '10' order by id;
总结
到此这篇关于mysql复制数据库及更改租户、定时任务等的文章就介绍到这了,更多相关mysql复制数据库、更改租户、定时任务内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论