Import Data into TiDB Cloud Premium using the MySQL Command-Line Client
This document describes how to import data into TiDB Cloud Premium using the MySQL Command-Line Client (mysql). The following sections provide step-by-step instructions for importing data from SQL or CSV files. This process performs a logical import, where the MySQL Command-Line Client replays SQL statements from your local machine against TiDB Cloud.
Prerequisites
Before you can import data to a TiDB Cloud Premium instance via the MySQL Command-Line Client, you need the following prerequisites:
- You have access to your TiDB Cloud Premium instance.
- Install the MySQL Command-Line Client (
mysql) on your local computer.
Step 1. Connect to your TiDB Cloud Premium instance
Connect to your TiDB instance using the MySQL Command-Line Client. If this is your first time, perform the following steps to configure the network connection and generate the TiDB SQL root user password:
Log in to the TiDB Cloud console and navigate to the TiDB Instances page. Then, click the name of your target instance to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
Ensure that the configurations in the connection dialog match your operating environment.
- Connection Type is set to
Public. - Connect With is set to
MySQL CLI. - Operating System matches your environment.
- Connection Type is set to
Click Generate Password to create a random password. If you have already configured a password, reuse that credential or rotate it before proceeding.
Step 2. Define the target database and table schema
Before importing data, create the target table structure that matches your dataset.
The following is an example SQL file (products-schema.sql) that creates a sample database and table. Update the database or table names to match your environment.
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(255),
price DECIMAL(10, 2)
);
Run the schema file against your TiDB Cloud Premium instance so the database and table exist before you load data in the next step.
Step 3. Import data from an SQL or CSV file
Use the MySQL Command-Line Client to load data into the schema you created in Step 2. Replace the placeholders with your own file paths, credentials, and dataset as needed, then follow the workflow that matches your source format.
Do the following to import data from an SQL file:
Provide an SQL file (for example,
products.sql) that contains the data you want to import. This SQL file must includeINSERTstatements with data, similar to the following:INSERT INTO products (product_id, product_name, price) VALUES (1, 'Laptop', 999.99), (2, 'Smartphone', 499.99), (3, 'Tablet', 299.99);Use the following command to import data from the SQL file:
mysql --comments --connect-timeout 150 \ -u '<your_username>' -h <your_instance_host> -P 4000 -D test \ --ssl-mode=VERIFY_IDENTITY --ssl-ca=<your_ca_path> \ -p<your_password> < products.sqlReplace the placeholder values (for example,
<your_username>,<your_instance_host>,<your_password>,<your_ca_path>, and the SQL file name) with your own connection details and file path.
Do the following to import data from a CSV file:
Ensure the target database and table exist in TiDB (for example, the
productstable you created in Step 2).Provide a sample CSV file (for example,
products.csv) that contains the data you want to import. The following is an example:products.csv:
product_id,product_name,price 1,Laptop,999.99 2,Smartphone,499.99 3,Tablet,299.99Use the following command to import data from the CSV file:
mysql --comments --connect-timeout 150 \ -u '<your_username>' -h <your_instance_host> -P 4000 -D test \ --ssl-mode=VERIFY_IDENTITY --ssl-ca=<your_ca_path> \ -p<your_password> \ -e "LOAD DATA LOCAL INFILE '<your_csv_path>' INTO TABLE products FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' IGNORE 1 LINES (product_id, product_name, price);"Replace the placeholder values (for example,
<your_username>,<your_instance_host>,<your_password>,<your_ca_path>,<your_csv_path>, and the table name) with your own connection details and dataset paths.
Step 4. Validate the imported data
After the import is complete, run basic queries to verify that the expected rows are present and the data is correct.
Use the MySQL Command-Line Client to connect to the same database and run validation queries, such as counting rows and inspecting sample records:
mysql --comments --connect-timeout 150 \
-u '<your_username>' -h <your_instance_host> -P 4000 -D test \
--ssl-mode=VERIFY_IDENTITY --ssl-ca=<your_ca_path> \
-p<your_password> \
-e "SELECT COUNT(*) AS row_count FROM products; \
SELECT * FROM products ORDER BY product_id LIMIT 5;"
Expected output (example):
+-----------+
| row_count |
+-----------+
| 3 |
+-----------+
+------------+---------------+--------+
| product_id | product_name | price |
+------------+---------------+--------+
| 1 | Laptop | 999.99 |
| 2 | Smartphone | 499.99 |
| 3 | Tablet | 299.99 |
+------------+---------------+--------+
Replace the placeholder values with your own connection details, and adjust the validation queries to suit the shape of your dataset.