Wu Yuxiong - natural born MySQL study notes: MySQL export data

MySQL can use SELECT ... INTO OUTFILE statements to export the data to a simple text file.
Use SELECT ... INTO OUTFILE statement export data 
the following data in the table data instance runoob_tbl to export / tmp / runoob.txt file: 
MySQL > the SELECT * the FROM runoob_tbl 
     -> the INTO the OUTFILE ' /tmp/runoob.txt ' ;
You can be set by the data output command options specify the format, CSV format to derive the following examples: 
MySQL > the SELECT * the passwd the INTO the OUTFILE the FROM ' /tmp/runoob.txt ' 
    -> the FIELDS TERMINATED BY ' , ' the ENCLOSED BY ' " ' 
    -> TERMINATED BY LINES ' \ R & lt \ n- ' ;
Generating a file, the values separated by commas. This format can be used by many programs. 
A the SELECT, B, A + B the INTO the OUTFILE ' /tmp/result.text ' 
the FIELDS TERMINATED BY ' , ' OPTIONALLY the ENCLOSED BY ' " ' 
LINES TERMINATED BY ' \ n- ' 
the FROM test_table;
SELECT ... INTO OUTFILE statement has the following properties: 
the LOAD the DATA INFILE SELECT ... INTO OUTFILE is the inverse operation, SELECT syntax. In order to write data to a database of a file, use SELECT ... INTO OUTFILE, to read the file back to the database, use LOAD DATA INFILE. 
The OUTFILE the INTO ... SELECT ' file_name ' form of SELECT selected row can be written to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. 
Output can not be an existing file. Prevent data from being tampered file. 
You need to have a login account server to retrieve the file. Otherwise, SELECT ... INTO OUTFILE will not play any role. 
In UNIX, after the file is created is readable permissions owned by the MySQL server. This means that although you can read the file, but may not be able to remove it.
Export table as the original data 
mysqldump mysql utility for transfer is stored in the database. It mainly produces a SQL script that contains the database from scratch to re-create the necessary command CREATE TABLE INSERT and so on. 
Use mysqldump to export data to use - the Tab option to specify the export file specified directory, the target must be written. 
The following examples will be exported to the data table runoob_tbl / tmp directory: 
$ the mysqldump -u the root -p --no-create- info \
             --tab = / tmp RUNOOB runoob_tbl 
password ******
Export SQL file data to the specified format, as follows: 
$ the mysqldump -u RUNOOB runoob_tbl the root -p> dump.txt 
password ******
If you need to export data for the entire database, you can use the following command: 
$ mysqldump -u root -p RUNOOB> database_dump.txt 
password ****** 
If you need to back up all the databases, you can use the following command: 
$ mysqldump -u root - --all-Databases the p-> database_dump.txt 
password ****** 
--all-in MySQL 3.23.12 Databases option to join or later. 
This method can be used to implement a backup policy database.
The data sheet and copy of the database to another host 
if you need to copy data to other MySQL server, you can specify the name of the database and data tables mysqldump command. 

Execute the following command on the source host, back up data to a file dump.txt: 
$ mysqldump -u root -p database_name table_name> dump.txt 
password ***** 

if a full backup of the database, you do not need to use a specific table name. 
If you need to back up the database into MySQL server, you can use the following command, use the following command need to make sure the database has been created: 
$ MySQL -u root -p database_name < dump.txt 
password *****
You can also use the following command to export the data directly into the remote server, but make sure both servers are connected, be accessible to each other: 
$ mysqldump -u root - the p-database_name \
        | -h other- MySQL Host. com database_name 
above the pipeline used in the command to import the exported data to the specified remote host.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12114782.html