The mysqlimport and mysqldump client programs provide a command-line interface for importing and exporting data. mysqlimport imports data files into tables. mysqldump exports tables to data files. 15.3.1. Importing Data with mysqlimportThe mysqlimport client program loads data files into tables. It provides a command-line interface to the LOAD DATA INFILE statement. That is, mysqlimport examines the options given on the command line. It then connects to the server and, for each input file named in the command, issues a LOAD DATA INFILE statement that loads the file into the appropriate table. Because mysqlimport works this way, to use it most effectively, you should be familiar with the LOAD DATA INFILE statement, which is discussed in Section 15.2.1, "Importing Data with LOAD DATA INFILE." This section describes mysqlimport invocation syntax and how its options correspond to various clauses of the LOAD DATA INFILE statement. Invoke mysqlimport from the command line as follows: shell> mysqlimport options db_name input_file ... db_name names the database containing the table to be loaded and input_file names the file that contains the data to be loaded. You can name several input files following the database name if you like. mysqlimport uses each filename to determine the name of the corresponding table into which the file's contents should be loaded. The program does this by stripping off any filename extension (the last period and anything following it) and using the result as the table name. For example, mysqlimport treats a file named City.txt or City.dat as input to be loaded into a table named City. After determining the table name corresponding to the filename, mysqlimport issues a LOAD DATA INFILE statement to load the file into the table. Each table to be loaded by mysqlimport must already exist, and each input file should contain only data values. mysqlimport isn't intended for processing dump files that consist of SQL statements. (Such files can be created with the mysqldump program; for instructions on processing an SQL-format dump file, see Section 32.8.1, "Reloading mysqldump Output.") The options part of the mysqlimport command may include any of the standard connection parameter options, such as --host or --user. You'll need to supply these options if the default connection parameters aren't appropriate. mysqlimport also understands options specific to its own operation. Invoke mysqlimport with the --help option to see a complete list of the options that can be used to tell mysqlimport the actions you want it to perform. By default, input files for mysqlimport are assumed to contain lines terminated by newlines, with each line containing tab-separated data values. This is the same default format assumed by the LOAD DATA INFILE statement. For an input file that's in a different format, use the following options to tell mysqlimport how to interpret the file:
The preceding options give you the flexibility to load input files containing data in a variety of formats. Some examples follow; each one loads an input file named City.txt into the City table in the world database. Commands that are shown on multiple lines should be entered on a single line. The following command loads a file that has lines ending in carriage return/linefeed pairs: shell> mysqlimport --lines-terminated-by="\r\n" world City.txt Note that the --lines-terminated-by value is quoted with double quotes. Format option values often contain special characters, such as backslash, that might have special meaning to your command interpreter. It might be necessary to quote such characters to tell your command interpreter to pass them unchanged to mysqlimport. The syntax for specifying a double quote is trickier and depends on which command interpreter you use. The following command loads a data file containing values quoted by double quote characters: shell> mysqlimport --fields-enclosed-by='"' world City.txt This command should work on most Unix shells, which allow the double quote character to be quoted within single quotes. This doesn't work on Windows, where you must specify a double quote within a double-quoted string by escaping it: shell> mysqlimport --fields-enclosed-by="\"" world City.txt The following command loads a file that has data values separated by commas and lines ending with carriage returns: shell> mysqlimport --fields-terminated-by=, --lines-terminated-by="\r" world City.txt Other mysqlimport options provide additional control over data file loading. The following list discusses some of those you're likely to find useful:
15.3.2. Exporting Data with mysqldumpThe mysqldump client program dumps table contents to files. It is useful for making database backups or for transferring database contents to another server. mysqldump can export tables as tab-delimited data files or produce SQL-format dump files that contain CREATE TABLE and INSERT statements for re-creating the dumped files. This section discusses how to use mysqldump to export tables as data files. For information on creating SQL-format dump files, see Section 32.4.2, "Making Text Backups with mysqldump." To use mysqldump to export tables as tab-delimited data files, specify the --tab=dir_name (or -T dir_name) option on the command line. This option causes mysqldump to issue SELECT … INTO OUTFILE statements to tell the MySQL server to write each dumped table as a tab-delimited text file in the dir_name directory. For each table, mysqldump itself writes a file containing a CREATE TABLE statement that you can use to re-create the table before reloading the data file into it. Invoke mysqldump from the command line as follows: shell> mysqldump --tab=dir_name options db_name tbl_name ... db_name names the database containing the table to be exported and tbl_name names the table to be exported. To export multiple tables, name all of them following the database name. If you don't provide any table names, mysqldump exports all tables in the database. The options part of the mysqldump command may include any of the standard connection parameter options, such as --host or --user. You'll need to supply these options if the default connection parameters aren't appropriate. mysqldump also understands options specific to its own operation. Invoke mysqldump with the --help option to see a complete list of the options that can be used to tell mysqldump the actions you want it to perform. Suppose that you dump the table City from the world database using the /tmp directory as the output directory: shell> mysqldump --tab=/tmp world City The output consists of a City.sql file containing the CREATE TABLE statement for the table, and a City.txt file containing the table data. To reload data exported by invoking mysqldump with the --tab option, change location into the dump directory. Then use mysql to process the .sql file that contains the CREATE TABLE statement, and use mysqlimport to load the .txt file that contains the table data: shell> cd /tmp shell> mysql world < City.sql shell> mysqlimport world City.txt Using --tab to produce tab-delimited dump files is much faster than creating SQL-format files, but you should keep in mind the following points:
The default data file format produced by the --tab option consists of tab-delimited lines with newline terminators. This is the same default format assumed by the SELECT … INTO OUTFILE statement. To control the format of the data files that mysqldump generates, use the following options:
The mysqlimport program has similar options for describing the data file format. See the discussion of that program for information about specifying char or string values that contain characters that your command interpreter considers special. If you combine the --tab option with format-control options such as --fields-terminated-by and --fields-enclosed-by, you should specify the same format-control options with mysqlimport so that it knows how to interpret the data files. The --all-databases and --databases options for mysqldump are used for dumping multiple databases. You cannot use those options together with --tab, which causes mysqldump to write the files for all dumped tables to a single directory. You would have no way to tell which files correspond to tables in each database. |