May 15, 2021 MySQL
You can use SELECT in MySQL... I NTO OUTFILE statement to simply export data to a text file.
In the following example, we export w3cschool_tbl data from the data table to the /tmp/tutorials .txt file:
mysql> SELECT * FROM tutorials_tbl -> INTO OUTFILE '/tmp/tutorials.txt';
You can use the command options to format the specified data output, as in the following instance, the export 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, a file is generated, with 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;
Mysqldump is MySQL's utility for restorting databases. It mainly produces an SQL script that contains the command CREATE TABLE INSERT, etc. necessary to recreate the database from the beginning.
Exporting data using mysqldump requires the --tab option to specify the directory specified by the export file, which must be writeable.
The following examples export data tutorials_tbl to the /tmp directory:
$ mysqldump -u root -p --no-create-info \ --tab=/tmp W3CSCHOOL w3cschool_tbl password ******
Export SQL format data to the specified file as follows:
$ mysqldump -u root -p W3CSCHOOL w3cschool_tbl > dump.txt password ******
The above command creates the following file:
-- MySQL dump 8.23 -- -- Host: localhost Database: W3CSCHOOL --------------------------------------------------------- -- Server version 3.23.58 -- -- Table structure for table `w3cschool_tbl` -- CREATE TABLE w3cschool_tbl ( w3cschool_id int(11) NOT NULL auto_increment, w3cschool_title varchar(100) NOT NULL default '', w3cschool_author varchar(40) NOT NULL default '', submission_date date default NULL, PRIMARY KEY (w3cschool_id), UNIQUE KEY AUTHOR_INDEX (w3cschool_author) ) TYPE=MyISAM; -- -- Dumping data for table `w3cschool_tbl` -- INSERT INTO w3cschool_tbl VALUES (1,'Learn PHP','John Poul','2007-05-24'); INSERT INTO w3cschool_tbl VALUES (2,'Learn MySQL','Abdul S','2007-05-24'); INSERT INTO w3cschool_tbl VALUES (3,'JAVA Tutorial','Sanjay','2007-05-06');
If you need to export data from the entire database, you can use the following commands:
$ mysqldump -u root -p W3CSCHOOL > database_dump.txt password ******
If you need to back up all your databases, you can use the following commands:
$ mysqldump -u root -p --all-databases > database_dump.txt password ******
The --all-databases option is added in MySQL 3.23.12 and later.
This method can be used to implement a backup strategy for the database.
If you need to copy data to another MySQL server, you can specify the database name and data table in the mysqldump command.
Perform the following command on the source host to back up the data to the dump .txt file:
$ mysqldump -u root -p database_name table_name > dump.txt password *****
If you fully back up the database, you do not need to use a specific table name.
If you need to import a backed-up database into a MySQL server, you can use the following command, which you need to verify that the database has been created:
$ mysql -u root -p database_name < dump.txt password *****
你也可以使用以下命令将导出的数据直接导入到远程的服务器上,但请确保两台服务器是相通的,是可以相互访问的:</p> $ mysqldump -u root -p database_name \ | mysql -h other-host.com database_name
The above command uses a pipeline to import exported data to a specified remote host.