Resolve the basic usage of the mysqldump tool in MySQL
- 2020-05-19 06:05:28
- OfStack
The mysqldump tool of MySQL is used for exporting. The basic usage is:
shell
>
mysqldump [OPTIONS] database [tables]
If you do not give any tables, the entire database will be exported.
By executing mysqldump --help, you can get the list of options that your version of mysqldump supports.
Note that if you run mysqldump without the --quick or --opt option, mysqldump will load the entire result set into memory before exporting the result, which could be a problem if you are exporting a large database.
mysqldump supports the following options:
--add-locks
Add LOCK TABLES before each table export and UNLOCK TABLE after. (for faster insertion into MySQL).
--add-drop-table
Add 1 drop table before each create statement.
--allow-keywords
Allows you to create column names that are keywords. This is done by prefixing the table name with each column name.
-c, --complete-insert
Use the full insert statement (with the column name).
-C, --compress
If both the client and the server support compression, compress all the information between them.
--delayed
Insert the line with the INSERT DELAYED command.
-e, --extended-insert
Use the new multi-line INSERT syntax. (gives a tighter and faster insert statement)
-#, --debug[=option_string]
Trace application usage (for debugging purposes).
--help
Displays a help message and exits.
--fields-terminated-by=...
--fields-enclosed-by=...
--fields-optionally-enclosed-by=...
--fields-escaped-by=...
--fields-terminated-by=...
These choices are used as the -T choice 1 and have the same meaning as the corresponding LOAD DATA INFILE clause.
LOAD DATA INFILE syntax.
-F, --flush-logs
Before starting the export, wash the log files from the MySQL server.
-f, --force,
Even if we get an SQL error during a table export, continue.
-h, --host=..
Export data from the MySQL server on the named host. The default host is localhost.
-l, --lock-tables.
Locks all tables for the start export.
-t, --no-create-info
Do not write table creation information (CREATE TABLE statement)
-d, --no-data
No row information is written to the table. This is useful if you only want to get 1 table structure exported!
--opt
Same --quick --add -- drop -- table --add -- locks --extended -- insert --lock -- tables.
Should give you the fastest possible export for reading into an MySQL server.
-pyour_pass, --password[=your_pass]
The password used to connect to the server. If you do not specify the "=your_pass" section, mysqldump requires a password from the terminal.
-P port_num, --port=port_num
The TCP/IP port number used when connecting to a host. (this is used to connect to a host other than localhost because it USES Unix sockets.)
-q, --quick
Unbuffered query, directly exported to stdout; Do it with mysql_use_result().
-S /path/to/socket, --socket=/path/to/socket
The socket file used when connecting to localhost, which is the default host.
-T, --tab=path-to-some-directory
For each given table, create one table_name.sql file, which contains the SQL CREATE command, and one table_name.txt file, which contains the data. Note: this only works if mysqldump is running on the same machine that the mysqld daemon is running on. The format of the.txt file is based on the -- fields-xxx and --lines--xxx options.
-u user_name, --user=user_name
The user name MySQL USES when connecting to the server. The default value is your Unix login name.
-O var=option, -- set-variable var=option sets the value of one variable. Possible variables are listed below.
-v, --verbose
Verbose mode. Print out more information about what the program does.
-V, --version
Print version information and exit.
-w, --where='where-condition'
Only the selected records are exported; Note that quotation marks are mandatory!
"--where=user='jimf'" "-wuserid
>
1" "-wuserid
<
1"
The most common mysqldump USES the possibility of making 1 backup of the entire database:
mysqldump --opt database
>
backup-file.sql
But it is also useful for enriching another MySQL database with information from one database:
mysqldump --opt database | mysql --host=remote-host -C database
Since mysqldump exports the full SQL statement, it is easy to import data using the mysql client:
shell
>
mysqladmin create target_db_name
shell
>
mysql target_db_name
<
backup-file.sql
is
shell
>
mysql library name
<
The file name
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Several common use cases:
Export the entire database
mysqldump-u username -p database name
>
Exported file name
mysqldump -u wcnc -p smgp_apps_wcnc
>
wcnc.sql
Export 1 table
mysqldump-u user name -p database name table name
>
Exported file name
mysqldump -u wcnc -p smgp_apps_wcnc users
>
wcnc_users.sql
3. Export a database structure
mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc
>
d:\wcnc_db.sql
-d no data -- add-drop-table add 1 drop table before each create statement
4. Import database
Use the source command
Go to the mysql database console,
For example, mysql-u root-p
mysql
>
use database
Then use the source command, followed by the script file (as used here.sql)
mysql
>
source d:\wcnc_db.sql