{"id":212,"date":"2011-07-05T05:59:13","date_gmt":"2011-07-05T05:59:13","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=212"},"modified":"2012-12-25T22:39:51","modified_gmt":"2012-12-25T22:39:51","slug":"mysqldump","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/mysqldump\/","title":{"rendered":"mysqldump, tips and tricks"},"content":{"rendered":"<p>I want to backup a mysql database. The easiest approach is using <em>mysqldump<\/em> with its default options, i.e.,<\/p>\n<pre class=\"sh\">\r\n# mysqldump -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> <em>db<\/em> &gt; backup.sql\r\n<\/pre>\n<p>This will dump the full DDL and DML needed to re-create the database. Table locking is enabled by default, although for InnoDB it is recommended to use the <em>&#8211;single-transaction<\/em> option so that the entire backup will be contained in one transaction (the default option locks each table prior to backing up).<\/p>\n<p>You can also backup specific tables, i.e.,<\/p>\n<pre class=\"sh\">\r\n# mysqldump -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> <em>db<\/em> <em>table<\/em> &gt; backup.sql\r\n<\/pre>\n<p>Since <em>mysqldump<\/em> outputs to stdout, you can pipe the output to a separate mysql server, in this case a dedicated backup database:<\/p>\n<pre class=\"sh\">\r\n# mysqldump -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> <em>db<\/em> | mysql -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> -C <em>backup-db<\/em>\r\n<\/pre>\n<p>If you have a very large database you can use gzip to compress, i.e.,<\/p>\n<pre class=\"sh\">\r\n# mysqldump -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> <em>db<\/em> | gzip -9 -c &gt; backup.sql.gz\r\n<\/pre>\n<h2>Export<\/h2>\n<p>I would like to export data from mysql into a spreadsheet application. Most spreadsheet applications can read CSV (comma-separated-value) files and <em>mysqldump<\/em> can be used to create CSV output of your tables.<\/p>\n<pre class=\"sh\">\r\n# mysqldump -u <em>user<\/em> -p<em>pass<\/em> \\\r\n  -T \/tmp --fields-enclosed-by=\\\" --fields-terminated-by=, <em>db<\/em>\r\n<\/pre>\n<p>This approach assumes you are running <em>mysqldump<\/em> on the database server, it leverages the OUTFILE functionality, and (for each table) is equivalent to the following sql:<\/p>\n<pre class=\"sh_sql\">\r\nmysql> SELECT *\r\n    -> INTO OUTFILE '\/tmp\/foobar_users.csv'\r\n    -> FIELDS TERMINATED BY ','\r\n    -> ENCLOSED BY '\"'\r\n    -> ESCAPED BY '\\\\'\r\n    -> LINES TERMINATED BY '\\n'\r\n    -> FROM foobar_users;\r\n<\/pre>\n<p>If you do not have access to the database servers filesystem (or do not want to touch it), you can use some shell magic to convert mysql output into csv, e.g.,<\/p>\n<pre class=\"sh\">\r\n# mysql -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> -ss \\\r\n  -e \"SELECT * FROM foobar_users\" <em>db<\/em> \\\r\n  | sed 's\/\\t\/\",\"\/g;s\/^\/\"\/;s\/$\/\"\/' &gt; foobar_users.csv\r\n<\/pre>\n<p>The sed command is switching unescaped tabs to &#8220;,&#8221; (including the double quotes), and adding a double quote to the beginning and end of the line.<\/p>\n<h2>Restore<\/h2>\n<p>I would like to restore a mysql database from a previous export.<\/p>\n<p>The output from <em>mysqldump<\/em> with default options can be sent directly into mysql as follows:<\/p>\n<pre class=\"sh\">\r\n# mysql -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> -C <em>db<\/em> &lt; backup.sql\r\n<\/pre>\n<p>If you have a compressed output file and lack the disk space to uncompress, you can use zcat and pipe the uncompressed backup into mysql, e.g.,<\/p>\n<pre class=\"sh\">\r\n# zcat backup.sql.gz | mysql -u <em>user<\/em> -h <em>host<\/em> -p<em>pass<\/em> -C <em>db<\/em>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I want to backup a mysql database. The easiest approach is using mysqldump with its default options, i.e., # mysqldump -u user -h host -ppass db &gt; backup.sql This will dump the full DDL and DML needed to re-create the database. Table locking is enabled by default, although for InnoDB it is recommended to use [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,14],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/212"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":10,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/212\/revisions\/724"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}