PolarDB-X is compatible with the import and export syntax of MySQL. This topic describes several data import and export methods supported by PolarDB-X.
Data import methods
Use the SOURCE statements to import data
Before you execute the source command, log on to the client of the destination database. Then, execute the following statements:
create database abc; # Create a database.
use abc; # Use the created database.
set names utf8; # Configure the encoding scheme of the data that you want to import.
source /home/abc/abc.sql # Import the data to the database that you created.
Use a MySQL command to import data
The following code block provides the syntax of the MySQL command that can be run to import data to a database:
mysql -u username -p password < Data (runoob.sql) that you want to import from the source database
Use the LOAD DATA statements to import data
For more information, see Use LOAD DATA to import data.
Use a program to import data
For more information, see Use a program to import data.
Use Batch Tool to import data
For more information, see Use Batch Tool to export and import data.
Data export methods
Use the mysql -e command to export data
Run the mysql -e
command to connect to a local or remote server and then execute SQL statements. For example, you can execute the SELECT statement to query data. The values in the raw data are separated with tab characters. You can use string manipulation functions to change the separator to commas ','
and save the file in the .csv format. The following code provides an example:
mysql -h ip -P port -u usr -pPassword db_name -N -e "SELECT id,k,c,pad FROM sbtest1;" >/home/data_1000w.txt
## The values in the raw data are separated with tab characters. For example, the returned result of this command is 188092293 27267211 59775766593-64673028018-...-09474402685 01705051424-...-54211554755.
mysql -h ip -P port -u usr -pPassword db_name -N -e "SELECT id,k,c,pad FROM sbtest1;" | sed 's/\t/,/g' >/home/data_1000w.csv
## After you execute this command, the values in the output are separated with commas (,). For example, the returned result is 188092293,272 67211,597 75766593-64673028018-...-09474402685,017 05051424-...-54211554755
Use the SELECT... INTO OUTFILE statements to export data
Syntax
SELECT ... INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
[export_options]
export_options:
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
Example
SELECT customer_id, firstname, surname INTO OUTFILE '/exportdata/customers.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM customers;
Description
The syntax of OUTFILE in PolarDB-X is the same as that of the mysql command. The exported files are stored on the PolarDB-X compute node on which the session of the SQL statement performed.
Use the mysqldump tool to export data
For more information, see Use mysqldump to migrate data.
Use Batch Tool to export data
For more information, see Use Batch Tool to export and import data.
How to select the tool to import and export data
For more information, see Optimize data import and export.