MySQL - Database Export MYSQL
- MySQL Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- Drop MySQL Database
- Selecting MySQL Database
- MySQL - Data Types
- Create MySQL Tables
- Drop MySQL Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - WHERE Clause
- MySQL - UPDATE Query
- MySQL - DELETE Query
- MySQL - LIKE Clause
- MySQL - Sorting Results
- Using MySQl Joins
- MySQL - Regexps
- MySQL - Transactions
- MySQL - ALTER Command
- MySQL - INDEXES
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- Using MySQL Sequences
- MySQL - Handling Duplicates
- MySQL - and SQL Injection
- MySQL - Database Export
MySQL - Database Export
Exporting Data with the SELECT ... INTO OUTFILE Statement
The syntax for this statement combines a regular SELECT command with INTO OUTFILE filename at the end. The default output format is the same as it is for the LOAD DATA command. So, the following statement exports the tutorials_tbl table into /tmp/tutorials.txt as a tab-delimited, linefeed-terminated file.
mysql> SELECT * FROM tutorials_tbl -> INTO OUTFILE '/tmp/tutorials.txt';
You can change the output format using various options to indicate how to quote and delimit columns and records. To export the tutorial_tbl table in a CSV format with CRLF-terminated lines, use the following code.
mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/tutorials.txt' -> FIELDS TERMINATED BY ',' ENCLOSED BY '"' -> LINES TERMINATED BY '\r\n';
The SELECT ... INTO OUTFILE has the following properties −
-
The output file is created directly by the MySQL server, so the filename should indicate where you want the file to be written on the server host. There is no LOCAL version of the statement analogous to the LOCAL version of LOAD DATA.
-
You must have the MySQL FILE privilege to execute the SELECT ... INTO statement.
-
The output file must not already exist. This prevents MySQL from clobbering files that may be important.
-
You should have a login account on the server host or some way to retrieve the file from that host. Otherwise, the SELECT ... INTO OUTFILE command will most likely be of no value to you.
-
Under UNIX, the file is created world-readable and is owned by the MySQL server. This means that although you will be able to read the file, you may not be able to delete it.
Exporting Tables as Raw Data
The mysqldump program is used to copy or back up tables and databases. It can write the table output either as a Raw Datafile or as a set of INSERT statements that recreate the records in the table.
To dump a table as a datafile, you must specify a --tab option that indicates the directory, where you want the MySQL server to write the file.
For example, to dump the tutorials_tbl table from the TUTORIALS database to a file in the /tmp directory, use a command as shown below.
$ mysqldump -u root -p --no-create-info \ --tab=/tmp tutorials tutorials_tbl password ******
Exporting Table Contents or Definitions in SQL Format
To export a table in SQL format to a file, use the command shown below.
$ mysqldump -u root -p TUTORIALS tutorials_tbl > dump.txt password ******
This will a create file having content as shown below.
-- 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');
To dump multiple tables, name them all followed by the database name argument. To dump an entire database, don't name any tables after the database as shown in the following code block.
$ mysqldump -u root -p TUTORIALS > database_dump.txt password ******
To back up all the databases available on your host, use the following code.
$ mysqldump -u root -p --all-databases > database_dump.txt password ******
The --all-databases option is available in the MySQL 3.23.12 version. This method can be used to implement a database backup strategy.
Copying Tables or Databases to Another Host
If you want to copy tables or databases from one MySQL server to another, then use the mysqldump with database name and table name.
Run the following command at the source host. This will dump the complete database into dump.txt file.
$ mysqldump -u root -p database_name table_name > dump.txt password *****
You can copy complete database without using a particular table name as explained above.
Now, ftp dump.txt file on another host and use the following command. Before running this command, make sure you have created database_name on destination server.
$ mysql -u root -p database_name < dump.txt password *****
Another way to accomplish this without using an intermediary file is to send the output of the mysqldump directly over the network to the remote MySQL server. If you can connect to both the servers from the host where the source database resides, use the following command (Make sure you have access on both the servers).
$ mysqldump -u root -p database_name \ | mysql -h other-host.com database_name
In mysqldump, half of the command connects to the local server and writes the dump output to the pipe. The remaining half of the command connects to the remote MySQL server on the other-host.com. It reads the pipe for input and sends each statement to the other-host.com server.
MySQL - Database Import - Recovery Methods
There are two simple ways in MySQL to load data into the MySQL database from a previously backed up file.
Importing Data with LOAD DATA
MySQL provides a LOAD DATA statement that acts as a bulk data loader. Here is an example statement that reads a file dump.txt from your current directory and loads it into the table mytbl in the current database.
mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl;
-
If the LOCAL keyword is not present, MySQL looks for the datafile on the server host using the looking into absolute pathname, which fully specifies the location of the file, beginning from the root of the filesystem. MySQL reads the file from the given location.
-
By default, LOAD DATA assumes that datafiles contain lines that are terminated by linefeeds (newlines) and that data values within a line are separated by tabs.
-
To specify a file format explicitly, use a FIELDS clause to describe the characteristics of fields within a line, and a LINES clause to specify the line-ending sequence. The following LOAD DATA statement specifies that the datafile contains values separated by colons and lines terminated by carriage returns and new line character.
mysql> LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl -> FIELDS TERMINATED BY ':' -> LINES TERMINATED BY '\r\n';
-
The LOAD DATA command assumes the columns in the datafile have the same order as the columns in the table. If that is not true, you can specify a list to indicate which table columns the datafile columns should be loaded into. Suppose your table has columns a, b, and c, but successive columns in the datafile correspond to columns b, c, and a.
You can load the file as shown in the following code block.
mysql> LOAD DATA LOCAL INFILE 'dump.txt' -> INTO TABLE mytbl (b, c, a);
Importing Data with mysqlimport
MySQL also includes a utility program named mysqlimport that acts as a wrapper around LOAD DATA, so that you can load the input files directly from the command line.
To load data from the dump.txt into mytbl, use the following command at the UNIX prompt.
$ mysqlimport -u root -p --local database_name dump.txt password *****
If you use mysqlimport, command-line options provide the format specifiers. The mysqlimport commands that correspond to the preceding two LOAD DATA statements looks as shown in the following code block.
$ mysqlimport -u root -p --local --fields-terminated-by = ":" \ --lines-terminated-by = "\r\n" database_name dump.txt password *****
The order in which you specify the options doesn't matter for mysqlimport, except that they should all precede the database name.
The mysqlimport statement uses the --columns option to specify the column order −
$ mysqlimport -u root -p --local --columns=b,c,a \ database_name dump.txt password *****
Handling Quotes and Special Characters
The FIELDS clause can specify other format options besides TERMINATED BY. By default, LOAD DATA assumes that values are unquoted and interprets the backslash (\) as an escape character for the special characters. To indicate the value quoting character explicitly, use the ENCLOSED BY command. MySQL will strip that character from the ends of data values during input processing. To change the default escape character, use ESCAPED BY.
When you specify ENCLOSED BY to indicate that quote characters should be stripped from data values, it is possible to include the quote character literally within data values by doubling it or by preceding it with the escape character.
For example, if the quote and escape characters are " and \, the input value "a""b\"c" will be interpreted as a"b"c.
For mysqlimport, the corresponding command-line options for specifying quote and escape values are --fields-enclosed-by and --fields-escaped-by.