|
| 1 | +--- |
| 2 | +title: TiDB Lightning "TiDB" Back End |
| 3 | +summary: Choose how to write data into the TiDB cluster. |
| 4 | +category: reference |
| 5 | +--- |
| 6 | + |
| 7 | +# TiDB Lightning "TiDB" Back End |
| 8 | + |
| 9 | +TiDB Lightning supports two back ends: "Importer" and "TiDB". It determines how `tidb-lightning` delivers data into the target cluster. |
| 10 | + |
| 11 | +The "Importer" back end (default) requires `tidb-lightning` to first encode the SQL or CSV data into KV pairs, and relies on the external `tikv-importer` program to sort these KV pairs and ingest directly into the TiKV nodes. |
| 12 | + |
| 13 | +The "TiDB" back end requires `tidb-lightning` to encode these data into SQL `INSERT` statements, and has these statements executed directly on the TiDB node. |
| 14 | + |
| 15 | +| Back end | "Importer" | "TiDB" | |
| 16 | +|:---|:---|:---| |
| 17 | +| Speed | Fast (~300 GB/hr) | Slow (~50 GB/hr) | |
| 18 | +| Resource usage | High | Low | |
| 19 | +| ACID respected while importing | No | Yes | |
| 20 | +| Target tables | Must be empty | Can be populated | |
| 21 | + |
| 22 | +## Deployment for "TiDB" back end |
| 23 | + |
| 24 | +When using the "TiDB" back end, you no longer need `tikv-importer`. Compared with the [standard deployment procedure](/dev/reference/tools/tidb-lightning/deployment.md), the "TiDB" back end deployment has the following two differences: |
| 25 | + |
| 26 | +* Steps involving `tikv-importer` can all be skipped. |
| 27 | +* The configuration must be changed to indicate the "TiDB" back end is used. |
| 28 | + |
| 29 | +### Ansible deployment |
| 30 | + |
| 31 | +1. The `[importer_server]` section in `inventory.ini` can be left blank. |
| 32 | + |
| 33 | + ```ini |
| 34 | + ... |
| 35 | + |
| 36 | + [importer_server] |
| 37 | + # keep empty |
| 38 | + |
| 39 | + [lightning_server] |
| 40 | + 192.168.20.10 |
| 41 | + |
| 42 | + ... |
| 43 | + ``` |
| 44 | + |
| 45 | +2. The `tikv_importer_port` setting in `group_vars/all.yml` is ignored, and the file `group_vars/importer_server.yml` does not need to be changed. But you need to edit `conf/tidb-lightning.yml` and change the `backend` setting to `tidb`. |
| 46 | + |
| 47 | + ```yaml |
| 48 | + ... |
| 49 | + tikv_importer: |
| 50 | + backend: "tidb" # <-- change this |
| 51 | + ... |
| 52 | + ``` |
| 53 | + |
| 54 | +3. Bootstrap and deploy the cluster as usual. |
| 55 | + |
| 56 | +4. Mount the data source for TiDB Lightning as usual. |
| 57 | + |
| 58 | +5. Start `tidb-lightning` as usual. |
| 59 | + |
| 60 | +### Manual deployment |
| 61 | + |
| 62 | +You do not need to download and configure `tikv-importer`. |
| 63 | + |
| 64 | +Before running `tidb-lightning`, add the following lines into the configuration file: |
| 65 | + |
| 66 | +```toml |
| 67 | +[tikv-importer] |
| 68 | +backend = "tidb" |
| 69 | +``` |
| 70 | + |
| 71 | +or supplying the `--backend tidb` arguments when executing `tidb-lightning`. |
| 72 | + |
| 73 | +## Conflict resolution |
| 74 | + |
| 75 | +The "TiDB" back end supports importing to an already-populated table. However, the new data might cause a unique key conflict with the old data. You can control how to resolve the conflict by using this task configuration. |
| 76 | + |
| 77 | +```toml |
| 78 | +[tikv-importer] |
| 79 | +backend = "tidb" |
| 80 | +on-duplicate = "replace" # or "error" or "ignore" |
| 81 | +``` |
| 82 | + |
| 83 | +| Setting | Behavior on conflict | Equivalent SQL statement | |
| 84 | +|:---|:---|:---| |
| 85 | +| replace | New entries replace old ones | `REPLACE INTO ...` | |
| 86 | +| ignore | Keep old entries and ignore new ones | `INSERT IGNORE INTO ...` | |
| 87 | +| error | Abort import | `INSERT INTO ...` | |
| 88 | + |
| 89 | +## Migrating from Loader to TiDB Lightning "TiDB" back end |
| 90 | + |
| 91 | +TiDB Lightning using the "TiDB" back end can completely replace functions of [Loader](/dev/reference/tools/loader.md). The following list shows how to translate Loader configurations into [TiDB Lightning configurations](/dev/reference/tools/tidb-lightning/config.md). |
| 92 | + |
| 93 | +<table> |
| 94 | +<thead><tr><th>Loader</th><th>TiDB Lightning</th></tr></thread> |
| 95 | +<tbody> |
| 96 | +<tr><td> |
| 97 | + |
| 98 | +```toml |
| 99 | + |
| 100 | +# logging |
| 101 | +log-level = "info" |
| 102 | +log-file = "loader.log" |
| 103 | + |
| 104 | +# Prometheus |
| 105 | +status-addr = ":8272" |
| 106 | + |
| 107 | +# concurrency |
| 108 | +pool-size = 16 |
| 109 | +``` |
| 110 | + |
| 111 | +</td><td> |
| 112 | + |
| 113 | +```toml |
| 114 | +[lightning] |
| 115 | +# logging |
| 116 | +level = "info" |
| 117 | +file = "tidb-lightning.log" |
| 118 | + |
| 119 | +# Prometheus |
| 120 | +pprof-port = 8289 |
| 121 | + |
| 122 | +# concurrency (better left as default) |
| 123 | +#region-concurrency = 16 |
| 124 | +``` |
| 125 | + |
| 126 | +</td></tr> |
| 127 | +<tr><td> |
| 128 | + |
| 129 | +```toml |
| 130 | + |
| 131 | +# checkpoint database |
| 132 | + |
| 133 | +checkpoint-schema = "tidb_loader" |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +``` |
| 141 | + |
| 142 | +</td><td> |
| 143 | + |
| 144 | +```toml |
| 145 | +[checkpoint] |
| 146 | +# checkpoint storage |
| 147 | +enable = true |
| 148 | +schema = "tidb_lightning_checkpoint" |
| 149 | +# by default the checkpoint is stored in |
| 150 | +# a local file, which is more efficient. |
| 151 | +# but you could still choose to store the |
| 152 | +# checkpoints in the target database with |
| 153 | +# this setting: |
| 154 | +#driver = "mysql" |
| 155 | +``` |
| 156 | + |
| 157 | +</td></tr> |
| 158 | +<tr><td> |
| 159 | + |
| 160 | +```toml |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +``` |
| 165 | + |
| 166 | +</td><td> |
| 167 | + |
| 168 | +```toml |
| 169 | +[tikv-importer] |
| 170 | +# use the "TiDB" back end |
| 171 | +backend = "tidb" |
| 172 | +``` |
| 173 | + |
| 174 | +</td></tr> |
| 175 | +<tr><td> |
| 176 | + |
| 177 | +```toml |
| 178 | + |
| 179 | +# data source directory |
| 180 | +dir = "/data/export/" |
| 181 | +``` |
| 182 | + |
| 183 | +</td><td> |
| 184 | + |
| 185 | +```toml |
| 186 | +[mydumper] |
| 187 | +# data source directory |
| 188 | +data-source-dir = "/data/export" |
| 189 | +``` |
| 190 | + |
| 191 | +</td></tr> |
| 192 | + |
| 193 | +<tr><td> |
| 194 | + |
| 195 | +```toml |
| 196 | +[db] |
| 197 | +# TiDB connection parameters |
| 198 | +host = "127.0.0.1" |
| 199 | +port = 4000 |
| 200 | + |
| 201 | +user = "root" |
| 202 | +password = "" |
| 203 | + |
| 204 | +#sql-mode = "" |
| 205 | +``` |
| 206 | + |
| 207 | +</td><td> |
| 208 | + |
| 209 | +```toml |
| 210 | +[tidb] |
| 211 | +# TiDB connection parameters |
| 212 | +host = "127.0.0.1" |
| 213 | +port = 4000 |
| 214 | +status-port = 10080 # <- this is required |
| 215 | +user = "root" |
| 216 | +password = "" |
| 217 | + |
| 218 | +#sql-mode = "" |
| 219 | +``` |
| 220 | + |
| 221 | +</td></tr> |
| 222 | +</tbody> |
| 223 | +</table> |
0 commit comments