{"id":2274,"date":"2013-07-12T15:29:22","date_gmt":"2013-07-12T07:29:22","guid":{"rendered":"http:\/\/rmohan.com\/?p=2274"},"modified":"2013-07-12T15:30:39","modified_gmt":"2013-07-12T07:30:39","slug":"how-to-take-mysql-dump","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2274","title":{"rendered":"How to take Mysql Dump"},"content":{"rendered":"<div>We can just check how to back up and restore mysql database and tables<\/div>\n<p>&nbsp;<\/p>\n<div>if we want the backup of entire mysql db<\/div>\n<div># mysqldump -u root -p -A &gt; \/bkp\/mysql_full.sql<\/div>\n<div>-A &#8212;&#8212; take all(full)db from \/var\/lib\/mysql<\/div>\n<div>-p &#8212;&#8212; password<\/div>\n<div>-u &#8212;&#8212; user<\/div>\n<p>&nbsp;<\/p>\n<div># vim mysql_full.sql shows the complete file db entries in it.<\/div>\n<div><\/div>\n<div>If we want the backup of a single db<\/div>\n<div># mysqldump -u root\u00a0 -p dbname &gt; \/bkp\/bkp_dbname.sql<\/div>\n<div>After taking this when we restore the db to another system first create the db with same name there and then restore the backup file to the new db<\/div>\n<p>&nbsp;<\/p>\n<div>Restoring mysql db<\/div>\n<div># mysql -u root -p dbname &lt; \/bkp\/bkp_dbname.sql<\/div>\n<p>&nbsp;<\/p>\n<div>To take full db bkp with date,<\/div>\n<div># mysqldump -uroot -p -A &gt; \/bkp\/fulldb-$(date %d.%m.%y).sql<\/div>\n<p>&nbsp;<\/p>\n<div>Restoring a particular db from full db backup to another system or same system, If we want to create the db in another system first copy the full backup in that system<\/div>\n<p>mysql&gt; create database dbname;\u00a0\u00a0\u00a0&#8212;&#8211; which we want to restore from other system (also from full db backup) and come out from the db, and put the restoration command<\/p>\n<div># mysql -u root -p dbname &lt; full-02.Jun.10.sql<\/div>\n<div>it will restore the db to the new system. Then login to the db and check the tables in the db<\/div>\n<div>mysql&gt; use dbname;<\/div>\n<div>mysql&gt; show tables;<\/div>\n<p>&nbsp;<\/p>\n<div>Mysql dump full db with triggers and routines<\/div>\n<div># mysqldump -u root -p -A &#8211;triggers=TRUE &#8211;routines=TRUE &#8211;add-drop-table &#8211;databases | bzip2 &gt; ALL_DB_`date %Y%m%d`.sql.bz2&#8211;add-drop-table may remove the existing table when you dump the file to another machine which has the same db.<\/p>\n<\/div>\n<div>above command dump all the databases. If we give &#8220;<code>--all-databases<\/code>\u00a0&#8211;databases\u00a0&#8221; parameters we don&#8217;t want to make any db name while restoring the dump or else we have to create the same db before restoring the dump.<\/div>\n<p>&nbsp;<\/p>\n<div>Dump single db with\u00a0 triggers and routines. It will take stored procedures and functions as well<\/div>\n<div>#\u00a0 mysqldump -u root -p &#8211;triggers=TRUE &#8211;routines=TRUE &#8211;add-drop-table &#8211;databases dbname &gt; dbname.sql<\/p>\n<div>\u00a0If you do not name any tables following\u00a0<i><code>db_name<\/code><\/i>\u00a0or if you use the\u00a0<code>--databases<\/code>or\u00a0<code>--all-databases<\/code>\u00a0option, entire databases are dumped.<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div>Taking a mysql table backup and restoring<\/div>\n<div># mysqldump -uroot -p dbname tablename &gt; tablename.sql<\/div>\n<p>&nbsp;<\/p>\n<div>For restoring a data base table<\/div>\n<div># mysql -uroot -p bdname &lt; tablename.sql<\/div>\n<p>&nbsp;<\/p>\n<div>Backup table with triggers and routines<\/div>\n<div># mysqldump -uroot -p -R &#8211;triggers dbname tablename &gt; tablename.sql<\/div>\n<p>&nbsp;<\/p>\n<div>To take only trigger and routines from the db<\/div>\n<div># mysqldump -R -t -n -d\u00a0 &#8211;databases dbname &gt; dbname.sql<\/div>\n<p>&nbsp;<\/p>\n<div>To take db dump without any date<\/div>\n<div># myqldump -u root -p -R -t -n -d &#8211;skip-opt dbname &gt; dbname.sql<\/div>\n<p>&nbsp;<\/p>\n<div>To backup db,either cp files or db from \/var\/lib\/mysql to another bd or other system and give mysql permission and if we run &#8216;show&#8217; we can see all tables in the new db or in the system. Or use mysqldump<b><\/b><br \/>\n<b><br \/>\n<\/b><\/p>\n<div>&#8211;add-drop-database\u00a0 &#8212; Add a DROP DATABASE statement before each CREATE DATABASE statement<\/div>\n<div>&#8211;add-drop-table\u00a0\u00a0\u00a0\u00a0 &#8212; Add a DROP TABLE statement before each CREATE TABLE statement<\/div>\n<div>&#8211;add-locks\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Surround each table dump with LOCK TABLES and UNLOCK TABLES statements<\/div>\n<div>&#8211;all-databases\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Dump all tables in all databases<\/div>\n<div>&#8211;all-tablespaces\u00a0\u00a0\u00a0 &#8212; Adds to a table dump all SQL statements needed to create any tablespaces used by an NDB Cluster table<\/div>\n<div>&#8211;bind-address=ip_address\u00a0&#8212;- Use the specified network interface to connect to the MySQL Server<\/div>\n<div>&#8211;comments\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Add comments to the dump file<\/div>\n<div>&#8211;databases\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Dump several databases<\/div>\n<div>&#8211;debug-info\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Print debugging information, memory and CPU statistics when the program exits<\/div>\n<div>&#8211;delete-master-logs\u00a0&#8212; On a master replication server, delete the binary logs after performing the dump operation<\/div>\n<div>&#8211;dump-date\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;\u00a0 Include dump date as &#8220;Dump completed on&#8221; comment if &#8211;comments is given<\/div>\n<div>&#8211;flush-logs\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Flush the MySQL server log files before starting the dump<\/div>\n<div>&#8211;flush-privileges\u00a0\u00a0 &#8212; Emit a FLUSH PRIVILEGES statement after dumping the mysql database<\/div>\n<div>&#8211;host\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Host to connect to (IP address or hostname)<\/div>\n<div>&#8211;lock-all-tables\u00a0\u00a0\u00a0 &#8212; Lock all tables across all databases<\/div>\n<div>&#8211;no-data\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Do not dump table contents<\/div>\n<div>&#8211;routines\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Dump stored routines (procedures and functions) from the dumped databases<\/div>\n<div>&#8211;skip-add-drop-table\u00a0&#8212; Do not add a DROP TABLE statement before each CREATE TABLE statement<\/div>\n<div>&#8211;skip-triggers\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Do not dump triggers<\/div>\n<div>&#8211;triggers\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212; Dump triggers for each dumped table<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We can just check how to back up and restore mysql database and tables <\/p>\n<p>&nbsp;<\/p>\n<p> if we want the backup of entire mysql db # mysqldump -u root -p -A &gt; \/bkp\/mysql_full.sql -A &#8212;&#8212; take all(full)db from \/var\/lib\/mysql -p &#8212;&#8212; password -u &#8212;&#8212; user <\/p>\n<p>&nbsp;<\/p>\n<p> # vim mysql_full.sql shows the complete file db entries in [&#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\/2274"}],"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=2274"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2274\/revisions"}],"predecessor-version":[{"id":2276,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2274\/revisions\/2276"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}