当前位置:网站首页>从 SQL 文件迁移数据到 TiDB
从 SQL 文件迁移数据到 TiDB
2022-07-06 07:58:00 【添香小铺】
本文介绍如何使用 TiDB Lightning 从 MySQL SQL 文件迁移数据到 TiDB。关于如何生成 MySQL SQL 文件,请参考 Dumpling 文档中的导出为 SQL 文件。
前提条件
第 1 步:准备 SQL 文件
将所有 SQL 文件放到统一目录下,例如 /data/my_datasource/
或 s3://my-bucket/sql-backup?region=us-west-2
。Lightning 将递归地寻找该目录下及其子目录内的所有 .sql
文件。
第 2 步:定义目标表结构
要导入 TiDB,就必须为 SQL 文件提供表结构。
如果使用 Dumpling 工具导出数据,则会自动导出表结构文件。此外,其他方式导出的数据可以通过以下任一方法创建表结构:
方法一:使用 TiDB Lightning 创建表结构。
编写包含 DDL 语句的 SQL 文件:
- 文件名格式为
${db_name}-schema-create.sql
,其内容需包含CREATE DATABASE
语句。 - 文件名格式为
${db_name}.${table_name}-schema.sql
,其内容需包含CREATE TABLE
语句。
- 文件名格式为
方法二:手动在下游 TiDB 建库和表。
第 3 步:编写配置文件
新建文件 tidb-lightning.toml
,包含以下内容:
[lightning] # 日志 level = "info" file = "tidb-lightning.log" [tikv-importer] # "local":默认使用该模式,适用于 TiB 级以上大数据量,但导入期间下游 TiDB 无法对外提供服务。 backend = "local" # # "tidb":TiB 级以下数据量也可以采用 `tidb` 后端模式,下游 TiDB 可正常提供服务。关于后端模式更多信息请参考 https://docs.pingcap.com/zh/tidb/stable/tidb-lightning-backends 。 # 设置排序的键值对的临时存放地址,目标路径必须是一个空目录,目录空间须大于待导入数据集的大小。建议设为与 `data-source-dir` 不同的磁盘目录并使用闪存介质,独占 I/O 会获得更好的导入性能。 sorted-kv-dir = "${sorted-kv-dir}" [mydumper] # 源数据目录 data-source-dir = "${data-path}" # 本地或 S3 路径,例如:'s3://my-bucket/sql-backup?region=us-west-2' [tidb] # 目标集群的信息 host = ${host} # 例如:172.16.32.1 port = ${port} # 例如:4000 user = "${user_name}" # 例如:"root" password = "${password}" # 例如:"rootroot" status-port = ${status-port} # 导入过程 Lightning 需要在从 TiDB 的“状态端口”获取表结构信息,例如:10080 pd-addr = "${ip}:${port}" # 集群 PD 的地址,Lightning 通过 PD 获取部分信息,例如 172.16.31.3:2379。当 backend = "local" 时 status-port 和 pd-addr 必须正确填写,否则导入将出现异常。
关于配置文件更多信息,可参阅 TiDB Lightning Configuration。
第 4 步:执行导入
运行 tidb-lightning
。如果直接在命令行中启动程序,可能会因为 SIGHUP
信号而退出,建议配合 nohup
或 screen
等工具。
若从 S3 导入,则需将有权限访问该 Amazon S3 后端存储的账号的 SecretKey 和 AccessKey 作为环境变量传入 Lightning 节点。
export AWS_ACCESS_KEY_ID=${access_key} export AWS_SECRET_ACCESS_KEY=${secret_key} nohup tiup tidb-lightning -config tidb-lightning.toml > nohup.out 2>&1 &
同时,TiDB Lightning 还支持从 ~/.aws/credentials
读取凭证文件。
导入开始后,可以采用以下任意方式查看进度:
- 通过
grep
日志关键字progress
查看进度,默认 5 分钟更新一次。 - 通过监控面板查看进度,请参考 TiDB Lightning 监控。
- 通过 Web 页面查看进度,请参考 Web 界面。
导入完毕后,TiDB Lightning 会自动退出。查看日志的最后 5 行中会有 the whole procedure completed
,则表示导入成功。
注意
无论导入成功与否,最后一行都会显示 tidb lightning exit
。它只是表示 TiDB Lightning 正常退出,不代表任务完成。
边栏推荐
- Onie supports pice hard disk
- 22. Empty the table
- 08- [istio] istio gateway, virtual service and the relationship between them
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Machine learning - decision tree
- Parameter self-tuning of relay feedback PID controller
- 07- [istio] istio destinationrule (purpose rule)
- 【Redis】NoSQL数据库和redis简介
- 861. Score after flipping the matrix
猜你喜欢
Opencv learning notes 9 -- background modeling + optical flow estimation
Nacos Development Manual
Generator Foundation
Qualitative risk analysis of Oracle project management system
让学指针变得更简单(三)
IP lab, the first weekly recheck
861. Score after flipping the matrix
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
数据治理:主数据的3特征、4超越和3二八原则
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
P3047 [USACO12FEB]Nearby Cows G(树形dp)
Type of data in energy dashboard
【T31ZL智能视频应用处理器资料】
【Redis】NoSQL数据库和redis简介
Linked list interview questions (Graphic explanation)
继电反馈PID控制器参数自整定
好用的TCP-UDP_debug工具下载和使用
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
edge瀏覽器 路徑獲得
实现精细化生产, MES、APS、ERP必不可少
Hackathon ifm
Circuit breaker: use of hystrix
CAD ARX gets the current viewport settings
Pangolin Library: control panel, control components, shortcut key settings
MES, APS and ERP are essential to realize fine production
解决方案:智慧工地智能巡檢方案視頻監控系統
Document 2 Feb 12 16:54
Epoll and IO multiplexing of redis
Parameter self-tuning of relay feedback PID controller
Get the path of edge browser