YouTip LogoYouTip

Mysql Database Export

In MySQL, you can use the **SELECT...INTO OUTFILE** statement to simply export data to a text file. * * * ## Exporting Data Using the SELECT ... INTO OUTFILE Statement SELECT...INTO OUTFILE is the syntax in MySQL used to export query results to a file. SELECT...INTO OUTFILE allows you to write the results of a query to a text file. The basic usage is: SELECT column1, column2, ... INTO OUTFILE 'file_path' FROM your_table WHERE your_conditions; **Parameter Description:** * `column1, column2, ...`: The columns to select. * `'file_path'`: Specifies the path and name of the output file. * `your_table`: The table to query. * `your_conditions`: The query conditions. Here is a simple example: ## Example SELECT id, name, email INTO OUTFILE '/tmp/user_data.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY 'n' FROM users; In the SQL statement above, we selected the id, name, and email columns from the users table and wrote the results to the /tmp/user_data.csv file. FIELDS TERMINATED BY ',' specifies the delimiter between columns (a comma), and LINES TERMINATED BY 'n' specifies the delimiter between lines (a newline character). It is important to note that executing SELECT...INTO OUTFILE requires the appropriate permissions, and the output file's directory must be writable by the MySQL server. In the following example, we export the data from the tutorial_tbl table to the /tmp/.txt file: mysql> SELECT * FROM tutorial_tbl -> INTO OUTFILE '/tmp/.txt'; You can set the specific format of the data output through command options. The following example exports in CSV format: mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/.txt' -> FIELDS TERMINATED BY ',' ENCLOSED BY '"' -> LINES TERMINATED BY 'rn'; In the example below, a file is generated with 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; ### The SELECT ... INTO OUTFILE statement has the following properties: * LOAD DATA INFILE is the inverse operation of SELECT ... INTO OUTFILE, using SELECT syntax. To write data from a database to a file, use SELECT ... INTO OUTFILE. To read the file back into the database, use LOAD DATA INFILE. * A SELECT of the form SELECT...INTO OUTFILE 'file_name' writes the selected rows into a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. * The output cannot be an existing file. This prevents file data from being tampered with. * You need an account that can log into the server to retrieve the file. Otherwise, SELECT ... INTO OUTFILE will not work. * In UNIX, the file is created readable and owned by the MySQL server. This means that although you can read the file, you might not be able to delete it. * * * ## Exporting Tables as Raw Data with mysqldump mysqldump is a command-line tool provided by MySQL for backing up and exporting databases. mysqldump is a utility for dumping databases for mysql. It primarily produces a SQL script containing the necessary commands to recreate the database from scratch, such as CREATE TABLE, INSERT, etc. To export data using mysqldump, you need to use the --tab option to specify the directory for the export files. This destination must be writable. The basic usage of mysqldump is: mysqldump -u username -p password -h hostname database_name > output_file.sql **Parameter Description:** * `-u`: Specifies the MySQL username. * `-p`: Prompts for the password. * `-h`: Specifies the MySQL hostname. * `database_name`: The name of the database to export. * `output_file.sql`: The file to save the exported data to. The following example exports the tutorial_tbl table to the /tmp directory: $ mysqldump -u root -p --no-create-info --tab=/tmp tutorial_tbl password ****** ### mysqldump Examples **1. Export an Entire Database** Export the mydatabase database to the mydatabase_backup.sql file: mysqldump -u root -p mydatabase > mydatabase_backup.sql **2. Export Specific Tables** If you only want to export a specific table from the database, you can use the following command: mysqldump -u username -p password -h hostname database_name table_name > output_file.sql Or: mysqldump -u root -p mydatabase mytable > mytable_backup.sql **3. Export Database Structure** If you only want to export the database structure without the data, you can use the --no-data option: mysqldump -u username -p password -h hostname --no-data database_name > output_file.sql **4. Export a Compressed File** You can compress the exported data to reduce file size. For example, using gzip: mysqldump -u username -p password -h hostname database_name | gzip > output_file.sql.gz ### Exporting Data in SQL Format Export data in SQL format to a specified file, as shown below: $ mysqldump -u root -p tutorial_tbl > dump.txt password ****** The content of the file created by the above command is as follows: -- MySQL dump 8.23---- Host: localhost Database: ----------------------------------------------------------- Server version 3.23.58---- Table structure for table `tutorial_tbl`-- CREATE TABLE tutorial_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 `tutorial_tbl`-- INSERT INTO tutorial_tbl VALUES (1,'Learn PHP','John Poul','2007-05-24'); INSERT INTO tutorial_tbl VALUES (2,'Learn MySQL','Abdul S','2007-05-24'); INSERT INTO tutorial_tbl VALUES (3,'JAVA Tutorial','Sanjay','2007-05-06'); If you need to export the data of the entire database, you can use the following command: $ mysqldump -u root -p > database_dump.txt password ****** If you need to back up all databases, you can use the following command: $ mysqldump -u root -p --all-databases > database_dump.txt password ****** The --all-databases option was added in MySQL version 3.23.12 and later. This method can be used to implement a database backup strategy. * * * ## Copying Tables and Databases to Another Host If you need to copy data to another MySQL server, you can specify the database name and table name in the mysqldump command. Execute 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 are backing up the entire database, you do not need to specify a particular table name. If you need to import the backed-up database into a MySQL server, you can use the following command. You need to ensure the database has already been created: $ mysql -u root -p database_name < dump.txt password ***** You can also use the following command to import the exported data directly to a remote server. Please ensure that the two servers are connected and can access each other: $ mysqldump -u root -p database_name | mysql -h other-host.com database_name The above command uses a pipe to import the exported data to the specified remote host.
← Mysql Database ImportJquery Plugin Autocomplete β†’