Mysqldump back up and restore a mysql database

Source: Internet
Author: User

 

Mysqldump is an effective tool to backup MySQL database. it creates *. SQL file with DROP table, CREATE table and INSERT into SQL-statements of the source database. to restore the database, execute *. SQL file on destination database. for MyISAM, use mysqlhotcopy method that we explained earlier, as it is faster for MyISAM tables.

Using mysqldump, you can backup a local database and restore it on a remote database at the same time, using a single command. in this article, let us review several practical examples on how to use mysqldump to backup and restore.

 

For the impatient, here is the quick snippet of how backup and restore MySQL database using mysqldump:

Backup: # mysqldump-u root-p [root_password] [database_name]> dumpfilename. SQL

 

Restore: # mysql-u root-p [root_password] [database_name] <dumpfilename. SQL

How To Backup MySQL database

1. Backup a single database:

This example takes a backup of sugarcrm database and dumps the output to sugarcrm. SQL

# Mysqldump-u root-ptmppassword sugarcrm> sugarcrm. SQL

# Mysqldump-u root-p [root_password] [database_name]> dumpfilename. SQL

The sugarcrm. SQL will contain drop table, create table and insert command for all the tables in the sugarcrm database. following is a partial output of sugarcrm. SQL, showing the dump information of accounts_contacts table:

 

--

-- Table structure for table 'accounts _ contacts'

--

 

Drop table if exists 'accounts _ contacts'; SET @ saved_cs_client =@character_set_client;

SET character_set_client = utf8;

Create table 'accounts _ contacts '(

'Id' varchar (36) not null,

'Contact _ id' varchar (36) default NULL,

'Account _ id' varchar (36) default NULL,

'Date _ modified' datetime default NULL,

'Deleted' tinyint (1) not null default '0 ',

Primary key ('id '),

KEY 'idx _ account_contact '('account _ id', 'Contact _ id '),

KEY 'idx _ contid_del_accid' ('Contact _ id', 'deleted', 'account _ id ')

) ENGINE = MyISAM default charset = utf8;

SET character_set_client = @ saved_cs_client;

 

--

-- Dumping data for table 'accounts _ contacts'

--

 

Lock tables 'accounts _ contacts' WRITE;

/*! 40000 alter table 'accounts _ contacts' disable keys */;

Insert into 'accounts _ contacts' VALUES ('6ff90374-26d1-5fd8-b844-4873b2e42091 ',

'11ba0239-c7cf-e87e-e266-4873b218a3f9 ', '503a06a8-0650-6fdd-22ae-4153b245ae53 ',

'2017-07-23 05:24:30 ', 1 ),

('83126e77-eeda-f335-dc1b-4873bc805541 ', '7c525b1c-8a11-d803-94a5-4153bc4ff7d2 ',

'80a6add6-81ed-0266-6db5-4153bc54bfb5 ', '2017-07-23 05:24:30', 1 ),

('4e800b97-c09f-7896-d3d7-48751d81d5ee ', 'f241c222-b91a-d7a9-f355-48751d6bc0f9 ',

'2017-1f44-9f10-bdc4-48751db40009 ', '2017-07-23 05:24:30', 1 ),

('C94917ea-route 4-8430-e003-0000be0817f41 ', 'c564b7f3-2923-30b5-4861-0000be0f70cb3 ',

'C71eff65-b76b-cbb0-d31a-487be06e4e0b ', '2017-07-23 05:24:30', 1 ),

('7dab11e1-64d3-ea6a-c62c-performance17e4e41 ', '79d6f6e5-50e5-9b2b-034b-performance1dae5af ',

'7b886f23-571b-595b-19dd-performance1eee867 ', '2017-07-23 05:24:30', 1 );

/*! 40000 alter table 'accounts _ contacts' enable keys */;

Unlock tables;

2. Backup multiple databases:

If you want to backup multiple databases, first identify the databases that you want to backup using the show databases as shown below:

# Mysql-u root-ptmppassword

 

Mysql> show databases;

+ -------------------- +

| Database |

+ -------------------- +

| Information_schema |

| Bugs |

| Mysql |

| Sugarcr |

+ -------------------- +

4 rows in set (0.00 sec)

For example, if you want to take backup of both sugarcrm and bugs database, execute the mysqldump as shown below:

# Mysqldump-u root-ptmppassword -- databases bugs sugarcrm> bugs_sugarcrm. SQL

Verify the bugs_sugarcrm. SQL dumpfile contains both the database backup.

# Grep-I "Current database:"/tmp/bugs_sugarcrm. SQL

-- Current Database: 'mysql'

-- Current Database: 'sugarcrm'

3. Backup all the databases:

The following example takes a backup of all the database of the MySQL instance.

# Mysqldump-u root-ptmppassword -- all-databases>/tmp/all-database. SQL

4. Backup a specific table:

In this example, we backup only the accounts_contacts table from sugarcrm database.

# Mysqldump-u root-ptmppassword sugarcrm accounts_contacts \>/tmp/sugarcrm_accounts_contacts. SQL

4. Different mysqldump group options:

-Opt is a group option, which is same as-add-drop-table,-add-locks,-create-options,-quick,-extended-insert, -lock-tables,-set-charset, and-disable-keys. opt is enabled by default, disable with-skip-opt.

-Compact is a group option, which gives less verbose output (useful for debugging ). disables structure comments and header/footer constructs. enables options-skip-add-drop-table-no-set-names-skip-disable-keys-skip-add-locks

How To Restore MySQL database

1. Restore a database

In this example, to restore the sugarcrm database, execute mysql with <as shown below. when you are restoring the dumpfilename. SQL on a remote database, make sure to create the sugarcrm database before you can perform the restore.

# Mysql-u root-ptmppassword

Mysql> create database sugarcrm;

Query OK, 1 row affected (0.02 sec)

 

# Mysql-u root-ptmppassword sugarcrm </tmp/sugarcrm. SQL

 

# Mysql-u root-p [root_password] [database_name] <dumpfilename. SQL

2. Backup a local database and restore to remote server using single command:

This is a sleek option, if you want to keep a read-only database on the remote-server, which is a copy of the master database on local-server. the example below will backup the sugarcrm database on the local-server and restore it as sugarcrm1 database on the remote-server. please note that you shoshould first create the sugarcrm1 database on the remote-server before executing the following command.

[Local-server] # mysqldump-u root-ptmppassword sugarcrm | mysql \-u root-ptmppassword -- host = remote-server-C sugarcrm1 [Note: There are two -- (hyphen) in front of host]

This article is from "small steps and continuous steps !" Blog

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.