Leilin Peng Share: MySQL export data

  MySQL export data

  MySQL you can use SELECT ... INTO OUTFILE statements to export the data to a simple text file.

  Use SELECT ... INTO OUTFILE statements to export data

  The following examples we will export the data to a data table tutorials_tbl /tmp/tutorials.txt file:

  mysql> SELECT * FROM tutorials_tbl

  -> INTO OUTFILE '/tmp/tutorials.txt';

  You can set the output data format specified by the command options, the following examples for the exported CSV format:

  mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/tutorials.txt'

  -> FIELDS TERMINATED BY ',' ENCLOSED BY '"'

  -> LINES TERMINATED BY '\r\n';

  In the following example, to generate a file, the values ​​separated by commas. This format can be used by many programs.

  SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'

  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

  LINES TERMINATED BY '\n'

  FROM test_table;

  SELECT ... INTO OUTFILE statement has the following properties:

  LOAD 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.

  SELECT ... INTO OUTFILE '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 --tab option to specify the export file specified directory, the target must be written.

  The following examples in Table tutorials_tbl data exported to the / tmp directory:

  $ mysqldump -u root -p --no-create-info \

  --tab=/tmp TUTORIALS tutorials_tbl

  password ******

  Export data in SQL format

  Export SQL file data to the specified format, as follows:

  $ mysqldump -u root -p TUTORIALS tutorials_tbl > dump.txt

  password ******

  The above command to create the file contents are as follows:

  -- MySQL dump 8.23

  --

  -- Host: localhost Database: TUTORIALS

  ---------------------------------------------------------

  -- Server version 3.23.58

  --

  -- Table structure for table `tutorials_tbl`

  --

  CREATE TABLE tutorials_tbl (

  tutorial_id int(11) NOT NULL auto_increment,

  tutorial_title varchar(100) NOT NULL default '',

  tutorial_author varchar(40) NOT NULL default '',

  submission_date date default NULL,

  PRIMARY KEY (tutorial_id),

  UNIQUE KEY AUTHOR_INDEX (tutorial_author)

  ) TYPE=MyISAM;

  --

  -- Dumping data for table `tutorials_tbl`

  --

  INSERT INTO tutorials_tbl

  VALUES (1,'Learn PHP','John Poul','2007-05-24');

  INSERT INTO tutorials_tbl

  VALUES (2,'Learn MySQL','Abdul S','2007-05-24');

  INSERT INTO tutorials_tbl

  VALUES (3,'JAVA Tutorial','Sanjay','2007-05-06');

  If you need to export data for the entire database, you can use the following command:

  $ mysqldump -u root -p TUTORIALS > database_dump.txt

  password ******

  If you need to back up all the databases, you can use the following command:

  $ mysqldump -u root -p --all-databases > database_dump.txt

  password ******

  --all-databases option was added in MySQL 3.23.12 or later.

  This method can be used to implement a backup policy database.

  The copy of the database and the data table 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 dump.txt file:

  $ 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 you 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 -p database_name \

  | mysql -h other-host.com database_name

  Pipe used in the above command to import the exported data to the specified remote host.

  This article reprinted from: w3cschool

  (Editor: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/pengpeng1208/p/10930411.html