MySQL Import Data
In this chapter, we introduce several simple MySQL data import commands.
mysql -u your_username -p -h your_host -P your_port -D your_database
1. Importing with the mysql command
The syntax for importing with the mysql command is:
mysql -u your_username -p -h your_host -P your_port -D your_database
your_username, your_host, your_port, and your_database are your MySQL username, host, port, and database, respectively.
Example:
# mysql -uroot -p123456 < runoob.sql
The above command imports the entire backed-up database runoob.sql.
After executing the above command, the system will ask for the MySQL user's password. Enter the password and press Enter.
In this way, MySQL will execute the statements in the SQL file and import the data into the specified database.
Please note that if the SQL file contains statements to create a database, make sure the database already exists before performing the import. If the file contains statements to create tables, make sure the tables do not exist or are empty, so as to avoid conflicts when importing data.
2. Importing with the source command
To import a database with the source command, you first need to log in to the database terminal:
mysql> create database abc; # 创建数据库
mysql> use abc; # 使用已创建的数据库
mysql> set names utf8; # 设置编码
mysql> source /home/abc/abc.sql # 导入备份数据库
The advantage of using the source command is that you can execute it directly in the MySQL command line without exiting MySQL and using other commands.
3. Importing Data with LOAD DATA
MySQL provides the LOAD DATA INFILE statement to insert data. The following example reads the file dump.txt from the current directory and inserts the data from that file into the mytbl table of the current database.
mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl;
If the LOCAL keyword is specified, it indicates that the file is read by path from the client host. If not specified, the file is read by path on the server.
You can explicitly specify the separator for column values and the end-of-line marker in the LOAD DATA statement, but the default markers are the tab and the newline.
The syntax of the FIELDS and LINES clauses is the same for both commands. Both clauses are optional, but if both are specified, the FIELDS clause must appear before the LINES clause.
If a user specifies a FIELDS clause, its subclauses (TERMINATED BY, [OPTIONALLY] ENCLOSED BY, and ESCAPED BY) are also optional. However, the user must specify at least one of them.
mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl
-> FIELDS TERMINATED BY ':'
-> LINES TERMINATED BY '\r\n';
By default, LOAD DATA inserts data according to the order of the columns in the data file. If the columns in the data file do not match the columns in the table being inserted into, you need to specify the column order.
For example, if the column order in the data file is a,b,c but the column order in the insert table is b,c,a, the data import syntax is as follows:
mysql> LOAD DATA LOCAL INFILE 'dump.txt'
-> INTO TABLE mytbl (b, c, a);
4. Importing Data with mysqlimport
The mysqlimport client provides a command-line interface to the LOAD DATA INFILEQL statement. Most options of mysqlimport correspond directly to LOAD DATA INFILE clauses.
To import data from the file dump.txt into the mytbl table, you can use the following command:
$ mysqlimport -u root -p --local mytbl dump.txt
password *****
The mysqlimport command can specify options to set a specific format. The command syntax is as follows:
$ mysqlimport -u root -p --local --fields-terminated-by=":" \
--lines-terminated-by="\r\n" mytbl dump.txt
password *****
The --columns option is used in the mysqlimport statement to set the column order:
$ mysqlimport -u root -p --local --columns=b,c,a \
mytbl dump.txt
password *****
Introduction to Common mysqlimport Options
| Option | Function |
|---|---|
| -d or --delete | Delete all information from the data table before importing new data into it |
| -f or --force | Regardless of whether errors are encountered, mysqlimport will force the insertion of data to continue |
| -i or --ignore | mysqlimport skips or ignores rows with the same unique key, and the data in the imported file will be ignored. |
| -l or -lock-tables | The table is locked before data is inserted, which prevents users' queries and updates from being affected while you are updating the database. |
| -r or -replace | This option has the opposite effect of the -i option; this option will replace records in the table that have the same unique key. |
| --fields-enclosed- by= char | Specifies what the data records in the text file are enclosed by. In many cases, data is enclosed by double quotes. By default, data is not enclosed by any character. |
| --fields-terminated- by=char | Specifies the separator between individual data values. In a period-separated file, the separator is a period. You can use this option to specify the separator between data. The default separator is the tab character (Tab) |
| --lines-terminated- by=str | This option specifies the separator string or character between rows of data in the text file. By default, mysqlimport uses newline as the row separator. You can choose to use a string instead of a single character: a new line or a carriage return. |
Other commonly used options of the mysqlimport command include -v to display the version, and -p to prompt for a password, etc.