Mysqldump and its command usage

Source: Internet
Author: User
Tags sql error import database

There are many MySQL database backup tools, but here we will introduce a small and easy-to-use mysqldump tool, which is located in the database directory bin/mysqldump.exe. in the past few days, when phpMyAdmin is used to back up the database, garbled code occurs, and the database is repeatedly tested in the local and remote dreamhost space. However, garbled code occurs when exporting the database. It should be a problem with phpMyAdmin, there is no problem with the database itself. let's go, huh, huh. the usage of mysqldump on Google.
If the host supports shell, you can log on to the host through SSH and execute the following commands to back up and restore the host. This is suitable for space such as dreamhost and bluehost.

Export Database:
Mysqldump-H hostname-uusername-P dbname> exported_db. SQL

Import database:
Mysql-H hostname-uusername-P dbname <exported_db. SQL

Hostname is the Database Host Name, and username and dbname are the login ID and database name respectively. The exported data inventory is placed in the exported_db. SQL file. Chocolate Factory (beta5): import of the WordPress ultra-large database.

Use mysqldump:
(The mysqldump command is located in the MySQL/bin/directory) // The command can only be used if it is dedicated to the MySQL/bin/directory. The command window running in cmd cannot be used, used in the MySQL/bin/directory where the database is located.

Basic usage: shell> mysqldump [Options] database [Tables]

If you do not specify any tables, the entire database will be exported.

Run mysqldump-help to obtain the option table supported by your mysqldump version.

Note: If you run mysqldump without the-quick or-opt option, mysqldump loads the entire result set to the memory before the export result. If you are exporting a large database, this may be a problem.

Several common use cases:

1. Export the entire database
Mysqldump-u username-P Database Name> exported file name
Mysqldump-u root-P dataname> dataname. SQL
At this time, you will be prompted to enter the password of the root user name. after entering the password, the dataname database will be successfully backed up in the MySQL/bin/directory.

2. Export a table
Mysqldump-u user name-P database name Table Name> exported file name
Mysqldump-u root-P dataname users> dataname_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 a drop table before each create statement

4. Import the database
Common source commands
Go to the MySQL Database Console,
For example, MySQL-u root-P

Mysql> Use Database

Then run the source command. The following parameter is the script file (for example,. SQL used here)
Mysql> source D:/wcnc_db. SQL

Mysqldump supports the following options:
-Add-locks
Add lock tables and unlock table before each table is exported. (To make it faster to insert data to MySQL ).

-Add-drop-table
Add a drop table before each create statement.

-Allow-keywords
Names of columns allowed to be created as keywords. This is done by the table name prefix on each column name.

-C,-complete-insert
Use the complete insert Statement (with the column name ).

-C,-compress
If both the client and server support compression, all information is compressed between the two.

-Delayed
Use the insert delayed command to insert rows.

-E,-extended-insert
Use the new multiline insert syntax. (A more compact and faster insert statement is provided)

-#,-Debug [= option_string]
TrackingProgramFor debugging ).

-Help
Displays a help message and exits.

-Fields-terminated-by =...

-Fields-enclosed-by =...

-Fields-optionally-enclosed-by =...

-Fields-escaped-by =...

-Fields-terminated-by =...

These options are used with-T options and have the same meaning as the load data infile clause.
Load data infile syntax.

-F,-flush-logs
Wash Out the log files on the MySQL server before starting the export.

-F,-force,
Even if we get an SQL error while exporting a table, continue.

-H,-host = ..
Export data from the MySQL server on the named host. The default host is localhost.

-L,-lock-tables.
Lock all tables for start export.

-T,-no-create-Info
Create table statement)

-D,-no-Data
No row information is written to the table. If you only want to export the structure of a table, this is very useful!

-OPT
Same as-quick-add-drop-table-add-locks-extended-insert-lock-tables.
You should be given the fastest export possible for reading a MySQL server.

-Pyour_pass,-Password [= your_pass]
The password used to connect to the server. If you do not specify "= your_pass", mysqldump requires a password from the terminal.

-P port_num,-Port = port_num
The TCP/IP Port used to connect to a host. (This is used to connect to a host other than localhost because it uses UNIX sockets .)

-Q,-quick
Directly export the data to stdout without buffering the query; Use mysql_use_result () to do it.

-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 a table_name. SQL file that contains the SQL CREATE command and a table_name.txt file that contains data. Note: This only works when mysqldump runs on the same machine where the mysqld daemon is running .. The format of the TXT file is determined by the-fields-xxx and-lines-xxx options.

-U user_name,-user = user_name
The username used by MySQL to connect to the server. The default value is your UNIX login name.

-O Var = option,-set-variable VAR = Option
Set the value of a variable. Possible variables are listed below.

-V,-verbose
Lengthy mode. Print out more information about the program.

-V,-version
Print the version information and exit.

-W,-where = 'where-condition'
Only selected records are exported. Note that the quotation marks are mandatory!
"-Where = user = 'jimf '" "-wuserid> 1"-wuserid <1 ″

The most common use of mysqldump may make a backup of the entire database:
Mysqldump-opt database> backup-file. SQL

However, it is also useful for enriching another MySQL database with information from one database:
Mysqldump-opt database | mysql-host = remote-host-C Database
Mysqldump exports complete SQL statements, so it is easy to import data into MYSQL client programs:

Shell> mysqladmin create target_db_name
Shell> MySQL target_db_name <backup-file. SQL
Yes
Shell> MySQL database name <file name

 

[From] http://blog.csdn.net/yangyu112654374/article/details/4674478

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.