有时候出于线上真实数据模拟的需求,我们需要整库把线上的数据拉到测试环境进行测试,这就涉及到整库的导入和导出问题,有这么几个问题需要处理:
- 导出的语句和文件不能是一个,否则如果线上数据几千万级别的情况下,会导致超时或者来自DBA的问候,所以最好一个表一个文件,一个表的话哪怕百万级也不算太慢
- 导入的语句最好是一个,或者支持批量选中文件导入,否则一个一个导太慢了
我的实际情况是,线上有100多张表,总共大概11.5GB,数据量在2000万级别。于是调研了下方法,发现:
- 对于导出而言:DateGrip支持整库Dump和批量导出所有表,Navicat支持整库Dump,但不支持批量导出所有表
- 对于导入而言:DateGrip支持批量导入所有表,但是速度非常慢,平均1分钟2000条,Navicat非常快,但是不支持批量导入
所以我决定分别使用这两个工具进行处理,导出用DateGrip,导入用Navicat。以下为自己的数据示例
1 通过DateGrip导出
选中指定库,并右键选中导出数据到文件:
设置导出选项:
执行导入即可
2 sql文件合成为一个
由于Navicat不支持批量执行sql语句,所以我们需要把导出的文件merge为一个大的sql文件,进入到导出目录下:
使用如下命令进行文件合成:
- windows电脑:
copy *.sql merge.sql
- mac电脑:
cat *.sql > merge.sql
以windows为例我们在目录下调出cmd
打开该文件可以看到是全量的sql:
create table bank_account ( id int not null primary key, bank_name varchar(50) not null, account_name varchar(50) not null, person_id int not null, remark tinytext null, constraint bank_account_id_uindex unique (id) ); INSERT INTO test.bank_account (id, bank_name, account_name, person_id, remark) VALUES (1, '农业银行', 'tml_农业', 1, null); INSERT INTO test.bank_account (id, bank_name, account_name, person_id, remark) VALUES (2, '中国银行', 'tml_中国', 1, null); INSERT INTO test.bank_account (id, bank_name, account_name, person_id, remark) VALUES (3, '北京银行', 'tml_北京', 1, null); INSERT INTO test.bank_account (id, bank_name, account_name, person_id, remark) VALUES (4, '华夏银行', 'gcy_华夏', 2, null); INSERT INTO test.bank_account (id, bank_name, account_name, person_id, remark) VALUES (5, '北京银行', 'gcy_北京', 2, null); create table person ( id int not null primary key, username varchar(50) not null, password varchar(50) not null, age int not null, phone int not null, email varchar(50) not null, hobby varchar(50) not null, remark tinytext null ); create index username__index on person (username); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (0, 'mhw', '343444', 22, 1983499834, '123234@qq.com', '跳远', null); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (1, 'tml', '123456', 30, 1234566454, '434355@qq.com', '篮球', null); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (2, 'gcy', '111111', 30, 1234555555, '155555@qq.com', '足球', null); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (4, 'wcong', '111111', 30, 11111111, '111111@qq.com', '跳远', null); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (7, 'aaaa', '111111', 30, 11111111, '111111@qq.com', '跳远', null); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (9, 'tmlnew', '12345565', 23, 18810578, '133044@qq.com', '足球', null); INSERT INTO test.person (id, username, password, age, phone, email, hobby, remark) VALUES (99, 'tml', '12345', 999, 112223, '12345@qq.com', '写代码', null);
3 通过Navicat导入
拿到拼接好的导出文件之后,我们就可以进行导入了,打开navicat,找到我们创建的test_prod库运行合并的sql文件:
运行结果如下: