{"id":7173,"date":"2018-01-23T12:11:48","date_gmt":"2018-01-23T04:11:48","guid":{"rendered":"http:\/\/rmohan.com\/?p=7173"},"modified":"2018-01-23T12:11:48","modified_gmt":"2018-01-23T04:11:48","slug":"mysql-backup-command-mysqldump-parameters-and-examples","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7173","title":{"rendered":"MySQL backup command mysqldump parameters and examples"},"content":{"rendered":"<p>MySQL backup command mysqldump parameters and examples<\/p>\n<p>1. Grammar options<br \/>\n-h, &#8211;host=name<br \/>\nCPU name<\/p>\n<p>-P[ port_num], &#8211;port=port_num<br \/>\nThe TCP \/ IP port number used to connect to the MySQL server<\/p>\n<p>&#8211;master-data<br \/>\nThis option adds the location and filename of the binlog to the output. If it is equal to 1, it will be printed as a CHANGE MASTERcommand; if equal to 2, the comment prefix will be added. And this option will automatically open &#8211;lock-all-tables, unless at the same time set up &#8211;single-transaction(in this case, the global read lock will only add a small amount of time to start the dump, do not forget to read &#8211;single-transactionthe part). In all cases, the actions in all logs occur at the exact moment of export. This option will automatically shut down &#8211;lock-tables.<\/p>\n<p>-x, &#8211;lock-all-tables<br \/>\nLock all the tables in all the libraries. This is achieved by holding a global read lock throughout the dump. Will automatically shut down &#8211;single-transactionand &#8211;lock-tables.<\/p>\n<p>&#8211;single-transaction<br \/>\nThe exported data is a consistent snapshot by encapsulating the export operation within a single transaction. Works only if the table uses a storage engine that supports MVCC (currently only InnoDB); other engines can not guarantee that the export is consistent. When the export &#8211;single-transactionoption is enabled , to make sure that the export file is valid (the correct table data and binary log location), make sure that no other connections execute the following statement: ALTER TABLE, DROP TABLE, RENAME TABLE, TRUNCATE TABLEThis will invalidate the consistent snapshot. This option will automatically shut down when turned on &#8211;lock-tables.<\/p>\n<p>-l, &#8211;lock-tables<br \/>\nRead lock on all tables. (Default is open, use &#8211;skip-lock-tablesto close the above options will shut down -loption)<\/p>\n<p>-F, &#8211;flush-logs<br \/>\nRefresh the server&#8217;s log file before starting export. Note that if you export many databases at a time (use -databases=or &#8211;all-databasesoption), log refreshes when each library is exported. The exception is when using &#8211;lock-all-tablesor &#8211;master-data: The log will only be refreshed once, at that time all tables will be locked. So if you want your exports and log refreshes to happen at exactly the same time, you need to use &#8211;lock-all-tablesor &#8211;master-datacooperate &#8211;flush-logs.<\/p>\n<p>&#8211;delete-master-logs<br \/>\nAfter the backup is complete delete the log on the main library. This option automatically opens &#8220; &#8211;master-data`.<\/p>\n<p>&#8211;opt<br \/>\nWith -add-drop-table, &#8211;add-locks, &#8211;create-options, &#8211;quick, &#8211;extended-insert, &#8211;lock-tables, &#8211;set-charset, &#8211;disable-keys. ( &#8211;skip-optWhich is turned on by default and off means that these options keep their defaults) should give you the quickest possible export for reading into a MySQL server, &#8211;compactalmost banning the options above.<\/p>\n<p>-q, &#8211;quick<br \/>\nDo not buffer the query, directly exported to stdout. (Turned on by default, use &#8211;skip-quickto close) This option is used for dumping large tables.<\/p>\n<p>&#8211;set-charset<br \/>\nWill be SET NAMES default_character_setadded to the output. This option is enabled by default. To disable the SET NAMESstatement, use &#8211;skip-set-charset.<\/p>\n<p>&#8211;add-drop-tables<br \/>\nCREATE TABLEAdd a DROP TABLEstatement before each statement. Open by default.<\/p>\n<p>&#8211;add-locks<br \/>\nAdded before LOCK TABLESand after each table export UNLOCK TABLE. (In order to make it faster to insert into MySQL). Open by default.<\/p>\n<p>&#8211;create-option<br \/>\nInclude all MySQL table options in the CREATE TABLE statement. Open by default, use &#8211;skip-create-optionsto close.<\/p>\n<p>-e, &#8211;extended-insert<br \/>\nUsing the new multi-line INSERT syntax, turned on by default (gives tighter and faster insert statements)<\/p>\n<p>-d, &#8211;no-data<br \/>\nAny row information not written to the table. This is useful if you only want to export the structure of a table.<\/p>\n<p>&#8211;add-drop-database<br \/>\nBefore the create database DROP DATABASE, the default off, so generally need to ensure that the database has been imported.<\/p>\n<p>&#8211;default-character-set=<br \/>\nThe default character set to use. If not specified, mysqldump uses utf8.<\/p>\n<p>-B, &#8211;databases<br \/>\nDump several databases. Usually, mysqldump treats the first name parameter in the command line as the database name, followed by the name as the table name. With this option, it treats all name parameters as database names. CREATE DATABASE IF NOT EXISTS db_nameAnd the USE db_namestatement is included in the output in front of each new database.<\/p>\n<p>&#8211;tables<br \/>\nOverlay &#8211;databaseoptions. All arguments after the option are treated as table names.<\/p>\n<p>-u[ name], &#8211;user=<br \/>\nMySQL user name to use when connecting to the server.<\/p>\n<p>-p[password], &#8211;password[=password]<br \/>\nThe password to use when connecting to the server. If you use short form (-p), you can not have a space between the option and the password. If on the command line, the password value behind &#8211;passwordor -poption is ignored , you will be prompted for one.<\/p>\n<p>Example<br \/>\nExport a database:<\/p>\n<p>$ mysqldump -h localhost -uroot -ppassword \\ &#8211; master-data = 2 &#8211;single-transaction &#8211;add-drop-table &#8211;create-options &#8211;quick \\ &#8211; extended-insert &#8211;default-character-set = utf8 \\ &#8211; databases discuz&gt; backup-file.sql<br \/>\nExport a table:<\/p>\n<p>$ mysqldump -u pak -p &#8211;opt &#8211;flush-logs pak t_user&gt; pak-t_user.sql<br \/>\nCompress backup files:<\/p>\n<p>$ mysqldump -hhostname -uusername -ppassword &#8211;databases dbname | gzip&gt; backup-file.sql.gz<br \/>\nThe corresponding reduction action is<br \/>\ngunzip &lt;backup-file.sql.gz | mysql -uusername -ppassword dbname<br \/>\nImport the database:<\/p>\n<p>mysql&gt; use target_dbname<br \/>\nmysql&gt; source \/mysql\/backup\/path\/backup-file.sql<br \/>\nor<br \/>\n$ mysql target_dbname &lt;backup-file.sql<br \/>\nImport there is a mysqlimportcommand, yet to study.<\/p>\n<p>Dump directly from one database to another:<\/p>\n<p>mysqldump -u username -p &#8211;opt dbname | mysql &#8211;host remote_host -C dbname2<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL backup command mysqldump parameters and examples<\/p>\n<p>1. Grammar options -h, &#8211;host=name CPU name<\/p>\n<p>-P[ port_num], &#8211;port=port_num The TCP \/ IP port number used to connect to the MySQL server<\/p>\n<p>&#8211;master-data This option adds the location and filename of the binlog to the output. If it is equal to 1, it will be printed as [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7173"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7173"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7173\/revisions"}],"predecessor-version":[{"id":7174,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7173\/revisions\/7174"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}